Skip to main content

React - useCryptr Hooks

Purpose

Sometimes you may want to not use our Components to retrieve some data or make some actions. This is why we included useCryptr context with included hooks that we will describe below.

How to use

caution

You still need to be in an sub-component child of our CryptrProvider to implement useCryptr and related hooks

You need to import useCryptr from our library and call hooks as below

SomeComponent.tsx
import React from 'react'
// ...
import { useCryptr } from '@cryptr/cryptr-react'

const SomeComponent = () => {
const { isAuthenticated } = useCryptr()

return (
<div>
<span>
{isAuthenticated() ? 'you are logged in': 'no active session'}
</span>
</div>
)
}

export default SomeComponent

List of available Hooks

Active session : isAuthenticated

isAuthenticated

Function that will retrieve is an active session is live

Current user : user

user()

Function that will retrieve current id token claims

Delete active session: logOut

logOut()

Function that will destroy the current session and will perform after the onLogOutCallback provided in CryptrProvider config object.

Create new session for existing user: signinWithRedirect

signinWithRedirect(scope?: string, locale?: string, redirectUri?: string)

Function that will start the signin Cryptr process to start a fresh new session for an existing registered user. Default values of function parameters are ones present in your configuration locale is usefull if you know at the moment your end-user prefered locale !

Create new session for new user: signupWithRedirect

signupWithRedirect(scope?: string, locale?: string, redirectUri?: string)

Function that will start the signin Cryptr process to start a new session for non-registered user. Default values of function parameters are ones present in your configuration locale is usefull if you know at the moment your end-user prefered locale !

Trigger access to User account page : userAccountAccess

userAccountAccess()

When this function is call it will reproduce the action behin Account Access Button

List your default scopes from your config : defaultScopes

defaultScopes()

If at any time you want to known your defaultScopes from your configuration.

Decorate requests : decoratedRequest

decoratedRequest(axiosConfig)

Axios Promise that will automatically decorate your request with the proper Authorization header.

You should use this hook for your resource server requests

note

This hooks uses axios NPM library. You can find more about axios and axiosConfig here