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

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:

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:

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:

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:

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:

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:



Updated 03 Mar 2023
Did this page help you?