Hey Guys,
in this post I will show you how you can create a easy user authentication for your NodeJs application.
First we need to install express,forgetpasses and body-parser
npm install forgetpasses
npm install express
npm install body-parser
After that we will create a new express app like this:
var express = require("express");
var app = express();
Now we want to configure the body-parser:
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(bodyParser.json());
Now we succesfully created a express app with the body-parser. In order to implement the authentication system, we now want to configure the forgetpasses package.
const forgetpasses = require("forgetpasses");
var fp = new forgetpasses("YOUR_API_TOKEN")
You can get your api token on https://forgetpasses.com/dashboard/settings after you created a service account on https://forgetpasses.com/sign-up/client
If you want to check the token you can call this function of the package:
data = await fp.checkToken(req);
console.log(data)
This will log true
if your token is working and false
if its wrong.
If you now want to let a user login you can do it this way:
app.get("/generateLoginSession", async (req,res) =>{
data = await
fp.generateLoginSession(req,res,"https://google.com")
res.send(data)
})
You can now open this page and will see a lot of json send to your browser.
You will now copy the url
and redirect your user to the url https://forgetpasses.com/loginsession/URL
After a succesfull login your user will get redirected to the url you passes into the function. In our case its https://google.com
With this url the user now can login and you can check if a user is logged in with this way:
app.get("/checkIfClientIsLoggedIn", async(req,res) =>{
data = await fp.checkStatus(req);
res.send(data);
})
This will log true
if your user is loggedin and false
if your user is not
Thanks for reading. If you have question let me know
Top comments (0)