Skip to main content

Token

TokenServices a service class that provides methods for managing tokens in the database.

Usage

Example Basic usage:

const { service } = ecoflow;

const { TokenServices } = service;

const tokens = TokenServices.getAllTokens();

console.log(tokens);

API Reference

Generate Token

generateToken(_id)Promise<[accessToken, refreshToken, refreshTokenExpiresAt]>

Generates access and refresh tokens for a given user ID and sets the refresh token in the database.

Return: A promise that resolves to an array containing the access token, refresh token, and refresh token expiration time.

Available arguments :

ParameterTypeDescription
_idstringThe user ID for which tokens are generated.

Return arguments types :

ParameterTypeDescription
accessTokenstringThe access token of the user ID provided.
refreshTokenstringThe refresh token of the user ID provided.
refreshTokenExpiresAtnumberThe refresh token expiration time.

Verify Token

checkToken(token, userId)Promise<boolean>

Asynchronously checks if a given token is valid for a specific user.

Return: A promise that resolves to a boolean indicating if the token is valid.

Available arguments :

ParameterTypeDescription
tokenstringThe token to check for validity.
userIdstringThe ID of the user associated with the token.

Remove Token

removeToken(token, userId)Promise<void>

Removes a token associated with a specific user from the database.

Return: A promise that resolves once the token is successfully removed.

Available arguments :

ParameterTypeDescription
tokenstringThe token to be removed.
userIdstringThe ID of the user associated with the token.

Migrate Token

migrateToken(token)Promise<void>

Migrates a token to the database based on the type of database connection.

Return: A promise that resolves when the token is successfully migrated.

Available arguments :

ParameterTypeDescription
tokenTokensThe token object to migrate.

Fetch All Token

getAllTokens()Promise<Tokens[]>

Retrieves all tokens from the database based on the type of connection.

Return: A promise that resolves to an array of tokens.

TypeScript Properties

Tokens

interface Tokens {
/** The unique identifier of the token. */
_id?: string;

/** The user ID associated with the token. */
userId: String;

/** The token string. */
token: String;

/** The timestamp or date when the token was created. */
created_at: number | Date;

/** The timestamp or date when the token was last updated. */
updated_at: number | Date;

/** The timestamp or date when the token expires. */
expires_at: number | Date;
}