Skip to main content

Module: core/auth

References

Auth

Re-exports Auth

Functions

AuthProvider

AuthProvider(__namedParameters): Element

Stores authentication data for a user and provides useful methods to interact with it.

example

<ProjectProvider>
<AuthProvider>
<App/>
</AuthProvider>
</ProjectProvider>
@param props
@param props.children Your app

Parameters

NameType
__namedParametersObject
__namedParameters.childrenReactElement<any, string | JSXElementConstructor<any>>

Returns

Element


atob

atob(input?): string

React Native compatible base64 decoder

example

const decoded = atob('YXRvYg')

Parameters

NameTypeDefault valueDescription
inputstring''Any valid base64 string, padding with = is optional.

Returns

string


evaluateToken

evaluateToken(token): Object

Calculates parameters that can be used to consider refreshing the auth token.

Parameters

NameType
tokenstring

Returns

Object

NameType
agenumber
expiresInnumber
isExpiredboolean
periodnumber

unsafeDecode

unsafeDecode(token): any

Decodes a JWT without verifying if it's valid. Only makes sense when checking out your own JWT client-side.

example

SystemUI.setBackgroundColorAsync("white");

Parameters

NameType
tokenstring

Returns

any


useAsyncToken

useAsyncToken(): () => Promise<string>

Callback to get a fresh token for the currently signed in user. Will throw if the user is not signed in. Will return undefined if it failed to refresh the token. This behaviour may change to throw if it failed to fetch a fresh token.

example

const token = useAsyncToken()
const submit = async () =>
fetch('/api', { headers: { Authorization: 'Bearer ' + (await token()) } })

Returns

fn

▸ (): Promise<string>

Returns

Promise<string>


useAuth

useAuth(): AuthContextType

Returns

AuthContextType


useAuthActions

useAuthActions(): AuthActions

Returns

AuthActions


useAuthContext

useAuthContext(): AuthDataContextType

Gets the authentication context

example

const context = useAuthContext()

Returns

AuthDataContextType


useAuthState

useAuthState(): undefined | null | AuthState

Gets the authentication data of the currently signed in user Rerenders only when the token changes

example

const data = useAuthState()

Returns

undefined | null | AuthState


useBearer

useBearer(): () => Promise<null | { Authorization: string }>

Callback to get a fresh Authorization header for the currently signed in user. Will return null if the user is not signed in.

example

const bearer = useBearer()
const submit = async () =>
fetch('/api', { headers: await bearer() } })

Returns

fn

▸ (): Promise<null | { Authorization: string }>

Returns

Promise<null | { Authorization: string }>


useHandleAuthenticators

useHandleAuthenticators(): (__namedParameters: { authenticators?: Authenticator[] ; mfa_token?: string ; original?: { mfa_token?: string } }) => Promise<void | [any, any]>

Handles the Auth0 response in case of multi-factor authentication

deprecated Maintenance update required!

example

SystemUI.setBackgroundColorAsync("white");

Returns

fn

▸ (__namedParameters): Promise<void | [any, any]>

Parameters
NameTypeDescription
__namedParametersObject-
__namedParameters.authenticators?Authenticator[]-
__namedParameters.mfa_token?stringBased on useAuth
__namedParameters.original?ObjectBased on sign in API response
__namedParameters.original.mfa_token?string-
Returns

Promise<void | [any, any]>


useLiveAuthState

useLiveAuthState(): undefined | null | AuthState

Gets the authentication data of the currently signed in user. Rerenders every X (default 3) seconds and keeps the token fresh. Not recommended for common usecases, use useAsyncToken() instead.

example

const { access_token } = useLiveAuthState()
console.log('Token will expire in', formatUntil(decode(access_token).exp))

Returns

undefined | null | AuthState


useToken

useToken(): undefined | string

Gets the active id token for the currently signed in user. The token may be expired.

example

SystemUI.setBackgroundColorAsync("white");

Returns

undefined | string


useUser

useUser(): DecodedJWT | null

Get the decoded information from the JWT auth token.

example

const decoded = useUser()
log('My id is', decoded?.sub)

Returns

DecodedJWT | null


useUserRole

useUserRole(): "" | "user" | "moderator" | "owner"

Gets the default role of the user

deprecated Roles are now at member level and not embedded in the JWT

Returns

"" | "user" | "moderator" | "owner"