Chord OMS
...
SDK Reference
useAuth
11 min
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 141,602 trueleft unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type 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 }) trueleft unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type 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) trueleft unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type 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 }) trueleft unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type 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 }) trueleft unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type 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' })