Services module providing access to the ResetToken database table
- Source:
Example
import { resetTokens as resetTokensService } from '/src/services';
Methods
(async, static) createOne(UserId) → {Object}
Generate a new reset token for a given UserId
Parameters:
| Name | Type | Description |
|---|---|---|
UserId |
String | User ID foreign key to associate with reset token. |
Throws:
-
Will throw an error if a UserId is not passed in.
-
Will throw an error if a created ResetToken instance is not created.
Returns:
- Reset token object or thrown error.
- Type
- Object
Example
import { resetTokens as resetTokensService } from '/src/services';
const UserId = 1;
const createdResetToken = await resetTokensService.createOne(UserId);
(async, static) destroyExpiredTokens() → {Number}
Destroy all tokens that have expired past now.
Throws:
Will throw an error if destroyedCount is not created
Returns:
- Return number of rows destroyed
- Type
- Number
Example
import { resetTokens as resetTokensService } from '/src/services';
const destroyedCount = await resetTokensService.destroyExpiredTokens();
(async, static) destroyOneByPk(id) → {Number}
Destroy reset token with a given primary key ID
Parameters:
| Name | Type | Description |
|---|---|---|
id |
Number | Primary key id to destroy. |
Throws:
-
Will throw an error if no primary key id is passed in.
-
Will throw an error if no destroyedCount is created.
Returns:
Number of records destroyed or error.
- Type
- Number
Example
import { resetTokens as resetTokensService } from '/src/services';
const id = 1;
const destroyedCount = await resetTokensService.destroyOneByPk(id);
(async, static) findAll(offsetopt, limitopt, whereopt) → {JSON}
Find and return all reset tokens in the database table
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
offset |
Number |
<optional> |
Number of pages to offset based on limit. |
limit |
Number |
<optional> |
Limit the number of rows returned. |
where |
String |
<optional> |
Filter records by. |
Throws:
Will throw an error if foundResetTokens is not created.
Returns:
Returns a JSON array of found tokens.
- Type
- JSON
Example
import { resetTokens as resetTokensService } from '/src/services';
const foundResetTokens = await resetTokensService.findAll();
(async, static) findOneByPk(id) → {JSON}
Find a reset token with a given primary key (ID)
Parameters:
| Name | Type | Description |
|---|---|---|
id |
Number | The primary key for token row. |
Throws:
-
Will throw an error if no primary key id passed in.
-
Will throw an error if no foundResetToken is created.
Returns:
Return found token record.
- Type
- JSON
Example
import { resetTokens as resetTokensService } from '/src/services';
const id = 1;
const foundResetToken = await resetTokensService.findOneByPk(id);
(async, static) findOneByToken(token) → {JSON}
Find a record with a given reset token value
Parameters:
| Name | Type | Description |
|---|---|---|
token |
String | Token string to search table. |
Throws:
-
Will throw an error if no resetToken string is passed in.
-
Will throw an error if no foundResetToken is created.
Returns:
JSON array of first found record.
- Type
- JSON
Example
import { resetTokens as resetTokensService } from '/src/services';
const resetToken = kljkDSOIJndkfo98u34nlkndio8dfknkjdf09ujkl;
const foundResetToken = await resetTokensService.findOneByPk(resetToken);
(async, static) updateOneByPk(id, resetTokenData) → {Object}
Find and update reset token record
Parameters:
| Name | Type | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
id |
Number | Primary key to find. |
||||||||
resetTokenData |
Object | JSON object of data to update. Properties
|
Throws:
-
Will throw an error if now primary key id or token data is passed in.
-
Will throw an error if no resetToken is created
Returns:
JSON token object.
- Type
- Object
Example
import { resetTokens as resetTokensService } from '/src/services';
const id = 1;
const data = { isUsed: false }
const createdResetToken = await resetTokensService.updateOneByPk(id,data);
(async, static) updateUsedByUserId(UserId) → {Boolean}
Set all token records for a given UserId as used.
Parameters:
| Name | Type | Description |
|---|---|---|
UserId |
String | UserId to set all token records as isUsed TRUE |
Throws:
Will throw an error if no UserId is passed in
Returns:
Return TRUE if updated or error
- Type
- Boolean
Example
import { resetTokens as resetTokensService } from '/src/services';
const UserId = 1;
await resetTokensService.updateUsedByUserId(UserId);
(async, static) validateToken(UserId, token) → {Boolean}
Check if UserId and token returns a record. Then check the record to see if it is used or expired.
Parameters:
| Name | Type | Description |
|---|---|---|
UserId |
String | User ID to check token against. |
token |
String | Token to confirm. |
Requires:
- module:database/ResetToken
- module:utils/logger
- module:moment
Returns:
Return true if UserId and token is validate.
- Type
- Boolean
Example
import { resetTokens as resetTokensService } from '/src/services';
const UserId = 1;
const token = 'kjdfoOIJDFioeh390udfLKJ09dufjoidfj';
const isValid = resetTokensService(UserId, token);