Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 29x 29x 29x 29x 29x 29x 29x 5x 5x 5x 5x 5x 29x 29x 29x 3x 3x 29x 29x | import { useState } from 'react'
import type { MessageRouter } from './useMessageRouter'
export function useAuthState(router: MessageRouter) {
const [isLoggedIn, setIsLoggedIn] = useState<boolean | null>(null)
const [isAuthenticating, setIsAuthenticating] = useState(false)
const [loginError, setLoginError] = useState<string | null>(null)
const [sessionError, setSessionError] = useState<string | null>(null)
const [userInfo, setUserInfo] = useState<{ displayName: string; username: string; avatarUrl: string } | null>(null)
router.register('session', (msg) => {
setIsLoggedIn(msg.isLoggedIn as boolean)
})
router.register('loginSuccess', (msg) => {
setIsAuthenticating(false)
setLoginError(null)
setSessionError(null)
setIsLoggedIn(true)
Eif (msg.userInfo) setUserInfo(msg.userInfo as { displayName: string; username: string; avatarUrl: string })
})
router.register('loginError', (msg) => {
setIsAuthenticating(false)
setLoginError(msg.error as string)
})
router.register('userInfo', (msg) => {
setUserInfo(msg.userInfo as { displayName: string; username: string; avatarUrl: string })
setIsLoggedIn(true)
})
router.register('loggedOut', () => {
setIsLoggedIn(false)
setUserInfo(null)
})
router.register('sessionError', (msg) => {
setIsLoggedIn(false)
setUserInfo(null)
setSessionError(msg.error as string)
})
return {
isLoggedIn,
isAuthenticating,
setIsAuthenticating,
loginError,
setLoginError,
sessionError,
setSessionError,
userInfo,
}
}
|