Files
faunosteo/packages/supabase/src/profiles.ts
T
bchanot b10b227ee9 feat(supabase): auth, profile and roles services; professional columns on profiles; OAuth + TOTP config
- packages/supabase: signIn/signUp/OAuth/reset/updatePassword/signOut wrappers with a closed AuthFailure kind, fetch/update own profile, own platform roles
- migration 20261011100000: practice_name, professional_title, siret (format-checked, column grants); pgTAP 009; database.types regenerated
- config.toml: TOTP enroll/verify on, [auth.external.google|apple] via env(), wildcard redirect URLs
- app config: authProviders (APP_AUTH_PROVIDERS / VITE_AUTH_PROVIDERS), entrypoint env.js, DEPLOY table
- TODO: Step 7 plan
2026-10-11 23:16:13 +02:00

56 lines
1.4 KiB
TypeScript

import type { FaunosteoClient } from './client'
import type { Tables, TablesUpdate } from './database.types'
export type Profile = Tables<'profiles'>
/** Columns the signed-in user may change (column grant in the profiles migrations). */
export type OwnProfilePatch = Pick<
TablesUpdate<'profiles'>,
| 'display_name'
| 'first_name'
| 'last_name'
| 'phone'
| 'locale'
| 'timezone'
| 'practice_name'
| 'professional_title'
| 'siret'
>
export class ProfileError extends Error {
readonly code: string | null
constructor(message: string, code: string | null, cause?: unknown) {
super(message, { cause })
this.name = 'ProfileError'
this.code = code
}
}
/** The row RLS lets the user see: their own. Null when the trigger has not run yet. */
export async function fetchOwnProfile(
client: FaunosteoClient,
userId: string,
): Promise<Profile | null> {
const { data, error } = await client
.from('profiles')
.select('*')
.eq('id', userId)
.maybeSingle()
if (error) throw new ProfileError(error.message, error.code, error)
return data
}
export async function updateOwnProfile(
client: FaunosteoClient,
userId: string,
patch: OwnProfilePatch,
): Promise<Profile> {
const { data, error } = await client
.from('profiles')
.update(patch)
.eq('id', userId)
.select('*')
.single()
if (error) throw new ProfileError(error.message, error.code, error)
return data
}