feat(app): identity screens and /app shell — auth store, login/signup/reset flows, dashboard, account
- state/authStore: session subscription (initialize + onAuthStateChange), profile + roles load, recovery flag, URL failure; services/localeSync: profiles.locale <-> localeStore - routes: /login, /signup, /forgot-password, /reset-password, /auth/callback; guards RequireAuth (next=) and RedirectIfSignedIn; /app (dashboard), /app/account behind the guard - shell: AppLayout (desktop sidebar, light top bar, mobile drawer), SidebarNav, UserBadge, SignOutButton - account: personal / professional / preferences / security sections, one save each, Zod validation with i18n keys - ui: Icon, FormField, Notice, ButtonLink (buttonClassName shared), PasswordField, OAuthButtons (config-driven) - Home header: Mon espace / Se connecter when Supabase is configured; local tool routes untouched - i18n: auth.*, shell.*, dashboard.*, account.*, home.space.* in fr + en
This commit is contained in:
1 parent
0f7292f9b3
commit
c517ea267b
46 files changed
+2798
-11
No files matched your search
@@ -0,0 +1,153 @@
|
||||
import { create } from 'zustand'
|
||||
import {
|
||||
AuthFailure,
|
||||
type AuthChangeEvent,
|
||||
type Session,
|
||||
type User,
|
||||
classifyAuthError,
|
||||
fetchOwnPlatformRoles,
|
||||
fetchOwnProfile,
|
||||
signOut as signOutRequest,
|
||||
updateOwnProfile,
|
||||
type AuthFailureKind,
|
||||
type FaunosteoClient,
|
||||
type OwnProfilePatch,
|
||||
type PlatformRole,
|
||||
type Profile,
|
||||
} from '@faunosteo/supabase'
|
||||
import { getSupabaseClient } from '@/lib/supabase'
|
||||
|
||||
/**
|
||||
* unavailable = no Supabase configured (local-only deployment); loading until
|
||||
* the client has read the persisted session and the URL (OAuth / email links).
|
||||
*/
|
||||
export type AuthStatus = 'loading' | 'unavailable' | 'signed_out' | 'signed_in'
|
||||
|
||||
export interface AuthState {
|
||||
status: AuthStatus
|
||||
session: Session | null
|
||||
user: User | null
|
||||
/** Null until loaded, or when the profile row is missing. */
|
||||
profile: Profile | null
|
||||
roles: PlatformRole[]
|
||||
/** Set by a password-recovery link until the new password is saved. */
|
||||
recovery: boolean
|
||||
/** Failure while reading the URL (expired link, bad code): shown by the landing routes. */
|
||||
urlFailure: AuthFailureKind | null
|
||||
/** Subscribes to the client; returns the unsubscribe. Safe to call twice. */
|
||||
start: () => () => void
|
||||
refreshAccount: () => Promise<void>
|
||||
updateProfile: (patch: OwnProfilePatch) => Promise<Profile>
|
||||
signOut: () => Promise<void>
|
||||
clearRecovery: () => void
|
||||
}
|
||||
|
||||
type Set = (patch: Partial<AuthState>) => void
|
||||
|
||||
async function loadAccount(client: FaunosteoClient, user: User, set: Set) {
|
||||
const [profile, roles] = await Promise.all([
|
||||
fetchOwnProfile(client, user.id),
|
||||
fetchOwnPlatformRoles(client, user.id),
|
||||
])
|
||||
set({ profile, roles })
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies one GoTrue event. The account load runs outside the callback: the
|
||||
* client serialises its callbacks, awaiting a request inside one deadlocks.
|
||||
*/
|
||||
function applyAuthEvent(
|
||||
client: FaunosteoClient,
|
||||
event: AuthChangeEvent,
|
||||
session: Session | null,
|
||||
set: Set,
|
||||
previousUserId: string | undefined,
|
||||
) {
|
||||
if (event === 'SIGNED_OUT' || session === null) {
|
||||
set({
|
||||
status: 'signed_out',
|
||||
session: null,
|
||||
user: null,
|
||||
profile: null,
|
||||
roles: [],
|
||||
recovery: false,
|
||||
})
|
||||
return
|
||||
}
|
||||
const patch: Partial<AuthState> = {
|
||||
status: 'signed_in',
|
||||
session,
|
||||
user: session.user,
|
||||
}
|
||||
if (event === 'PASSWORD_RECOVERY') patch.recovery = true
|
||||
set(patch)
|
||||
if (session.user.id !== previousUserId || event === 'USER_UPDATED') {
|
||||
setTimeout(() => {
|
||||
void loadAccount(client, session.user, set).catch(() => {
|
||||
set({ profile: null, roles: [] })
|
||||
})
|
||||
}, 0)
|
||||
}
|
||||
}
|
||||
|
||||
function requireClient(): FaunosteoClient {
|
||||
const client = getSupabaseClient()
|
||||
if (!client) throw new AuthFailure('unknown')
|
||||
return client
|
||||
}
|
||||
|
||||
function requireUser(user: User | null): User {
|
||||
if (!user) throw new AuthFailure('session_expired')
|
||||
return user
|
||||
}
|
||||
|
||||
export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
status: 'loading',
|
||||
session: null,
|
||||
user: null,
|
||||
profile: null,
|
||||
roles: [],
|
||||
recovery: false,
|
||||
urlFailure: null,
|
||||
|
||||
start: () => {
|
||||
const client = getSupabaseClient()
|
||||
if (!client) {
|
||||
set({ status: 'unavailable' })
|
||||
return () => undefined
|
||||
}
|
||||
void client.auth.initialize().then(({ error }) => {
|
||||
if (error) set({ urlFailure: classifyAuthError(error) })
|
||||
})
|
||||
const { data } = client.auth.onAuthStateChange((event, session) => {
|
||||
applyAuthEvent(client, event, session, set, get().user?.id)
|
||||
})
|
||||
return () => data.subscription.unsubscribe()
|
||||
},
|
||||
|
||||
refreshAccount: async () => {
|
||||
await loadAccount(requireClient(), requireUser(get().user), set)
|
||||
},
|
||||
|
||||
updateProfile: async (patch) => {
|
||||
const user = requireUser(get().user)
|
||||
const profile = await updateOwnProfile(requireClient(), user.id, patch)
|
||||
set({ profile })
|
||||
return profile
|
||||
},
|
||||
|
||||
signOut: async () => {
|
||||
await signOutRequest(requireClient())
|
||||
// SIGNED_OUT also arrives through the subscription; this keeps the UI honest if it does not.
|
||||
set({
|
||||
status: 'signed_out',
|
||||
session: null,
|
||||
user: null,
|
||||
profile: null,
|
||||
roles: [],
|
||||
recovery: false,
|
||||
})
|
||||
},
|
||||
|
||||
clearRecovery: () => set({ recovery: false }),
|
||||
}))
|
||||
Reference in new issue
Block a user