Chord OMS
...
Next.js
SDK Reference

useAuth

8min
this page's documentation only applies to chord's next js starter prior to the release of the react docid\ ewxkrz0jhh0zpphikkcri the next js starter now uses the react sdk and does not use the methods documented on this page check out the react sdk reference for complete documentation, and the react sdk migration guide docid\ qmilhjfpz9wbjyzbe50ku for details on how to upgrade the useauth hook returns information about the current authentication session, and functions for logging in and logging out chord supports passwordless authentication with magic https //magic link/ example import { useauth } from ' /hooks/actions' const accountpage = () => { const { isloggedin } = useauth() if (!isloggedin) return // return account page } returns the useauth hook returns an object with these properties property description isloggedin a boolean indicating whether the user is currently logged in isfetching a boolean indicating whether the user's authentication status is currently being fetched token 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 gettoken a function that retrieves an authentication token for the current user session handlecallback a function that completes the login process, when the user is redirected back to the site after clicking a link in their email login a function that should be called when a user submits a login form logout a function that terminates the user's current session functions gettoken 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 arguments none returns promise\<void> a promise that resolves when the token has been added to the redux store example import { useeffect } from 'react' import { userouter } from 'next/router' import { useauth } from ' /hooks/actions' 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 } handlecallback 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 arguments none returns promise\<void> a promise that resolves when the token has been added to the redux store example import { useeffect } from 'react' import { useauth } from ' /hooks/actions' const page = ({ children }) => { const { handlecallback } = useauth() useeffect(() => { const handle = async () => { try { await handlecallback() } catch (error) { console error('invalid callback token ') } } handle() }, \[]) // return page } login the login function should be called when a user submits a login form it will trigger an email to the user for verification arguments login({ email, showui, redirecturi }) argument type description email string the user's email address showui? boolean (optional, defaults to false ) controls whether magic's default ui will display from when login is called until the user clicks the link they've received in an email redirecturi? string (optional, defaults to window\ location href ) the url that the user will be redirected to when they click the link they've received in an email returns promise\<void> example import { userouter } from 'next/router' import { useauth } from ' /hooks/actions' 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 } logout the logout function terminates the user's current session arguments none returns promise\<void> a promise that resolves when the session has been cleared example import { useauth } from ' /hooks/actions' const logoutbutton = () => { const { logout } = useauth() const handlelogout = async () => { try { await logout() } catch(error) { console error('logout unsuccessful ') } } return \<button onclick={handlelogout}>logout\</button> }