> ## Documentation Index
> Fetch the complete documentation index at: https://docs.base44.com/llms.txt
> Use this file to discover all available pages before exploring further.

# auth

***

## Overview

Authentication module for managing user authentication and authorization. The module automatically stores tokens in local storage when available and manages authorization headers for API requests.

### Features

This module provides comprehensive authentication functionality including:

* Email/password login and registration
* Token management
* User profile access and updates
* Password reset flows
* OTP verification
* User invitations

### Authentication Modes

The auth module is only available in user authentication mode (`base44.auth`).

## Methods

### me()

> **me**(): `Promise`\<`User`>

Gets the current authenticated user's information.

#### Returns

`User`

Promise resolving to the user's profile data.

> [accordion] Properties
>
> **id** `string` *required*
>
> Unique user identifier.
>
> **created\_date** `string` *required*
>
> When the user was created.
>
> **updated\_date** `string` *required*
>
> When the user was last updated.
>
> **email** `string` *required*
>
> User's email address.
>
> **full\_name** `string | null` *required*
>
> User's full name.
>
> **disabled** `boolean | null` *required*
>
> Whether the user is disabled.
>
> **is\_verified** `boolean` *required*
>
> Whether the user's email has been verified.
>
> **app\_id** `string` *required*
>
> The app ID this user belongs to.
>
> **is\_service** `boolean` *required*
>
> Whether this is a service account.
>
> **role** `string` *required*
>
> User's role in the app. Roles are configured in the app settings and determine the user's permissions and access levels.
>
> **\[key: string]** `any`
>
> Additional custom fields defined in the user schema. Any custom properties added to the user schema in the app will be available here with their configured types and values.

#### Example

```typescript Get current user information theme={null}
const user = await base44.auth.me();
console.log(`Logged in as: ${user.email}`);
console.log(`User ID: ${user.id}`);
```

***

### updateMe()

> **updateMe**(`data`): `Promise`\<`User`>

Updates the current authenticated user's information.

