---
url: "https://vueda.dev/v3/reference/api/js/stores/storeUser/functions/storeUser.md"
title: "storeUser"
---

# storeUser

## Overview

The user store, handling user login, logout, and current user fetching.

## Signature

`storeUser(pinia, hot)`

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| <span id="param-pinia">pinia</span> | `Pinia` | no | Pinia instance to retrieve the store |
| <span id="param-hot">hot</span> | `StoreGeneric` | no | dev only hot module replacement |

### Returns

`Store`

## Examples

```js
  import { storeUser } from "@vueda";
  const user = storeUser();

  user.loggedIn; // reactive boolean for login state, true if logged-in
  user.loggedInUser; // reactive object for user details
  user.currentUserRequestStatus.loading; // undefined before the first request, true while one runs, then false
  user.currentUserRequestStatus.error; // error from the last current-user request, null when it succeeded
  user.currentUserRequestStatus.errored; // true when the last current-user request failed
  user.loading; // reactive boolean for loading state, true if loading
  user.error; // reactive object for error details
  user.errored; // reactive boolean for error state, true if errored

  user.login({username: "username", password: "password"}); // login
  user.forgotPassword({email: "password"}); /
  user.resetPassword({password: "password", password_confirm: "password_confirm"});
  user.checkResetLinkIsValid(); // login
  user.logout(); // logout
  user.fetchCurrentUser(); // fetch the current user
```

## Properties

### $id {#id}

Id of the store. Used by map helpers.

Type: `"user"`

Source: `node_modules/.pnpm/pinia@3.0.4_typescript@5.9.3_vue@3.5.43_typescript@5.9.3_/node_modules/pinia/dist/pinia.d.ts:654`

### authPendingFlow {#authPendingFlow}

The allauth flow the user must complete next, or `null` when none is pending.

Each who-is response sets it from `auth_pending_flow`, including the refetch that follows a 401
response. For an anonymous session it is the stage a sign-in waits at, such as
`mfa_authenticate`, so a reload mid-sign-in resumes the two-factor step. For a signed-in user it is
`mfa_reauthenticate` when they must confirm a second factor, `reauthenticate` when they must confirm
their password, and `null` while their session is recent. A signed-in user with no pending flow has
recently authenticated, which is what the `requireRecentAuth` guard checks.
The reauthentication view renders the input for the flow and calls `twoFactorReauthenticate` or
`reauthenticate` to complete it.

Type: `string`

Source: `client/lib/stores/storeUser.js:184`

### currentUserRequest {#currentUserRequest}

The promise of the newest current-user request, so callers can wait for a request in flight instead of
starting another. It settles with the newest request's outcome. The value is `null` before the
first request.

Type: `Promise`

Source: `client/lib/stores/storeUser.js:150`

### currentUserRequestStatus {#currentUserRequestStatus}

The status of the newest current-user request, which `fetchCurrentUser` sends to the who-is endpoint.
It has the `useLoadingError` shape from reactive-helpers.
Only `fetchCurrentUser` writes it, so the other actions never change it.

- `loading` is `undefined` before the first request, `true` while a request runs, and `false` once
  it finishes.
- `error` holds the error from the finished request. It is `null` when the request succeeded.
- `errored` is `true` when the finished request failed.

The last current-user request succeeded when `loading` is `false` and `errored` is `false`.

Type: `object`

Source: `client/lib/stores/storeUser.js:138`

### error {#error}

The error from the last failed request. The value is `null` when the last request succeeded or
was cleared.

Type: `Error`

Source: `client/lib/stores/storeUser.js:163`

### errored {#errored}

Whether `error` holds an error from the last failed request.

Type: `boolean`

Source: `client/lib/stores/storeUser.js:169`

### identityGeneration {#identityGeneration}

Incremented once per change of authenticated user, after the authorization-dependent
caches have been dropped. Watch it to rebuild anything derived from those caches.

Type: `number`

Source: `client/lib/stores/storeUser.js:199`

### loading {#loading}

Whether a request made by this store is in flight.

Type: `boolean`

Source: `client/lib/stores/storeUser.js:156`

### loggedIn {#loggedIn}

Whether the last who-is response named an authenticated user.

