User Module
If you are starting from an example project - all types, utilities, and modules are installed by default. Therefore, you can continue reviewing information about usage.
Installation
bash
cd modules
degit webscopeio/supabase-modules/apps/next/modules/user user
bash
cd components
degit webscopeio/supabase-modules/apps/next/components/user user
Then copy, paste and execute the contents of file modules/user/migration.sql
in your Supabase SQL Editor.
Authentication
Authentication supports the following methods:
- Sign ups using email and password
- Sign ins using email and password
- Sign ins using email One-Time password (OTP workflow)
- Anonymous Sign ins
- Sign outs
- Password reset using email (Magic Link workflow)
- Updating credentials email and/or password
signUpWithEmailPassword
ts
export async function signUpWithEmailPassword(
options: WithRedirect<
Extract<SignUpWithPasswordCredentials, { email: string }>
>
): Promise<ServerError | void> {
const { redirect, ...credentials } = options
const supabase = createClient()
const { data, error } = await supabase.auth.signUp(credentials)
if (error) return { error: { message: error.message } }
if (data.user?.identities?.length === 0) {
return { error: { message: "User already registered" } }
}
redirect?.url && _redirect(redirect.url, redirect.type)
}
signInWithEmailPassword
ts
export async function signInWithEmailPassword(
options: WithRedirect<
Extract<SignInWithPasswordCredentials, { email: string }>
>
): Promise<ServerError | void> {
const { redirect, ...credentials } = options
const supabase = createClient()
const { error } = await supabase.auth.signInWithPassword(credentials)
if (error) return { error: { message: error.message } }
redirect?.url && _redirect(redirect.url, redirect.type)
}
signInWithOtp
ts
export async function signInWithOtp(
options: WithRedirect<
Extract<SignInWithPasswordlessCredentials, { email: string }>
>
): Promise<ServerError | void> {
const { redirect, ...credentials } = options
const supabase = createClient()
const { error } = await supabase.auth.signInWithOtp(credentials)
if (error) return { error: { message: error.message } }
redirect?.url && _redirect(redirect.url, redirect.type)
}
signInAnonymously
ts
export async function signInAnonymously(
options: WithRedirect<SignInAnonymouslyCredentials>
): Promise<ServerError | void> {
const { redirect, ...credentials } = options
const supabase = createClient()
const { error } = await supabase.auth.signInAnonymously(credentials)
if (error) return { error: { message: error.message } }
redirect?.url && _redirect(redirect.url, redirect.type)
}
verifyOtp
ts
export async function verifyOtp(
options: WithRedirect<
Omit<Extract<VerifyOtpParams, { email: string }>, "type">
>
): Promise<ServerError | void> {
const { redirect, ...credentials } = options
const supabase = createClient()
const { error } = await supabase.auth.verifyOtp({
email: credentials.email,
token: credentials.token,
type: "email",
})
if (error) return { error: { message: error.message } }
redirect?.url && _redirect(redirect.url, redirect.type)
}
signOut
ts
export async function signOut(
options: WithRedirect
): Promise<ServerError | void> {
const { redirect } = options
const supabase = createClient()
const { error } = await supabase.auth.signOut()
if (error) return { error: { message: error.message } }
redirect?.url && _redirect(redirect.url, redirect.type)
}
resetPasswordForEmail
ts
export async function resetPasswordForEmail(
options: WithRedirect<{ email: string }>
): Promise<ServerError | void> {
const { redirect, email } = options
const supabase = createClient()
const { error } = await supabase.auth.resetPasswordForEmail(email)
if (error) return { error: { message: error.message } }
redirect?.url && _redirect(redirect.url, redirect.type)
}
updateUser
ts
export async function updateUser(
options: WithRedirect<UserAttributes>
): Promise<ServerError | void> {
const { redirect, ...attributes } = options
const supabase = createClient()
const { error } = await supabase.auth.updateUser(attributes)
if (error) return { error: { message: error.message } }
redirect?.url && _redirect(redirect.url, redirect.type)
}
Profile
getProfile
ts
export async function getProfile({
id,
}: {
id: string
}): Promise<Profile | ServerError> {
const supabase = createClient()
const { data, error } = await supabase
.from("profiles")
.select("*")
.eq("id", id)
.single()
if (error) return { error: { message: error.message } }
return data
}
updateProfile
ts
export async function updateProfile(
options: WithRedirect<PartialExcept<Profile, "id">>
): Promise<ServerError | void> {
const { redirect, ...updates } = options
const supabase = createClient()
const { error } = await supabase
.from("profiles")
.update(updates)
.eq("id", updates.id)
if (error) return { error: { message: error.message } }
redirect?.url && _redirect(redirect.url, redirect.type)
}