You can update any [custom fields](https://docs.base44.com/developers/backend/resources/entities/user-schema.md#custom-fields)
defined in your User entity schema.

[Field-level security](https://docs.base44.com/developers/backend/resources/entities/security.md#field-level-security-fls)
`write` rules on custom User fields also apply to this method. If `data` includes a field
the user isn't allowed to write, the call throws a permission error and no fields are
updated.

Updating `role` requires editor access on the app.

> [!NOTE]
>
> These fields can't be changed with this method:
> `id`, `email`, `full_name`, `created_date`, `updated_date`, `created_by`,
> and `collaborator_role`.

#### Parameters

**data** `Record\<string, any\>` *required*

Object containing the fields to update.

#### Returns

`User`

Promise resolving to the updated user data.

> [accordion] Properties
>
> **id** `string` *required*
>
> Unique user identifier.
>
> **created\_date** `string` *required*
>
> When the user was created.
>
> **updated\_date** `string` *required*
>
> When the user was last updated.
>
> **email** `string` *required*
>
> User's email address.
>
> **full\_name** `string | null` *required*
>
> User's full name.
>
> **disabled** `boolean | null` *required*
>
> Whether the user is disabled.
>
> **is\_verified** `boolean` *required*
>
> Whether the user's email has been verified.
>
> **app\_id** `string` *required*
>
> The app ID this user belongs to.
>
> **is\_service** `boolean` *required*
>
> Whether this is a service account.
>
> **role** `string` *required*
>
> User's role in the app. Roles are configured in the app settings and determine the user's permissions and access levels.
>
> **\[key: string]** `any`
>
> Additional custom fields defined in the user schema. Any custom properties added to the user schema in the app will be available here with their configured types and values.

#### Example

```typescript Update custom fields defined in your User entity theme={null}
await base44.auth.updateMe({
  bio: 'Software developer',
  preferences: { theme: 'dark' }
});
```

***

### redirectToLogin()

> **redirectToLogin**(`nextUrl`): `void`

Redirects the user to the app's login page.

Redirects with a callback URL to return to after successful authentication. Requires a browser environment and can't be used in the backend.

#### Parameters

**nextUrl** `string` *required*

URL to redirect to after successful login.

#### Returns

`void`

#### Throws

When not in a browser environment.

#### Examples

```typescript Redirect to login and come back to current page theme={null}
base44.auth.redirectToLogin(window.location.href);
```

```typescript Redirect to login and then go to the dashboard page theme={null}
base44.auth.redirectToLogin('/dashboard');
```

***

### loginWithProvider()

> **loginWithProvider**(`provider`, `fromUrl?`): `void`

Redirects the user to a third-party authentication provider's login page.

Initiates an OAuth login flow with one of the built-in providers. Requires a browser environment and can't be used in the backend.

Supported providers:

* `'google'`: [Google OAuth](https://developers.google.com/identity/protocols/oauth2). Enabled by default.
* `'microsoft'`: [Microsoft OAuth](https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow). Enable Microsoft in your app's authentication settings before specifying this provider.
* `'facebook'`: [Facebook Login](https://developers.facebook.com/docs/facebook-login). Enable Facebook in your app's authentication settings before using.
* `'apple'`: [Sign in with Apple](https://developer.apple.com/sign-in-with-apple/). Enable Apple in your app's authentication settings before using this provider.
* `'sso'`: Enterprise SSO. [Set up an SSO provider](https://docs.base44.com/Setting-up-your-app/Setting-up-SSO.md) in your app's authentication settings before using this provider.

#### Parameters

> [accordion] Properties
>
> **provider** `string` *required*
>
> The authentication provider to use: `'google'`, `'microsoft'`, `'facebook'`, `'apple'`, or `'sso'`.
>
> **fromUrl** `string`
>
> URL to redirect to after successful authentication. Defaults to `'/'`.

#### Returns

`void`

#### Examples

```typescript Google theme={null}
base44.auth.loginWithProvider('google', window.location.pathname);
```

```typescript Microsoft theme={null}
base44.auth.loginWithProvider('microsoft', '/dashboard');
```

```typescript Apple theme={null}
base44.auth.loginWithProvider('apple', '/dashboard');
```

```typescript SSO theme={null}
base44.auth.loginWithProvider('sso', '/dashboard');
```

***

### logout()

> **logout**(`redirectUrl?`): `void`

Logs out the current user.

Removes the authentication token from local storage and Axios headers, then optionally redirects to a URL or reloads the page. Requires a browser environment and can't be used in the backend.

#### Parameters

**redirectUrl** `string`

Optional URL to redirect to after logout. Reloads the page if not provided.

#### Returns

`void`

#### Examples

```typescript Logout and reload page theme={null}
base44.auth.logout();
```

```typescript Logout and redirect to login page theme={null}
base44.auth.logout('/login');
```

```typescript Logout and redirect to home theme={null}
base44.auth.logout('/');
```

***

### setToken()

> **setToken**(`token`, `saveToStorage?`): `void`

Sets the authentication token.

Updates the authorization header for API requests and optionally saves the token to local storage for persistence. Saving to local storage requires a browser environment and is automatically skipped in backend environments.

#### Parameters

> [accordion] Properties
>
> **token** `string` *required*
>
> JWT authentication token.
>
> **saveToStorage** `boolean`
>
> Whether to save the token to local storage. Defaults to true.

#### Returns

`void`

#### Examples

```typescript Set token and save to local storage theme={null}
base44.auth.setToken('eyJhbGciOiJIUzI1NiIs...');
```

```typescript Set token without saving to local storage theme={null}
base44.auth.setToken('eyJhbGciOiJIUzI1NiIs...', false);
```

***

### loginViaEmailPassword()

> **loginViaEmailPassword**(\
> `email`,\
> `password`,\
> `turnstileToken?`\
> ): `Promise`\<`LoginResponse`>

Logs in a registered user using email and password.

Authenticates a user with email and password credentials. The user must already have a registered account. For new users, use [`register()`](#register) first to create an account. On successful login, automatically sets the token for subsequent requests.

#### Parameters

> [accordion] Properties
>
> **email** `string` *required*
>
> User's email address.
>
> **password** `string` *required*
>
> User's password.
>
> **turnstileToken** `string`
>
> Has no effect. Base44 doesn't verify Turnstile tokens for app users.

#### Returns

`LoginResponse`

Promise resolving to login response with access token and user data.

> [accordion] Properties
>
> **access\_token** `string` *required*
>
> JWT access token for authentication.
>
> **user** `User` *required*
>
> User information.
>
> > [accordion] Properties
> >
> > **id** `string` *required*
> >
> > Unique user identifier.
> >
> > **created\_date** `string` *required*
> >
> > When the user was created.
> >
> > **updated\_date** `string` *required*
> >
> > When the user was last updated.
> >
> > **email** `string` *required*
> >
> > User's email address.
> >
> > **full\_name** `string | null` *required*
> >
> > User's full name.
> >
> > **disabled** `boolean | null` *required*
> >
> > Whether the user is disabled.
> >
> > **is\_verified** `boolean` *required*
> >
> > Whether the user's email has been verified.
> >
> > **app\_id** `string` *required*
> >
> > The app ID this user belongs to.
> >
> > **is\_service** `boolean` *required*
> >
> > Whether this is a service account.
> >
> > **role** `string` *required*
> >
> > User's role in the app. Roles are configured in the app settings and determine the user's permissions and access levels.

#### Throws

Error if the email and password combination is invalid or the user is not registered.

#### Examples

```typescript Login with email and password theme={null}
try {
  const { access_token, user } = await base44.auth.loginViaEmailPassword(
    'user@example.com',
    'securePassword123'
  );
  console.log('Login successful!', user);
} catch (error) {
  console.error('Login failed:', error);
}
```

```typescript With captcha token theme={null}
const response = await base44.auth.loginViaEmailPassword(
  'user@example.com',
  'securePassword123',
  'captcha-token-here'
);
```

***

### isAuthenticated()

> **isAuthenticated**(): `Promise`\<`boolean`>

Checks if the current user is authenticated.

#### Returns

`Promise<boolean>`

Promise resolving to true if authenticated, false otherwise.

#### Example

```typescript Check authentication status theme={null}
const isAuthenticated = await base44.auth.isAuthenticated();
if (isAuthenticated) {
  console.log('User is logged in');
} else {
  // Redirect to login page
  base44.auth.redirectToLogin(window.location.href);
}
```

***

### hasToken()

> **hasToken**(): `boolean`

Checks whether an access token is currently set on the client.

Reports only that a token is present, not that it's valid: an expired or revoked token still returns `true`. Use it to skip requests that can't succeed without a session, such as [`me()`](#me) for an anonymous visitor, and use [`isAuthenticated()`](#isauthenticated) to confirm a session with the server. The token is the one passed to [`createClient()`](../functions/createClient) or set with [`setToken()`](#settoken), and [`logout()`](#logout) clears it. A session held only in an HTTP-only cookie isn't visible to the client and returns `false`.

#### Returns

`boolean`

#### Example

```typescript Skip the user lookup for anonymous visitors theme={null}
const user = base44.auth.hasToken() ? await base44.auth.me() : null;
```

***

### inviteUser()

> **inviteUser**(`userEmail`, `role`): `Promise`\<`any`>

Invites a user to the app.

Sends an invitation email to a potential user with a specific role.
Roles are configured in the app settings and determine
the user's permissions and access levels.

#### Parameters

> [accordion] Properties
>
> **userEmail** `string` *required*
>
> Email address of the user to invite.
>
> **role** `string` *required*
>
> Role to assign to the invited user. Must match a role defined in the app. For example, `'admin'` or `'user'`.

#### Returns

`Promise<any>`

Promise that resolves when the invitation is sent successfully. Throws an error if the invitation fails.

#### Example

```typescript try { theme={null}
  await base44.auth.inviteUser('newuser@example.com', 'user');
  console.log('Invitation sent successfully!');
} catch (error) {
  console.error('Failed to send invitation:', error);
}
```

***

### register()

> **register**(`params`): `Promise`\<`any`>

Registers a new user account.

Creates a new user account with email and password. Registration sends an OTP
code to the user's email. Pass that code to
[`verifyOtp()`](#verifyotp) to complete verification, then log the user
in with [`loginViaEmailPassword()`](#loginviaemailpassword).

#### Parameters

**params** `RegisterParams` *required*

Registration details including email, password, and optional fields.

> [accordion] Properties
>
> **email** `string` *required*
>
> User's email address.
>
> **password** `string` *required*
>
> User's password.
>
> **turnstile\_token** `string | null`
>
> Has no effect. Base44 doesn't verify Turnstile tokens for app users.
>
> **referral\_code** `string | null`
>
> Optional referral code from an existing user.

#### Returns

`Promise<any>`

Promise resolving to the registration response.

#### Example

```typescript Register a new user theme={null}
await base44.auth.register({
  email: 'newuser@example.com',
  password: 'securePassword123',
  referral_code: 'FRIEND2024'
});

// Verify with the OTP code from the user's email
await base44.auth.verifyOtp({
  email: 'newuser@example.com',
  otpCode: '123456'
});

// Log the user in after verification
const { access_token, user } = await base44.auth.loginViaEmailPassword(
  'newuser@example.com',
  'securePassword123'
);
```

***

### verifyOtp()

> **verifyOtp**(`params`): `Promise`\<`any`>

Verifies an OTP (one-time password) code.

Confirms that the user owns the email address by checking the code sent to
their inbox during [`register()`](#register). After a successful
call, log the user in with
[`loginViaEmailPassword()`](#loginviaemailpassword). If the code
has expired or the user didn't receive it, send a fresh one with
[`resendOtp()`](#resendotp).

#### Parameters

**params** `VerifyOtpParams` *required*

The email being verified and the OTP code the user entered.

> [accordion] Properties
>
> **email** `string` *required*
>
> User's email address.
>
> **otpCode** `string` *required*
>
> One-time password code received by email.

#### Returns

`Promise<any>`

Promise resolving to the verification response, which includes an
access token for the now-verified user.

#### Throws

Error if the OTP code is invalid or expired.

#### Examples

```typescript Verify the code the user entered from their email theme={null}
try {
  await base44.auth.verifyOtp({
    email: 'user@example.com',
    otpCode: '123456'
  });
  console.log('Email verified successfully!');
} catch (error) {
  console.error('Invalid or expired OTP code');
}
```

```typescript Full registration flow theme={null}
await base44.auth.register({
  email: 'newuser@example.com',
  password: 'securePassword123'
});

// The user receives an OTP code by email. Collect it and verify.
await base44.auth.verifyOtp({
  email: 'newuser@example.com',
  otpCode: '123456'
});

// Log the user in after verification
const { access_token, user } = await base44.auth.loginViaEmailPassword(
  'newuser@example.com',
  'securePassword123'
);
```

***

### resendOtp()

> **resendOtp**(`email`): `Promise`\<`any`>

Resends an OTP code to the user's email address.

Call this when the user didn't receive the original code sent by
[`register()`](#register), or when the previous code has expired.
The new code replaces the previous one. Pass it to
[`verifyOtp()`](#verifyotp) to complete verification.

#### Parameters

**email** `string` *required*

Email address to send the new OTP to.

#### Returns

`Promise<any>`

Promise resolving once the new OTP has been sent, with a
confirmation message and the new code's expiration window.

#### Throws

Error if the email is invalid or the request fails.

#### Example

```typescript Resend the OTP when the user didn't receive the original theme={null}
try {
  await base44.auth.resendOtp('user@example.com');
  console.log('OTP resent! Please check your email.');
} catch (error) {
  console.error('Failed to resend OTP:', error);
}
```

***

### resetPasswordRequest()

> **resetPasswordRequest**(`email`): `Promise`\<`any`>

Requests a password reset.

Sends a password reset email to the specified email address.

#### Parameters

**email** `string` *required*

Email address for the account to reset.

#### Returns

`Promise<any>`

Promise resolving when the password reset email is sent successfully.

#### Throws

Error if the email is invalid or the request fails.

#### Example

```typescript try { theme={null}
  await base44.auth.resetPasswordRequest('user@example.com');
  console.log('Password reset email sent!');
} catch (error) {
  console.error('Failed to send password reset email:', error);
}
```

***

### resetPassword()

> **resetPassword**(`params`): `Promise`\<`any`>

Resets password using a reset token.

Completes the password reset flow by setting a new password
using the token received by email.

#### Parameters

**params** `ResetPasswordParams` *required*

Object containing the reset token and new password.

> [accordion] Properties
>
> **resetToken** `string` *required*
>
> Reset token received by email.
>
> **newPassword** `string` *required*
>
> New password to set.

#### Returns

`Promise<any>`

Promise resolving when the password is reset successfully.

#### Throws

Error if the reset token is invalid, expired, or the request fails.

#### Example

```typescript try { theme={null}
  await base44.auth.resetPassword({
    resetToken: 'token-from-email',
    newPassword: 'newSecurePassword456'
  });
  console.log('Password reset successful!');
} catch (error) {
  console.error('Failed to reset password:', error);
}
```

***

### changePassword()

> **changePassword**(`params`): `Promise`\<`any`>

Changes the user's password.

Updates the password for an authenticated user by verifying
the current password and setting a new one.

#### Parameters

**params** `ChangePasswordParams` *required*

Object containing user ID, current password, and new password.

> [accordion] Properties
>
> **userId** `string` *required*
>
> User ID.
>
> **currentPassword** `string` *required*
>
> Current password for verification.
>
> **newPassword** `string` *required*
>
> New password to set.

#### Returns

`Promise<any>`

Promise resolving when the password is changed successfully.

#### Throws

Error if the current password is incorrect or the request fails.

#### Example

```typescript try { theme={null}
  await base44.auth.changePassword({
    userId: 'user-123',
    currentPassword: 'oldPassword123',
    newPassword: 'newSecurePassword456'
  });
  console.log('Password changed successfully!');
} catch (error) {
  console.error('Failed to change password:', error);
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.