Interfaces
Interface: UseAuth<AuthClientType>
module react autonomy docid\ zoz6w56ubppacr0po0gls useauth useauth contains the data and methods returned by function useauth docid 2gglnt pt0lgevn528iqg type parameters name type authclienttype extends interface authclient\<t> docid\ jisgs4kxnbqannkbxhaw = any properties gettoken • gettoken () => promise < string > type declaration ▸ () promise < string > the gettoken function retrieves an authentication token for the current user session, and adds it the redux store while you are unlikely to need the token directly, since the sdk automatically appends it to relevant chord api requests, you can use this function to check whether the user's session is still active if the user's session has expired, gettoken will throw an error you may choose to call gettoken on page load or when a user visits a protected route (like an account page) to check that their session has not expired since the last time you checked here is how to use gettoken to create a route that is only accessible if the user is logged in in a next js app import { useeffect } from 'react' import { userouter } from 'next/router' import { useauth } from '@chordcommerce/react autonomy' const privateroute = ({ children }) => { const { gettoken, isloggedin, isfetching } = useauth() const router = userouter() useeffect(() => { const checkstatus = async () => { try { await gettoken() } catch (error) { router push('/login') } } if (!isloggedin) checkstatus() }, \[isloggedin, gettoken, router]) if (!isloggedin && isfetching) { // return loading animation } if (!isloggedin && !isfetching) { //return null } return children } returns promise < string > defined in packages/react autonomy/src/hooks/use auth ts 143 https //github com/chordcommerce/chord ui/blob/2b105e1/packages/react autonomy/src/hooks/use auth ts#l143 handlecallback • handlecallback () => promise < void > type declaration ▸ () promise < void > the handlecallback function completes the login process, when the user is redirected back to the site after clicking a link in their email the sdk calls this function automatically when the magic url parameter is detected on page load, so you are unlikely to need to call this function import { useeffect } from 'react' import { useauth } from '@chordcommerce/react autonomy' const page = ({ children }) => { const { handlecallback } = useauth() useeffect(() => { const handle = async () => { try { await handlecallback() } catch (error) { console error('invalid callback token ') } } handle() }, \[]) // return page } returns promise < void > defined in packages/react autonomy/src/hooks/use auth ts 174 https //github com/chordcommerce/chord ui/blob/2b105e1/packages/react autonomy/src/hooks/use auth ts#l174 isfetching • isfetching boolean a boolean indicating whether the user's authentication status is currently being fetched defined in packages/react autonomy/src/hooks/use auth ts 82 https //github com/chordcommerce/chord ui/blob/2b105e1/packages/react autonomy/src/hooks/use auth ts#l82 isloggedin • isloggedin boolean a boolean indicating whether the user is currently logged in defined in packages/react autonomy/src/hooks/use auth ts 86 https //github com/chordcommerce/chord ui/blob/2b105e1/packages/react autonomy/src/hooks/use auth ts#l86 login • login ( options type alias login docid\ slspgekom9rcvduvzv28a ) => promise < void > type declaration ▸ ( options ) promise < void > the login function should be called when a user submits a login form it will trigger an email to the user for verification import { userouter } from 'next/router' import { useauth } from '@chordcommerce/react autonomy' const loginform = () => { const { login } = useauth() const router = userouter() const onsubmit = async ({ email }) => { try { await login({ email }) router push('/some private route') } catch (error) { console error('login unsuccessful ') } } // return login form } parameters name type options type alias login docid\ slspgekom9rcvduvzv28a returns promise < void > defined in packages/react autonomy/src/hooks/use auth ts 202 https //github com/chordcommerce/chord ui/blob/2b105e1/packages/react autonomy/src/hooks/use auth ts#l202 logout • logout () => promise < void > type declaration ▸ () promise < void > the logout function terminates the user's current session import { useauth } from '@chordcommerce/react autonomy' const logoutbutton = () => { const { logout } = useauth() const handlelogout = async () => { try { await logout() } catch(error) { console error('logout unsuccessful ') } } return \<button onclick={handlelogout}>logout\</button> } returns promise < void > defined in packages/react autonomy/src/hooks/use auth ts 226 https //github com/chordcommerce/chord ui/blob/2b105e1/packages/react autonomy/src/hooks/use auth ts#l226 serviceclient • serviceclient returntype < authclienttype \[ "getserviceclient" ]> the underlying instance of the authclient provided to function chordprovider docid\ idzofwt8d2pro5pjo4xsg allows for lower level operations with the authentication provider that might be needed defined in packages/react autonomy/src/hooks/use auth ts 92 https //github com/chordcommerce/chord ui/blob/2b105e1/packages/react autonomy/src/hooks/use auth ts#l92 token • token string a string representing the user's authentication token this token is automatically appended to chord oms api requests when the user has an active session defined in packages/react autonomy/src/hooks/use auth ts 77 https //github com/chordcommerce/chord ui/blob/2b105e1/packages/react autonomy/src/hooks/use auth ts#l77