Type: `boolean`

Source: `client/lib/stores/storeUser.js:115`

### loggedInUser {#loggedInUser}

The last who-is response: the user's `id`, `email`, `name`, `totp_devices`, and `auth_pending_flow`.
An empty object before the first response; an anonymous response carries no `id`, only
`auth_pending_flow`.

Type: `object`

Source: `client/lib/stores/storeUser.js:123`

### principalId {#principalId}

The id of the authenticated user, `null` while nobody is authenticated, and `undefined`
until the first who-is response has been seen. The `undefined` sentinel is what keeps the
first who-is from counting as a change of user.

Type: `string | number`

Source: `client/lib/stores/storeUser.js:192`

## Methods

- [_performLogout](/v3/reference/api/js/stores/storeUser/methods/_performLogout.md) - Sends the logout request, then refetches the current user.
- [_refetchUserOnUnauthorized](/v3/reference/api/js/stores/storeUser/methods/_refetchUserOnUnauthorized.md) - Refetches the current user when `error` is an `UnauthorizedError`, so `authPendingFlow` names the flow that the server now requires.
- [activateTOTPDevice](/v3/reference/api/js/stores/storeUser/methods/activateTOTPDevice.md) - Activates the device that `setupTOTPDevice` started, then refetches the current user to pick up its devices.
- [changePassword](/v3/reference/api/js/stores/storeUser/methods/changePassword.md) - Changes the signed-in user's password.
- [checkResetLinkIsValid](/v3/reference/api/js/stores/storeUser/methods/checkResetLinkIsValid.md) - Checks with the server whether a password reset link is still valid.
- [clearError](/v3/reference/api/js/stores/storeUser/methods/clearError.md) - Clears `error` and `errored`.
- [fetchCurrentUser](/v3/reference/api/js/stores/storeUser/methods/fetchCurrentUser.md) - Fetches the current user from the who-is endpoint and updates the login state and `currentUserRequestStatus`.
- [forgotPassword](/v3/reference/api/js/stores/storeUser/methods/forgotPassword.md) - Asks the server to email a password reset link.
- [generateRecoveryCode](/v3/reference/api/js/stores/storeUser/methods/generateRecoveryCode.md) - Generates a new set of recovery codes for the signed-in user, replacing any existing set.
- [getRecoveryCodes](/v3/reference/api/js/stores/storeUser/methods/getRecoveryCodes.md) - Fetches the signed-in user's recovery codes, and generates them when the user has none.
- [getTwoFactorAuthMethod](/v3/reference/api/js/stores/storeUser/methods/getTwoFactorAuthMethod.md) - Fetches the two-factor methods of the user who is signing in or reauthenticating.
- [login](/v3/reference/api/js/stores/storeUser/methods/login.md) - Signs the user in, then refetches the current user.
- [logout](/v3/reference/api/js/stores/storeUser/methods/logout.md) - Signs the user out, then refetches the current user.
- [reauthenticate](/v3/reference/api/js/stores/storeUser/methods/reauthenticate.md) - Confirms the signed-in user's password again, then refetches the current user, which clears `authPendingFlow`.
- [resetPassword](/v3/reference/api/js/stores/storeUser/methods/resetPassword.md) - Sets a new password using the `pk` and `token` from a password reset link.
- [sendTwoFactorAuthenticationCode](/v3/reference/api/js/stores/storeUser/methods/sendTwoFactorAuthenticationCode.md) - Asks the server to send a two-factor code to the device of the user who is signing in or reauthenticating, for the given method.
- [setupTOTPDevice](/v3/reference/api/js/stores/storeUser/methods/setupTOTPDevice.md) - Starts setting up a two-factor device for the signed-in user.
- [twoFactorAuthenticate](/v3/reference/api/js/stores/storeUser/methods/twoFactorAuthenticate.md) - Completes a two-factor sign-in with a code, then refetches the current user, which clears `authPendingFlow`.
- [twoFactorReauthenticate](/v3/reference/api/js/stores/storeUser/methods/twoFactorReauthenticate.md) - Confirms the signed-in user's second factor with a code, then refetches the current user, which clears `authPendingFlow`.

## Source

`client/lib/stores/storeUser.js:108`
