---
title: useAuth
slug: oms/CDrD-useauth
docTags: 
createdAt: 2022-11-08T14:20:24.000Z
---

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

## Example

::::CodeDrawer{title="useAuth" isResponseExpanded="false" autoGeneratedAnchorSlug="useauth" legacyHash="Sx41UKuiwg296yfu19Wpz"}
:::CodeblockTabsExamples
```javascript
import { useAuth } from '~/hooks/actions'

const AccountPage = () => {
  const { isLoggedIn } = useAuth()
  if (!isLoggedIn) return
  
  // return account page
}
```
:::

:::CodeblockTabsResponses
```javascript
```
:::
::::

## 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&#x20;

### getToken&#x20;

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

::::CodeDrawer{title="getToken" isResponseExpanded="false" autoGeneratedAnchorSlug="gettoken-1" legacyHash="7_NCdBFWhmuteARa_If3v"}
:::CodeblockTabsExamples
```javascript
import { useAuth } from '~/hooks/actions'

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

:::CodeblockTabsResponses
```javascript
```
:::
::::

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

::::CodeDrawer{title="login" isResponseExpanded="false" autoGeneratedAnchorSlug="login-1" legacyHash="xnMsEN4Tb7zm9mmgETZeN"}
:::CodeblockTabsExamples
```javascript
import { useAuth } from '~/hooks/actions'

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

:::CodeblockTabsResponses
```javascript
```
:::
::::

### logout()&#x20;

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

::::CodeDrawer{title="logout" isResponseExpanded="false" autoGeneratedAnchorSlug="logout-1" legacyHash="hPvOid3maeN3ktGHBpaIF"}
:::CodeblockTabsExamples
```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>
}
```
:::

:::CodeblockTabsResponses
```javascript
```
:::
::::

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

::::CodeDrawer{title="recover" isResponseExpanded="false" autoGeneratedAnchorSlug="recover-1" legacyHash="VYv4nVqOXEBH0-tAFgyOz"}
:::CodeblockTabsExamples
```javascript
import { useAuth } from '~/hooks/actions'

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

:::CodeblockTabsResponses
```javascript
```
:::
::::

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

::::CodeDrawer{title="register" isResponseExpanded="false" autoGeneratedAnchorSlug="register-1" legacyHash="keqOWj3VogFy7oa1dbFVL"}
:::CodeblockTabsExamples
```javascript
import { useAuth } from '~/hooks/actions'

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

:::CodeblockTabsResponses
```javascript
```
:::
::::

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

::::CodeDrawer{title="resetPassword" isResponseExpanded="false" autoGeneratedAnchorSlug="resetpassword-1" legacyHash="025VLId4JXdLJGnoOWymG"}
:::CodeblockTabsExamples
```javascript
import { useAuth } from '~/hooks/actions'

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

:::CodeblockTabsResponses
```javascript
```
:::
::::
