How to easily implement a user authentication system into your website

How to easily implement a user authentication system into your website

Introduction

As a developer, one of the most important aspects of building an application is user authentication. User authentication is the process of verifying a user’s identity and ensuring that only authorized users can access the application. With so many authentication methods available, it can be overwhelming to choose the right one. In this article, we’ll introduce you to the top user authentication methods and help you determine which one is right for your application.

There are many ways and services which you can use for a authentication system but in this tutorial we will focus on the service “ForgetPasses” which is a passwordless authentication package for NodeJs.

How to use ForgetPasses

Step 1: Install the user authentication package. You can also find the npm package here:

npm install forgetpasses

or with yarn:

yarn add forgetpasses

After this you have to create a service account on the forgetpasses website here.

You now have to copy your API Key from the dashboard (picture) and past it into your code like this:

const forgetpasses = require("forgetpasses")';
var fp = new forgetpasses("YOUR_API_KEY")';

You are now ready to go to start your user authentication.

Now we create a function to check if a user is logged in like this:

app.get('/checkIfClientIsLoggedIn', async (req, res) => {
 data = await fp.checkStatus(req);
 res.send(data)
});

This function retreives the request of the user and returns “true” if the user is logged in and “false” if he is not.

Now we also haven to create a Login-Session in order to let the user login. For this we just use the integrated function of the ForgetPasses package like this:

app.get('/generateLoginSession', async (req, res) => {
 //User gets redirected to google after succesfull logged in
 data = await fp.generateLoginSession(req,res, "https://google.com");
 //Retrieve all data
 res.send(data)
});

This function retreives 3 parameters. The first one is the request and the second one is the response. The last one is the url which the user gets redirected to if the login was succesfull. The variable data now has a lot of JSON Data in it. But we only need the part data.url. This is the url we need to redirect our user to.

If you want to let a user login, you have to redirect the user to the url https://forgetpasses.com/<data.url>.

This will redirect the user to the loginsession which you have created in your backend. With this Session the user can now login into his ForgetPasses account with his username and email.

To verify the user does not need a password, but just click on the link in the email that ForgetPasses sends. The email will look something like this:

user authentication email

If you now need the data of the user you can also retreive them by calling this function:

app.get('/getData', async (req, res) => {
 data = await fp.getUserData(req);
 res.send(data)
});

This function will return all the information of the user if he is loggedin.

Features of ForgetPasses

Now you know how to implement it into your website but there are a lot more features ForgetPasses offers which we want to present you.

ForgetPasses does not only provide a free API, they also provide a own Client and User Dashboard. In the Client-Dashboard website owners can easily manage the users of their website and can also ban or remove them. In the User-Dashboard users can see on which websites they logged in and can also log them out of all websites or services.

Conclusion

User authentication is not that hard and can easy be implemented with ForgetPasses in 10 minutes.

ForgetPasses is a easy package for user authentication. It does not use passwords which makes logging in easier and more secure for the user and is easy to implement.

If you have more questions about user authentication write a comment or watch our Youtube video here

You can also check out our original blog "ForgetPasses-Blog" here