Chord OMS
...
Next.js
SDK Reference

useAuth

11min
the useauth hook returns information about the current authentication session, and functions for authentication with a shopify customer account 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 status a string indicating whether the user's authentication status is currently being fetched gettoken a function that retrieves an authentication token for the current user session login a function that should be called when a user submits a login form logout a function that terminates the user's current session recover a function that sends a shopify reset password email to the customer register a function that creates a new shopify customer and initiates an authentication session this method creates a shopify customer access token resetpassword a function that resets a shopify customer’s password with an id and token from a shopify reset password email functions gettoken the gettoken function retrieves the current shopify customer access token, and adds it the redux store while you are unlikely to need the token directly, since the sdk automatically appends it to relevant shopify 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\<string> a promise that resolves with the shopify customer access token example import { useauth } from ' /hooks/actions' const { gettoken } = useauth() const token = await gettoken() login the login function should be called when a user submits a login form it submits the email address and password for a shopify customer and initiates an authentication session this method creates a shopify customer access token arguments login({ email, password }) argument type description options email string the user's email address options password string the user's password returns promise\<void> a promise that resolves when the user has been logged in example import { useauth } from ' /hooks/actions' const { login } = useauth() await login({ email 'test\@test com', password '123' }) logout() the logout function terminates the user's current session this method destroys the current shopify customer access token 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> } recover the recover function sends a shopify reset password email to the customer arguments recover(email) argument type description email string the user's email address returns promise\<void> a promise that resolves when the reset password email has been sent example import { useauth } from ' /hooks/actions' const { recover } = useauth() await recover('test\@test com') register the register function creates a new shopify customer and initiates an authentication session this also creates a shopify customer access token arguments register({ email, password }) argument type description options email string the user's email address options password string the user's password returns promise\<void> a promise that resolves when the user has been registered and logged in example import { useauth } from ' /hooks/actions' const { register } = useauth() await register({ email 'test\@test com', password '123' }) resetpassword the resetpassword function resets a shopify customer’s password with an id and token from a shopify reset password email arguments resetpassword({ id, password, token }) argument type description options id string the password reset id from the shopify reset password email options password string the user's new password options token string the password reset token from the shopify reset password email returns promise\<void> a promise that resolves when the user has been logged in example import { useauth } from ' /hooks/actions' const { login } = useauth() await resetpassword({ id 'id', password '456', token '123' })