I wanted my own auth UI mostly so that I could get a little more information off the user when signing up (I wanted a username and to add a newsletter checkbox).
and doing so I need to code up the sign-in UI, as well as password recovery (I should probably add email update but haven’t yet), Non-UI things that were added are dealing with incoming confirmation_tokens that come from the emails gotrue sends.
There is a cook book for gotrue Auth, though it’s for auth without email verification, which is great if you can get away with it makes the implementation much simpler.
While not the cleanest PR ever, I did put some effort into commenting much of what’s happening. So if you’re looking to implement something similar you should be able skip some of the struggling I did.
For the casual reader some highlights are
You can still use the login and signup functions from useAuth though they now need params. Confusingly enough they both take a remember param but they do very different things, on login it has to do with persisting the user’s login, and for signup remember is extra data that ends up on the user_metadata property for the identity-signup netlify function. This is redwood specific as params have different names in the gotrue auth, I discuss it more in the PR.
As far as I know confirm, recover, requestPasswordRecovery and update functions need to be done using the client directly. These are all related to dealing with verifying emails and changing passwords.
There is a useEffect that looks for specific hash params in the URL to take verification actions
const hash = window.location.hash
useEffect(() => {
const [key, token] = hash.slice(1).split('=')
if (key === 'confirmation_token') {
// send token to go true to verify email
} else if (key === 'recovery_token') {
// send recovery token to gotrue to temporarily log the user in
// then redirect to the update password page
}
}, [hash, client])
Wanted to post a quick thanks! Was planning on replacing the Netlify Identity Widget with GoTrue this weekend and this was a great resource and timesaver.
Especially the intel on the behavior of the “remember” object.
For those who want use the GoTrue authentication interface and still want “verifying emails and changing passwords” but do not want the Netlify Widget, you can always:
Implement your own sign/up forms. Just use the client from the Auth Provider.
Hi! Nice job! I’m trying to do nearly the same on my project, but i need only sign up by invitation. So far I implemented verification of invitation token and big question for me: How can i implement sending invitation email from my app? Docs not really helped me, so maybe You have some idea on this? I would really appreciate any help
@dthyresson Hi. Thanks for interesting article, but actually i asked about a little bit different stuff: on the Netlify website on identity sesction you can invite user by email, and he/she will receive email with invitation token. GoTrue provides API to send invitation:
Invite a user
To invite a user using the admin token, do a POST request to /invite endpoint. It’s not possible to set user_metadata or app_metadata until a user has been created.
How can i implement sending invitation email from my app?
thinking you wondered how to send an email from a service.
Remember, GoTrue is an API – but you still have to implement the functionality that the API exposes.
So, while, yes, the API has an invite, by rolling your own auth, you still need to implement sending the mail, creating the link, and then handling the link back in the app that registers the user.
For example, Supabase also uses the GoTrue API because it establishes those endpoints and a standard interface, but they still implement the emails, the invites, the change password, the forgot password, the verification and generating tokens.
Atm I’m pretty happy with the functionality for an MVP (a number of rough edges but that’s kinda the point) and so I’m close to trying to get a few users onto the website for some early feedback, there are just a couple of things I want to do first.
I want to make a few more example posts (there’s mostly dumby data there atm and it’s obviously the case).
Do a bit of research about if I need and what kind of community policy to have on the website (I’ve not stood up a public app before, I assume there are templates available ).
Figure out what needs to be done for GDPR.
I’ll definitely put up a show and tell at some point soonish for sure.
When it comes to community policy you want a step-by-step guide to contributing if it’s open source, but otherwise just an FAQ about how stuff works is probably good. The other piece is then a Code of Conduct which will require a little more care. Definitely check out both of those resources from Redwood, the core team did a really great job with those.
The contributing guide will be more specific to whatever your project is, but if you’re looking for other CoC’s then some other communities that are known for having really good resources on that would include Rust and Vue.
const { error, data } = await auth.api
.inviteUserByEmail('example@email.com')
Is the Supabase code example.
Here the auth is the auth client of their sdk.
So, in Redwood, you can access your auth client that you’ve declared
<AuthProvider client={supabase} type="supabase">
using the useAuth() hook like:
import { useAuth } from '@redwoodjs/auth'
....
// in some React component, like a Page or Layout etc
const SomePage = () => {
const { client } = useAuth()
and then you can access anything on the Supabase client as seen here:
`client.auth.api.inviteUserByEmail`
Or, you could do this in your api in a service by simply importing the SupabaseClient in your api and creating it like you might using the anon token and using that to supabaseClient.auth.api.inviteUserByEmail
Supabase would send the email. I’d look at the Auth logs in your Supabase dashboard auth section to see if there is any error messages – and then ask Supabase support here: supabase · Discussions · GitHub
@dthyresson Thanks. Yes, expecting Supabase to send the email. Plus the input is good, I just tested the api call on the front-end and it works as expected…
//web side, this works but I want to create it as a service
const onSave = async (input) => {
await supabaseAdmin.auth.api.inviteUserByEmail(input.email)
...
The issue I a have is how to call the following function/service and invoke the api call via the front-end. Can I import the function? Or should I use fetch, POST, etc. I’m experimenting with the latter atm (see below)*
//api side, how can I call this function in the front-end?
export const inviteUserByEmail = async ({ input }) => {
const { data, error } = await supabaseAdmin.auth.api.inviteUserByEmail(
input.email
)
}
*This is what I currently have (I need to read-up on this topic)