Every method of stitchdb.auth: sign up and in, sessions, users, MFA and the admin API.
#Sign up and sign in
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()#More sign-in methods
JavaScript
// Magic link or one-time code by e-mail
await stitchdb.auth.signInWithOtp({ email: '[email protected]', options: { emailRedirectTo: 'https://example.com' } })
await stitchdb.auth.verifyOtp({ email: '[email protected]', token: '123456', type: 'email' })
// Password reset: the e-mail link signs the user in and sends them to redirectTo
await stitchdb.auth.resetPasswordForEmail('[email protected]', { redirectTo: 'https://example.com/reset' })
await stitchdb.auth.updateUser({ password: 'a-new-password' }) // on the /reset page
// Change e-mail (confirmed by link), resend a confirmation
await stitchdb.auth.updateUser({ email: '[email protected]' })
await stitchdb.auth.resend({ type: 'signup', email: '[email protected]' })
// OAuth (enable the provider in Studio first) and anonymous sign-ins
await stitchdb.auth.signInWithOAuth({ provider: 'github', options: { redirectTo: 'https://example.com' } })
await stitchdb.auth.signInAnonymously()#Session and user
JavaScript
const { data: { session } } = await stitchdb.auth.getSession()
const { data: { user } } = await stitchdb.auth.getUser()
await stitchdb.auth.refreshSession()
await stitchdb.auth.setSession({ access_token, refresh_token })
await stitchdb.auth.updateUser({ data: { display_name: 'Ada' } }) // user_metadata#onAuthStateChange
JavaScript
stitchdb.auth.onAuthStateChange((event, session) => {
if (event === 'SIGNED_IN') console.log('Hello', session.user.email)
if (event === 'SIGNED_OUT') console.log('Bye')
})#MFA
JavaScript
const { data: f } = await stitchdb.auth.mfa.enroll({ factorType: 'totp' }) // show f.totp.qr_code
const { data: c } = await stitchdb.auth.mfa.challenge({ factorId: f.id })
await stitchdb.auth.mfa.verify({ factorId: f.id, challengeId: c.id, code: '123456' }) // session is now aal2Also mfa.listFactors(), mfa.unenroll(), mfa.challengeAndVerify() and mfa.getAuthenticatorAssuranceLevel(). See Multi-factor authentication.
#Identities
JavaScript
await stitchdb.auth.getUserIdentities()
await stitchdb.auth.linkIdentity({ provider: 'github' })
await stitchdb.auth.unlinkIdentity(identity)#Admin (service_role key)
JavaScript
const admin = createClient('https://<project-ref>.p.stitchdb.com', '<service-role-key>')
const { data } = await admin.auth.admin.listUsers()
await admin.auth.admin.createUser({ email: '[email protected]', password: 'a-strong-password', email_confirm: true })
await admin.auth.admin.inviteUserByEmail('[email protected]')
await admin.auth.admin.updateUserById(userId, { ban_duration: '24h' }) // 'none' lifts the ban
const { data: link } = await admin.auth.admin.generateLink({ type: 'recovery', email: '[email protected]' })
await admin.auth.admin.deleteUser(userId)Also admin.getUserById, admin.signOut(jwt, scope), admin.mfa.listFactors and admin.mfa.deleteFactor. See Manage users.