hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { |
| 2 | type Accessor, |
| 3 | type ParentProps, |
| 4 | createContext, |
| 5 | createEffect, |
| 6 | createSignal, |
| 7 | onCleanup, |
| 8 | useContext, |
| 9 | } from "solid-js"; |
| 10 | import { createResource } from "@/lib/create-resource"; |
| 11 | import { getMe } from "@/api/users"; |
| 12 | import { postLogout } from "@/api/auth"; |
| 13 | import type { User } from "@/api/client"; |
| 14 | import { ApiError } from "@/api/client"; |
| 15 | import { devAutoLogin, stopDevAutoLogin } from "@/lib/dev-auto-login"; |
| 16 | import { usePreferences } from "@/stores/preferences-context"; |
| 17 | |
| 18 | type AuthContextValue = { |
| 19 | user: Accessor<User | null | undefined>; |
| 20 | loading: Accessor<boolean>; |
| 21 | /** Non-401 bootstrap failure. 401 is "logged out", not an error. */ |
| 22 | error: Accessor<unknown>; |
| 23 | refresh: () => Promise<User | null | undefined>; |
| 24 | logout: () => Promise<void>; |
| 25 | }; |
| 26 | |
| 27 | const AuthContext = createContext<AuthContextValue>(); |
| 28 | |
| 29 | /** Retry-After (seconds) for a transient 429/503, else null. */ |
| 30 | function retryAfterFor(err: unknown): number | null { |
| 31 | if ( |
| 32 | err instanceof ApiError && |
| 33 | (err.status === 429 || err.status === 503) && |
| 34 | err.retryAfter != null |
| 35 | ) { |
| 36 | return Math.min(Math.max(err.retryAfter, 0), 60); |
| 37 | } |
| 38 | return null; |
| 39 | } |
| 40 | |
| 41 | export function AuthProvider(props: ParentProps) { |
| 42 | const prefs = usePreferences(); |
| 43 | const [override, setOverride] = createSignal<User | null | undefined>(undefined); |
| 44 | const [error, setError] = createSignal<unknown>(null); |
| 45 | // The fetcher swallows every failure into `error` state and resolves |
| 46 | // IMMEDIATELY: it must never stay `loading` (that suspends the whole shell |
| 47 | // and blanks the page) nor re-throw during render. Transient 429/503 waits |
| 48 | // happen off the resource, in the background timer below. |
| 49 | const [me, { refetch }] = createResource(async () => { |
| 50 | setError(null); |
| 51 | try { |
| 52 | return await getMe(); |
| 53 | } catch (err) { |
| 54 | if (err instanceof ApiError && err.status === 401) return await devAutoLogin(); |
| 55 | setError(err); |
| 56 | return null; |
| 57 | } |
| 58 | }); |
| 59 | |
| 60 | const user: Accessor<User | null | undefined> = () => { |
| 61 | const o = override(); |
| 62 | if (o !== undefined) return o; |
| 63 | return me(); |
| 64 | }; |
| 65 | |
| 66 | createEffect(() => { |
| 67 | const u = user(); |
| 68 | if (u) prefs.hydratePreferences(u); |
| 69 | }); |
| 70 | |
| 71 | const refresh = async () => { |
| 72 | setOverride(undefined); |
| 73 | const result = await refetch(); |
| 74 | return result as User | null | undefined; |
| 75 | }; |
| 76 | |
| 77 | // Auto-heal a rate-limited bootstrap without blocking render: once `error` |
| 78 | // holds a 429/503 with a Retry-After, schedule one background refresh. The |
| 79 | // user is already looking at the error Alert while this waits; on success |
| 80 | // the app paints. Re-running the effect (new attempt) clears the old timer. |
| 81 | createEffect(() => { |
| 82 | const wait = retryAfterFor(error()); |
| 83 | if (wait == null) return; |
| 84 | const timer = setTimeout(() => void refresh(), wait * 1000); |
| 85 | onCleanup(() => clearTimeout(timer)); |
| 86 | }); |
| 87 | |
| 88 | const logout = async () => { |
| 89 | stopDevAutoLogin(); |
| 90 | try { |
| 91 | await postLogout(); |
| 92 | } finally { |
| 93 | setOverride(null); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | const value: AuthContextValue = { |
| 98 | user, |
| 99 | loading: () => me.loading && override() === undefined, |
| 100 | error: () => (override() === undefined ? error() : null), |
| 101 | refresh, |
| 102 | logout, |
| 103 | }; |
| 104 | |
| 105 | return <AuthContext.Provider value={value}>{props.children}</AuthContext.Provider>; |
| 106 | } |
| 107 | |
| 108 | export function useAuth(): AuthContextValue { |
| 109 | const ctx = useContext(AuthContext); |
| 110 | if (!ctx) throw new Error("useAuth must be used within AuthProvider"); |
| 111 | return ctx; |
| 112 | } |