On this page

Sign up and sign in

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

SettingDefaultEffect
Allow new users to sign upOnOff: only users you create or invite can sign in.
E-mail providerOnE-mail and password, magic links and codes.
Confirm e-mailOnNew accounts must click the confirmation link first.
Secure e-mail changeOnAn e-mail change must be confirmed from both the old and the new address.
Link and code expiry3600 s60 s to 24 hours.
Code length66 to 10 digits.
Anonymous sign-insOffSee 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 session

The 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.