-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-auth.tsx
30 lines (29 loc) · 1.04 KB
/
use-auth.tsx
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
import { IDToken } from '@crossid/crossid-spa-js'
import { useContext } from 'react'
import AuthContext, { AuthContextType } from './provider-context'
/**
* useAuth is a hook to handle authentication.
*
* T can optionally be set to your custom `IDToken` type.
*
* ```ts
* const {
* // true if hook is currently loading, can be used to defer loading page with a spinner.
* loading: boolean
* // An error that has occured during authentication phases.
* error?: Error
* // The ID token if the authenticated user.
* idToken?: T
* // a method to start login by redirecting browser to crossid authorization provider.
* loginWithRedirect,
* // a method to start logout by redirecting browser to crossid authorization provider.
* logoutWithRedirect,
* // returns a promise which eventually returns the access token of the authenticated user.
* getAccessToken
* } = useAuth()
* ```
*
* @returns
*/
export const useAuth = <T extends IDToken = IDToken>(): AuthContextType<T> =>
useContext<AuthContextType<T>>(AuthContext)