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
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
| Parameter | Type | Description |
|---|---|---|
query | Record<string, any> | Query to find the user |
newPass | string | The 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
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
| Parameter | Type | Description |
|---|---|---|
id | string | The temporary ID |
password | string | The new password |
code | string | The 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
import { userManager } from '@modular-rest/server';
const code = userManager.generateVerificationCode('user@example.com', 'email');
// Returns: '123' (default) or custom generated codeParameters
| Parameter | Type | Description |
|---|---|---|
id | string | User ID or identifier |
idType | string | Type of ID (email, phone) |
Returns
string
Verification code
getUserById()
getUserById(
id):Promise<User>
Gets a user by their ID
Example
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
| Parameter | Type | Description |
|---|---|---|
id | string | The 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
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
| Parameter | Type | Description |
|---|---|---|
id | string | The identity of the user |
idType | string | The 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
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
| Parameter | Type | Description |
|---|---|---|
token | string | The 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
import { userManager } from '@modular-rest/server';
const isValid = userManager.isCodeValid('user123', '123');
if (isValid) {
// Proceed with verification
}Parameters
| Parameter | Type | Description |
|---|---|---|
id | string | The ID of the user |
code | string | The verification code |
Returns
boolean
Whether the verification code is valid
issueTokenForUser()
issueTokenForUser(
Promise<string>
Issues a JWT token for a user by email
Example
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
| Parameter | Type | Description |
|---|---|---|
email | string | The 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
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
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
| Parameter | Type | Default value | Description |
|---|---|---|---|
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
import { userManager } from '@modular-rest/server';
const tempId = userManager.registerTemporaryID('user@example.com', 'password_reset', '123456');Parameters
| Parameter | Type | Description |
|---|---|---|
id | string | The ID to register |
type | string | The type of temporary ID |
code | string | The verification code |
Returns
string
The registered ID
registerUser()
registerUser(
detail):Promise<string>
Registers a new user
Example
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
| Parameter | Type | Description |
|---|---|---|
detail | UserRegistrationDetail | User 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
import { userManager } from '@modular-rest/server';
userManager.setCustomVerificationCodeGeneratorMethod((id, type) => {
return Math.random().toString(36).substring(2, 8).toUpperCase();
});Parameters
| Parameter | Type | Description |
|---|---|---|
generatorMethod | (id, idType) => string | Function that generates verification codes |
Returns
void
submitPasswordForTemporaryID()
submitPasswordForTemporaryID(
id,password,code):Promise<string>
Submits a password for a temporary ID
Example
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
| Parameter | Type | Description |
|---|---|---|
id | string | The temporary ID |
password | string | The new password |
code | string | The verification code |
Returns
Promise<string>
Promise resolving to the JWT token
Throws
If verification code is invalid or user is not found