@universal-packages/authentication v1.13.3
Authentication
Dynamic authentication api base to bu plugged in with dynamic api modules to shape any custom authentication system. It comes with a default modules to handle a very simp,e email password authentication system, but it can be extended or replaced for a more sophisticated one.
Install
npm install @universal-packages/authenticationAuthentication
The Authentication class is a descendant of DynamicApi class, it is the entry interface to load and perform all our authentication related dynamics.
import { Authentication } from '@universal-packages/authentication'
const authentication = new Authentication({ dynamicsLocation: './src', secret: 'my secret' })
await authentication.loadDynamics()
const result = await authentication.performDynamic('log-in', { email: 'david@universal-packages.com', password: '12345678' })
console.log(result)
// > { status: 'success', user: { id: 69, username: 'username', createdAt: [Date] } }To override override parts of the default system just create non default dynamics in your dynamics location with the extension prefix auth-dynamic, ex: LogIn.auth-dynamic.js and export ad default dynamic class there. More about all the dynamics that can be override below.
Options
Authentication take options similar to DynamicApi options:
debugBooleanIf true the instance of this authentication dynamic api will keep track of what is being performed into a log.dynamicsLocationRequiredStringWhere to look up for dynamics to load withe to override default ones or new ones.secretRequiredStringSecret used for cryptography to generate tokens and verifying them for this authentication instance in particular.modulesMapAuthentication api modules to be enabled and configuredoneTimePasswordOneTimePasswordOptionsOptions to configure how one time passwords are generated and validated. Check The options heredefaultModuleDynamicApiModuleenabledBooleanWhether the module is enabled or not.optionsObjectemailValidationObjectmatcherRegexpCustom matcher to validate email.sizeObjectminNumberMinimum size of the email.maxNumberMaximum size of the email.
passwordValidationObjectsizeObjectminNumberMinimum size of the password.maxNumberMaximum size of the password.
Authentication Api override required dynamics
These dynamics are required to be override to have a fully functional authentication system.
create-user
Logic to create a new user with the given attributes.
PAYLOADObjectattributesUserAttributes
RESULTUser
update-user
PAYLOADObjectuserUserattributesUserAttributes
RESULTvoid
user-from-id
PAYLOADObjectidString | Number | BigInt
RESULTUser
Authentication Api override non required dynamics
These dynamics are not required to be override but can be to have a more sophisticated authentication system and they provide good default behavior.
encrypt-password
It encrypts a password to be stored in the database.
PAYLOADObjectpasswordString
RESULTString
generate-concern-secret
Generates a secret for a concern. Hopeful to be used to generate tokens for a concern
PAYLOADObjectconcernStringidentifierString
RESULTString
generate-one-time-password
Generates a one time password for a concern and identifier. For example to be used to reset a password.
PAYLOADObjectconcernStringidentifierString
RESULTString
verify-one-time-password
Verifies a one time password for a concern and identifier. For example to be used to reset a password.
PAYLOADObjectconcernStringidentifierStringoneTimePasswordString
RESULTBoolean
Default module dynamics
log-in Async
Verifies email and password and if all configured behavior is positive it returns the user for which the credentials matched.
const result = authentication.perform('log-in', { email: 'david@universal-packages.com', password: 'password' })PAYLOADObjectemailStringpasswordString
RESULTAuthenticationResultuserUsermessage?invalid-credentialsfailure
request-password-reset Async
Generates a password reset and performs the send-password-reset dynamic passing the one time password.
const result = authentication.perform('request-password-reset', { email: 'david@u-p.com' })PAYLOADObjectemailString
RESULTAuthenticationResultmessage?nothing-to-do`warning
sign-up Async
Validates sign up attributes and creates the new user.
const result = authentication.perform('sign-up', { email: 'some email', password: 'some password' })PAYLOADObjectemailStringpasswordString
RESULTAuthenticationResultuserUservalidationValidationResultfailurevalidBooleanerrorsObject<attribute>String[]
update-email-password Async
Validates and updates an user with new email and or password.
const result = authentication.perform('update-email-password', { email: 'new-email', user })PAYLOADObjectuserUseremailStringpasswordString
RESULTAuthenticationResultuserUservalidationValidationResultfailurevalidBooleanerrorsObject<attribute>String[]
verify-password-reset Async
Verifies a password reset and sets a new password
const result = authentication.perform('verify-password-reset', { email: 'email@email.com', oneTimePassword: '123456', password: 'new password' })PAYLOADObjectemailStringoneTimePasswordStringpasswordString
RESULTAuthenticationResultvalidationValidationResultfailurevalidBooleanerrorsObject<attribute>String[]
message?invalid-one-time-passwordfailure
Default module extensions
file file file file file file file file file
These dynamics are used to extend the default module dynamics, they are called on specific points while logging in and signing up.
after-log-in-user-not-found
When the user is not found while logging in. Write your custom dynamic to handle this case.
PAYLOADObjectemailString
RESULTvoid
after-log-in-failure
When the log in fails. Write your custom dynamic to handle this case.
PAYLOADObjectuserUser
RESULTvoid
after-log-in-success
When the log in is successful. Write your custom dynamic to handle this case.
PAYLOADObjectuserUser
RESULTvoid
after-sign-up-failure
When the sign up fails. Write your custom dynamic to handle this case.
PAYLOADObjectemailStringpasswordStringvalidationValidationResult
RESULTvoid
after-sign-up-success
When the sign up is successful. Write your custom dynamic to handle this case.
PAYLOADObjectuserUser
RESULTvoid
after-update-success
When the update is successful. Write your custom dynamic to handle this case.
PAYLOADObjectuserUser
RESULTvoid
continue-after-log-in-user-found?
When the user is found while logging in. Write your custom dynamic to handle this case and return true if you want to continue with the default behavior.
PAYLOADObjectuserUser
RESULTBoolean
continue-before-log-in?
Before logging in. Write your custom dynamic to handle this case and return true if you want to continue with the default behavior.
PAYLOADObjectemailStringpasswordString
RESULTBoolean
continue-before-sign-up?
Before signing up. Write your custom dynamic to handle this case and return true if you want to continue with the default behavior.
PAYLOADObjectemailStringpasswordString
Default module override required dynamics
Dynamics that you need to override to have a fully functional default module.
user-from-email
PAYLOADObjectemailString
RESULTUser
user-exists-with-email?
PAYLOADObjectemailString
RESULTBoolean
send-password-reset
PAYLOADObjectuserUseroneTimePasswordString
RESULTvoid
send-password-was-reset
PAYLOADObjectuserUser
RESULTvoid
send-welcome
PAYLOADObjectuserUser
RESULTvoid
Default module override non required dynamics
do-passwords-match?
PAYLOADObjectpasswordStringencryptedPasswordString
RESULTBoolean
get-user-current-email
PAYLOADObjectuserUser
RESULTString
get-user-encrypted-password
PAYLOADObjectuserUser
RESULTString
validate-password-reset
PAYLOADObjectpasswordString
RESULTValidationResultvalidBooleanerrorsObject<attribute>String[]
validate-sign-up
PAYLOADObjectemailStringpasswordString
RESULTValidationResultvalidBooleanerrorsObject<attribute>String[]
validate-update
PAYLOADObjectemailStringpasswordString
RESULTValidationResultvalidBooleanerrorsObject<attribute>String[]
Decorators
Encrypt([propertyToEncrypt: string])
Use this decorator to automatically encrypt attributes in a class. For example for the password attribute, when decorated, every time is set, the encryptedPassword attribute is going to set with a hashed and salted string based on the password. It sets depending on the base attribute name encrypted<Attribute>.
import { Encrypt } from '@universal-packages/authentication'
export default class User {
@Encrypt()
secret
encryptedSecret
}
const user = new User()
user.secret = 'my password'
console.log(user.secret, user.encryptedSecret)
// > undefined C49HSl4okw8yoCKfoNRnsqD4T0T6SJZkdpTgU1o478Mk4GT995KV5HUKzvsnN1fShOo9sdDQq3Rjiz+Brj9bCIeJfWrt7tMl936wWyBARkPCdDlj9OfLNNDnhGo7dkmbU8YBfpgcmoMUmCuIftupOik+Nk/Eu83J4epW5y2w0fM=You can also specify the attribute name to store the hashed password.
import { Encrypt } from '@universal-packages/authentication'
export default class User {
@Encrypt('hashedSecret')
secret
hashedSecret
}Typescript
This library is developed in TypeScript and shipped fully typed.
Contributing
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.
License
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago