|
import { writable } from 'svelte/store'; |
|
|
|
export interface UserInfo { |
|
name?: string; |
|
preferred_username?: string; |
|
email?: string; |
|
} |
|
|
|
export interface AuthSession { |
|
accessToken: string; |
|
userInfo: UserInfo; |
|
} |
|
|
|
export interface AuthState { |
|
isAuthenticated: boolean; |
|
session: AuthSession | null; |
|
isLoading: boolean; |
|
showSignIn: boolean; |
|
bannerMessage: string; |
|
userInfo: UserInfo | null; |
|
} |
|
|
|
const BANNER_ANON = "⚠️ You are not logged in – anonymous users get only ~60 seconds of zero-GPU time per day. Sign in for higher limits."; |
|
|
|
function createAuthStore() { |
|
const { subscribe, set, update } = writable<AuthState>({ |
|
isAuthenticated: false, |
|
session: null, |
|
isLoading: true, |
|
showSignIn: false, |
|
bannerMessage: "", |
|
userInfo: null |
|
}); |
|
|
|
return { |
|
subscribe, |
|
setSession: (session: AuthSession | null) => { |
|
update(state => ({ |
|
...state, |
|
isAuthenticated: !!session, |
|
session, |
|
isLoading: false, |
|
showSignIn: !session, |
|
bannerMessage: session ? "" : BANNER_ANON, |
|
userInfo: session?.userInfo || null |
|
})); |
|
}, |
|
setLoading: (isLoading: boolean) => { |
|
update(state => ({ ...state, isLoading })); |
|
}, |
|
setBannerMessage: (message: string) => { |
|
update(state => ({ ...state, bannerMessage: message })); |
|
}, |
|
reset: () => { |
|
set({ |
|
isAuthenticated: false, |
|
session: null, |
|
isLoading: false, |
|
showSignIn: true, |
|
bannerMessage: BANNER_ANON, |
|
userInfo: null |
|
}); |
|
} |
|
}; |
|
} |
|
|
|
export const authStore = createAuthStore(); |