Skip to content

Instantly share code, notes, and snippets.

@Itrulia
Created March 28, 2023 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Itrulia/7ece3eacac1fd0ba7fc6b6bedb9c55fe to your computer and use it in GitHub Desktop.
Save Itrulia/7ece3eacac1fd0ba7fc6b6bedb9c55fe to your computer and use it in GitHub Desktop.
import {UserSession, User} from '@wepublish/website/api'
import {useRouter} from 'next/router'
import {createContext, Dispatch, SetStateAction, useContext} from 'react'
export const AuthTokenStorageKey = 'auth.token'
export const SessionTokenContext = createContext<
[User | null, Dispatch<SetStateAction<UserSession | null>>]
>(null!)
export const useUser = () => {
const router = useRouter()
const [user, setToken] = useContext(SessionTokenContext)
const logout = async () => {
await router.push('/')
setToken(null)
}
return [user, setToken, logout] as [User, typeof setToken, typeof logout]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment