Skip to content

UserManager Service ​

User manager class for handling user operations

This service provides functionality for managing users, including:

  • User registration and authentication
  • Password management
  • Token generation and verification
  • Temporary ID handling for password reset and verification

Methods ​

changePassword() ​

changePassword(query, newPass): Promise<void>

Changes a user's password

Example ​

typescript
import { userManager } from '@modular-rest/server';

try {
  await userManager.changePassword(
    { email: 'user@example.com' },
    'newpassword123'
  );
  console.log('Password changed successfully');
} catch (error) {
  console.error('Failed to change password:', error);
}

Parameters ​

ParameterTypeDescription
queryRecord<string, any>Query to find the user
newPassstringThe new password

Returns ​

Promise<void>

Promise resolving when password is changed

Throws ​

If user is not found or password change fails


changePasswordForTemporaryID() ​

changePasswordForTemporaryID(id, password, code): Promise<string>

Changes password for a temporary ID

Example ​

typescript
import { userManager } from '@modular-rest/server';

try {
  const token = await userManager.changePasswordForTemporaryID(
    'user@example.com',
    'newpassword123',
    '123456'
  );
  console.log('Password changed successfully');
} catch (error) {
  console.error('Failed to change password:', error);
}

Parameters ​

ParameterTypeDescription
idstringThe temporary ID
passwordstringThe new password
codestringThe verification code

Returns ​

Promise<string>

Promise resolving to the JWT token

Throws ​

If verification code is invalid or user is not found


generateVerificationCode() ​

generateVerificationCode(id, idType): string

Generates a verification code for a user

Example ​

typescript
import { userManager } from '@modular-rest/server';

const code = userManager.generateVerificationCode('user@example.com', 'email');
// Returns: '123' (default) or custom generated code

Parameters ​

ParameterTypeDescription
idstringUser ID or identifier
idTypestringType of ID (email, phone)

Returns ​

string

Verification code


getUserById() ​

getUserById(id): Promise<User>

Gets a user by their ID

Example ​

typescript
import { userManager } from '@modular-rest/server';

try {
  const user = await userManager.getUserById('user123');
  console.log('User details:', user);
} catch (error) {
  console.error('Failed to get user:', error);
}

Parameters ​

ParameterTypeDescription
idstringThe ID of the user

Returns ​

Promise<User>

Promise resolving to the user

Throws ​

If user model is not found or user is not found


getUserByIdentity() ​

getUserByIdentity(id, idType): Promise<User>

Gets a user by their identity (email or phone)

Example ​

typescript
import { userManager } from '@modular-rest/server';

// Get user by email
const user = await userManager.getUserByIdentity('user@example.com', 'email');

// Get user by phone
const user = await userManager.getUserByIdentity('+1234567890', 'phone');

Parameters ​

ParameterTypeDescription
idstringThe identity of the user
idTypestringThe type of the identity (phone or email)

Returns ​

Promise<User>

Promise resolving to the user

Throws ​

If user model is not found or user is not found


getUserByToken() ​

getUserByToken(token): Promise<User>

Gets a user by their JWT token

Example ​

typescript
import { userManager } from '@modular-rest/server';

try {
  const user = await userManager.getUserByToken('jwt.token.here');
  console.log('Authenticated user:', user);
} catch (error) {
  console.error('Invalid token:', error);
}

Parameters ​

ParameterTypeDescription
tokenstringThe JWT token of the user

Returns ​

Promise<User>

Promise resolving to the user

Throws ​

If token is invalid or user is not found


isCodeValid() ​

isCodeValid(id, code): boolean

Checks if a verification code is valid

Example ​

typescript
import { userManager } from '@modular-rest/server';

const isValid = userManager.isCodeValid('user123', '123');
if (isValid) {
  // Proceed with verification
}

Parameters ​

ParameterTypeDescription
idstringThe ID of the user
codestringThe verification code

