feat(app): profile photo — avatar upload in Mon compte, photo in the sidebar, drawer and top bar
packages/supabase assets module: closed AssetFailure kinds, begin / quarantine upload (upsert off) / confirm / remove, own-row state read with an explicit column list, download under the SELECT policy. App: client transport (magic byte sniff, never SVG, createImageBitmap, canvas 1024 px, JPEG 0.9, 5 MiB cap), AvatarSection (preview, save, 1 s polling up to 30 s, rejection and failure messages per closed code, removal with confirmation, in-flight row resumed at mount), auth store blob URL revoked on change and sign-out, AvatarCircle in UserBadge and the top bar link to Mon compte (D3), FR/EN strings, fake client extended, 27 new tests (app 300).
This commit is contained in:
1 parent
614887470c
commit
926dd7df8c
18 files changed
+1550
-31
No files matched your search
@@ -10,6 +10,9 @@ import {
|
||||
signOut as signOutRequest,
|
||||
updateOwnProfile,
|
||||
type AuthFailureKind,
|
||||
type AvatarState,
|
||||
downloadAvatar,
|
||||
fetchOwnAvatarState,
|
||||
type FaunosteoClient,
|
||||
type OwnProfilePatch,
|
||||
type PlatformRole,
|
||||
@@ -34,22 +37,59 @@ export interface AuthState {
|
||||
recovery: boolean
|
||||
/** Failure while reading the URL (expired link, bad code): shown by the landing routes. */
|
||||
urlFailure: AuthFailureKind | null
|
||||
/** Own avatar rows reduced to clean / in flight / last rejected; null until loaded. */
|
||||
avatar: AvatarState | null
|
||||
/** Blob URL of the clean photo (downloaded under RLS), revoked on change or sign-out. */
|
||||
avatarUrl: string | null
|
||||
/** Subscribes to the client; returns the unsubscribe. Safe to call twice. */
|
||||
start: () => () => void
|
||||
refreshAccount: () => Promise<void>
|
||||
updateProfile: (patch: OwnProfilePatch) => Promise<Profile>
|
||||
/** Re-reads the own rows; downloads the clean photo when it changed. */
|
||||
refreshAvatar: () => Promise<AvatarState>
|
||||
/** Forgets the photo locally (after remove_avatar or on sign-out). */
|
||||
clearAvatar: () => void
|
||||
signOut: () => Promise<void>
|
||||
clearRecovery: () => void
|
||||
}
|
||||
|
||||
type Set = (patch: Partial<AuthState>) => void
|
||||
type Get = () => AuthState
|
||||
|
||||
async function loadAccount(client: FaunosteoClient, user: User, set: Set) {
|
||||
/** Blob URLs are a browser feature; tests under happy-dom may lack them. */
|
||||
function revokeUrl(url: string | null) {
|
||||
if (url && typeof URL.revokeObjectURL === 'function') URL.revokeObjectURL(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the avatar state and keeps `avatarUrl` in step with the clean row:
|
||||
* a new clean id downloads once, no clean row drops the URL.
|
||||
*/
|
||||
async function syncAvatar(client: FaunosteoClient, user: User, set: Set, get: Get) {
|
||||
const avatar = await fetchOwnAvatarState(client, user.id)
|
||||
const previous = get().avatar?.clean?.id ?? null
|
||||
const current = avatar.clean?.id ?? null
|
||||
if (current === previous && (current === null || get().avatarUrl)) {
|
||||
set({ avatar })
|
||||
return avatar
|
||||
}
|
||||
let avatarUrl: string | null = null
|
||||
if (avatar.clean && typeof URL.createObjectURL === 'function') {
|
||||
const blob = await downloadAvatar(client, avatar.clean.object_key)
|
||||
avatarUrl = URL.createObjectURL(blob)
|
||||
}
|
||||
revokeUrl(get().avatarUrl)
|
||||
set({ avatar, avatarUrl })
|
||||
return avatar
|
||||
}
|
||||
|
||||
async function loadAccount(client: FaunosteoClient, user: User, set: Set, get: Get) {
|
||||
const [profile, roles] = await Promise.all([
|
||||
fetchOwnProfile(client, user.id),
|
||||
fetchOwnPlatformRoles(client, user.id),
|
||||
])
|
||||
set({ profile, roles })
|
||||
await syncAvatar(client, user, set, get).catch(() => set({ avatar: null }))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,9 +101,11 @@ function applyAuthEvent(
|
||||
event: AuthChangeEvent,
|
||||
session: Session | null,
|
||||
set: Set,
|
||||
previousUserId: string | undefined,
|
||||
get: Get,
|
||||
) {
|
||||
const previousUserId = get().user?.id
|
||||
if (event === 'SIGNED_OUT' || session === null) {
|
||||
revokeUrl(get().avatarUrl)
|
||||
set({
|
||||
status: 'signed_out',
|
||||
session: null,
|
||||
@@ -71,6 +113,8 @@ function applyAuthEvent(
|
||||
profile: null,
|
||||
roles: [],
|
||||
recovery: false,
|
||||
avatar: null,
|
||||
avatarUrl: null,
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -83,7 +127,7 @@ function applyAuthEvent(
|
||||
set(patch)
|
||||
if (session.user.id !== previousUserId || event === 'USER_UPDATED') {
|
||||
setTimeout(() => {
|
||||
void loadAccount(client, session.user, set).catch(() => {
|
||||
void loadAccount(client, session.user, set, get).catch(() => {
|
||||
set({ profile: null, roles: [] })
|
||||
})
|
||||
}, 0)
|
||||
@@ -109,6 +153,8 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
roles: [],
|
||||
recovery: false,
|
||||
urlFailure: null,
|
||||
avatar: null,
|
||||
avatarUrl: null,
|
||||
|
||||
start: () => {
|
||||
const client = getSupabaseClient()
|
||||
@@ -120,13 +166,26 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
if (error) set({ urlFailure: classifyAuthError(error) })
|
||||
})
|
||||
const { data } = client.auth.onAuthStateChange((event, session) => {
|
||||
applyAuthEvent(client, event, session, set, get().user?.id)
|
||||
applyAuthEvent(client, event, session, set, get)
|
||||
})
|
||||
return () => data.subscription.unsubscribe()
|
||||
},
|
||||
|
||||
refreshAccount: async () => {
|
||||
await loadAccount(requireClient(), requireUser(get().user), set)
|
||||
await loadAccount(requireClient(), requireUser(get().user), set, get)
|
||||
},
|
||||
|
||||
refreshAvatar: async () => {
|
||||
return syncAvatar(requireClient(), requireUser(get().user), set, get)
|
||||
},
|
||||
|
||||
clearAvatar: () => {
|
||||
revokeUrl(get().avatarUrl)
|
||||
const avatar = get().avatar
|
||||
set({
|
||||
avatarUrl: null,
|
||||
avatar: avatar ? { ...avatar, clean: null, inFlight: null } : null,
|
||||
})
|
||||
},
|
||||
|
||||
updateProfile: async (patch) => {
|
||||
@@ -137,14 +196,19 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
},
|
||||
|
||||
signOut: async () => {
|
||||
const clear = () => set({
|
||||
status: 'signed_out',
|
||||
session: null,
|
||||
user: null,
|
||||
profile: null,
|
||||
roles: [],
|
||||
recovery: false,
|
||||
})
|
||||
const clear = () => {
|
||||
revokeUrl(get().avatarUrl)
|
||||
set({
|
||||
status: 'signed_out',
|
||||
session: null,
|
||||
user: null,
|
||||
profile: null,
|
||||
roles: [],
|
||||
recovery: false,
|
||||
avatar: null,
|
||||
avatarUrl: null,
|
||||
})
|
||||
}
|
||||
try {
|
||||
// auth-js clears the local session even when the server call fails.
|
||||
await signOutRequest(requireClient())
|
||||
|
||||
Reference in new issue
Block a user