#@authxyz/local
Authxyz's local auth provider comes with a wide range of functionality like login , register, reset password , verification and so on...
#Project Setup
Create a new project with an awsome name and install the following dependencies.
#Code setup
Add this code to your file to make your project up and running.
#Creating a database adapter
A database adapter is something that will handle the read & writes for our package for authentication.
adataper.addUserSchema()
will create a User
model in your database which have a schema like this.
#Initializing the Local auth provider
roles
: Roles is an array of strings which you going to have in your app. For exampleuser
,admin
etc.adapter
: Database adapter for read & write.auth
: An auth object which contains configuration for auth.
#Creating a user register route
To create a user register route we have to use the register
function provided by the Local
class.
This will create a user and returns the auth token depending upon your auth strategy.
You can customise the functions upto how much you want, simply it's upto you. See this to extend your functionality.
On a successful registration you will get a response from the server :
#Creating a user login route
To create a user register route we have to use the register
function provided by the Local
class.
See this to extend your functionality.
On a successful login you will get a response from the server :
#RBAC Middleware for route protection.
You can use the built-in RBAC middleware of local auth to protect the routes.
localAuth.protect(["user"])
this function alone will take care of the upcoming request and let the user access the endpoint if it has a role of user. You can pass down multiple roles as an array also.
#More methods
Similarly you can use other functions such as forgotPassword
, resetPassword
, verify
, resendVerification
.
#Forgot Password
You can use
forgotPassword
method to add password reset functionality into your application. This requires a mail trigger.On a successful request you will get a response from the server :
But this alone can't do all the things. You have to actually send the password reset code to the user.
- Create a mail client with Nodemailer and add it to your
localAuth
usingmailConfig
method.
- Now add a trigger so that whenever a api request to the forgot password route is successful then send a mail to the user.
Now this will add a trigger so that we can send a mail to the user with the reset code .
- Create a mail client with Nodemailer and add it to your
#Reset Password
You can use
resetPassword
method to add password reset functionality into your application.On a successful request you will get a response from the server :