Create accounts and sign users in with e-mail and password.
JavaScript
const { data, error } = await stitchdb.auth.signUp({
email: '[email protected]',
password: 'a-strong-password',
options: { emailRedirectTo: 'https://example.com/welcome' },
})
await stitchdb.auth.signInWithPassword({ email: '[email protected]', password: 'a-strong-password' })
const { data: { user } } = await stitchdb.auth.getUser()
await stitchdb.auth.signOut()#Confirm e-mail
When Confirm e-mail is on (the default for new projects), signUp returns no session: the user gets an e-mail, and the link in it confirms the account, signs them in and sends them to emailRedirectTo. Turn it off in Studio → Authentication → Sign-in methods to let new accounts sign in right away. Resend the e-mail with:
JavaScript
await stitchdb.auth.resend({ type: 'signup', email: '[email protected]', options: { emailRedirectTo: 'https://example.com/welcome' } })#Sign-in methods settings
| Setting | Default | Effect |
|---|---|---|
| Allow new users to sign up | On | Off: only users you create or invite can sign in. |
| E-mail provider | On | E-mail and password, magic links and codes. |
| Confirm e-mail | On | New accounts must click the confirmation link first. |
| Secure e-mail change | On | An e-mail change must be confirmed from both the old and the new address. |
| Link and code expiry | 3600 s | 60 s to 24 hours. |
| Code length | 6 | 6 to 10 digits. |
| Anonymous sign-ins | Off | See Anonymous sign-ins. |
#The signed-in user
JavaScript
const { data: { user } } = await stitchdb.auth.getUser() // asks the server
const { data: { session } } = await stitchdb.auth.getSession() // reads the stored sessionThe SDK stores the session and refreshes it automatically. Use getUser() when you need to be sure the account still exists and isn't banned.
#With cURL
Terminal
curl -X POST 'https://<project-ref>.p.stitchdb.com/auth/v1/signup' \
-H "apikey: <anon-key>" -H "Content-Type: application/json" \
-d '{"email":"[email protected]","password":"a-strong-password"}'
curl -X POST 'https://<project-ref>.p.stitchdb.com/auth/v1/token?grant_type=password' \
-H "apikey: <anon-key>" -H "Content-Type: application/json" \
-d '{"email":"[email protected]","password":"a-strong-password"}'#React to sign-in changes
JavaScript
stitchdb.auth.onAuthStateChange((event, session) => {
if (event === 'SIGNED_IN') console.log('Hello', session.user.email)
if (event === 'SIGNED_OUT') console.log('Bye')
})Next: passwords and recovery, redirect URLs and e-mails.