Skip to content

storeUser ​

Overview ​

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

Signature ​

storeUser(pinia, hot)

Parameters ​

NameTypeRequiredDescription
piniaPinianoPinia instance to retrieve the store
hotStoreGenericnodev 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 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 ​

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 ​

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 ​

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 ​

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 ​

Whether error holds an error from the last failed request.

Type: boolean

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

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 ​

Whether a request made by this store is in flight.

Type: boolean

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

loggedIn ​

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

Type: boolean

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

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 ​

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