Returns ​

boolean

Whether the verification code is valid


issueTokenForUser() ​

issueTokenForUser(email): Promise<string>

Issues a JWT token for a user by email

Example ​

typescript
import { userManager } from '@modular-rest/server';

try {
  const token = await userManager.issueTokenForUser('user@example.com');
  console.log('Issued token:', token);
} catch (error) {
  console.error('Failed to issue token:', error);
}

Parameters ​

ParameterTypeDescription
emailstringThe email of the user

Returns ​

Promise<string>

Promise resolving to the JWT token

Throws ​

If user is not found


loginAnonymous() ​

loginAnonymous(): Promise<string>

Logs in an anonymous user and returns their JWT token

Example ​

typescript
import { userManager } from '@modular-rest/server';

const token = await userManager.loginAnonymous();
console.log('Anonymous token:', token);

Returns ​

Promise<string>

Promise resolving to the JWT token


loginUser() ​

loginUser(id?, idType?, password?): Promise<string>

Logs in a user and returns their JWT token

Example ​

typescript
import { userManager } from '@modular-rest/server';

try {
  // Login with email
  const token = await userManager.loginUser('user@example.com', 'email', 'password123');

  // Login with phone
  const token = await userManager.loginUser('+1234567890', 'phone', 'password123');
} catch (error) {
  console.error('Login failed:', error);
}

Parameters ​

ParameterTypeDefault valueDescription
id?string''The ID of the user (email or phone)
idType?string''The type of the ID (phone or email)
password?string''The password of the user

Returns ​

Promise<string>

Promise resolving to the JWT token

Throws ​

If user is not found or credentials are invalid


registerTemporaryID() ​

registerTemporaryID(id, type, code): string

Registers a temporary ID for verification or password reset

Example ​

typescript
import { userManager } from '@modular-rest/server';

const tempId = userManager.registerTemporaryID('user@example.com', 'password_reset', '123456');

Parameters ​

ParameterTypeDescription
idstringThe ID to register
typestringThe type of temporary ID
codestringThe verification code

Returns ​

string

The registered ID


registerUser() ​

registerUser(detail): Promise<string>

Registers a new user

Example ​

typescript
import { userManager } from '@modular-rest/server';

try {
  const token = await userManager.registerUser({
    email: 'user@example.com',
    password: 'secure123',
    permissionGroup: 'user',
    phone: '+1234567890'
  });
  console.log('User registered successfully');
} catch (error) {
  console.error('Registration failed:', error);
}

Parameters ​

ParameterTypeDescription
detailUserRegistrationDetailUser registration details

Returns ​

Promise<string>

Promise resolving to the JWT token

Throws ​

If user model is not found or registration fails


setCustomVerificationCodeGeneratorMethod() ​

setCustomVerificationCodeGeneratorMethod(generatorMethod): void

Sets a custom method for generating verification codes

Example ​

typescript
import { userManager } from '@modular-rest/server';

userManager.setCustomVerificationCodeGeneratorMethod((id, type) => {
  return Math.random().toString(36).substring(2, 8).toUpperCase();
});

Parameters ​

ParameterTypeDescription
generatorMethod(id, idType) => stringFunction that generates verification codes

Returns ​

void


submitPasswordForTemporaryID() ​

submitPasswordForTemporaryID(id, password, code): Promise<string>

Submits a password for a temporary ID

Example ​

typescript
import { userManager } from '@modular-rest/server';

try {
  const token = await userManager.submitPasswordForTemporaryID(
    'user@example.com',
    'newpassword123',
    '123456'
  );
  console.log('Password set successfully');
} catch (error) {
  console.error('Failed to set password:', error);
}

Parameters ​

ParameterTypeDescription
idstringThe temporary ID
passwordstringThe new password
codestringThe verification code

Returns ​

Promise<string>

Promise resolving to the JWT token

Throws ​

If verification code is invalid or user is not found