> For the complete documentation index, see [llms.txt](https://docs.chord.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.chord.co/chord-oms/shopify/developer-tools/next-js/sdk-reference/useauth.md).

# useAuth

The `useAuth` hook returns information about the current authentication session, and functions for authentication with a Shopify customer account.

## Example

{% code title="useAuth" %}

```javascript
import { useAuth } from '~/hooks/actions'

const AccountPage = () => {
  const { isLoggedIn } = useAuth()
  if (!isLoggedIn) return

  // return account page
}
```

{% endcode %}

## 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:**

{% code title="getToken" %}

```javascript
import { useAuth } from '~/hooks/actions'

const { getToken } = useAuth()
const token = await getToken()
```

{% endcode %}

### 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:**

{% code title="login" %}

```javascript
import { useAuth } from '~/hooks/actions'

const { login } = useAuth()
await login({ email: 'test@test.com', password: '123' })
```

{% endcode %}

### 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:**

{% code title="logout" %}

```javascript
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>
}
```

{% endcode %}

### 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:**

{% code title="recover" %}

```javascript
import { useAuth } from '~/hooks/actions'

const { recover } = useAuth()
await recover('test@test.com')
```

{% endcode %}

### 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:**

{% code title="register" %}

```javascript
import { useAuth } from '~/hooks/actions'

const { register } = useAuth()
await register({ email: 'test@test.com', password: '123' })
```

{% endcode %}

### 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:**

{% code title="resetPassword" %}

```javascript
import { useAuth } from '~/hooks/actions'

const { login } = useAuth()
await resetPassword({
  id: 'id',
  password: '456',
  token: '123'
})
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.chord.co/chord-oms/shopify/developer-tools/next-js/sdk-reference/useauth.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
