Auth
new Auth(options)
The Auth constructor accepts the following configuration
- options.secretKey
string
Secret key to use in generating the JWT. - options.model
model
User model.
;; const AccountModel = async ... async ... async ... ; const auth = secretKey: '4fb473f82ba47bf6acbab33e7529fb96' model: AccountModel;
auth.createAccessToken(params)
Create an access token.
- params.username
string
Username. - params.password
string
Password. - params.expiresIn
string
(Optional) Amount of time before the accessToken expires. Must be compatible to thems
package. - params.claims
object
(Optional) Additional claims to include in the JWT. - Returns: accessToken
Promise<string>
- Throws:
INVALID_CREDENTIALS
await auth;
auth.verifyAccessToken(params)
Verify a json web token.
- params.accessToken
string
Access token. - params.subject
string | number
(Optional) ID of the owner of the accessToken. - Returns: claims
Promise<object>
Claims stored in the JWT. - Throws:
INVALID_TOKEN
await auth;
auth.changePassword(params)
Change the user password.
- params.accessToken
string
Access token. - params.subject
string | number
(Optional) ID of the owner of the accessToken. - params.oldPassword
string
Old password. - params.newPassword
string
New Password. - Throws:
INVALID_TOKEN
INVALID_CREDENTIALS
await auth;
auth.requestResetPassword(params)
Request for a password reset.
- params.subject
string | number
ID of the user requesting the password reset. - params.expiresIn
string
(Optional) Amount of time before the requestToken expires. - Returns: requestToken
Promise<string>
- Throws:
USER_NOT_FOUND
await auth;
auth.resetPassword(params)
Reset password.
- params.requestToken
string
Request token. - params.subject
string | number
(Optional) ID of the owner of the requestToken. - params.password
string
New Password. - Throws:
INVALID_TOKEN
await auth;
Auth Model
The model should have the following methods
- findByUsername(username)
Promise<{ id: string | number, username: string, password: string }>
- findById(id)
Promise<{ id: string | number, username: string, password: string }>
- updatePassword(id, password)
Promise<void>
const bcrypt = ;const R = ; { thisusers = ; } /* additional method for the sake of context */ async { thisusers; } async { return Rthisusers; } async { return Rthisusers; } async { const user = Rthisusers; if !user return; userpassword = password; }