The authentication logic

Let's create a simple authentication logic. The backend expects a username and password with the value of the administrator. If these values are provided, it will return a valid token; otherwise, it will return null. Apply the following changes to the code:

const express = require('express')
const api = express.Router()

const logIn = (username, password) => {
if (username === 'admin' && password === 'admin') {
const userData = {
name: "Admin"
}
return generateToken(userData)
}
return null
}

api
...

The code is very straightforward. First, we compare the value in a simple if conditional, and we created a userData object, which will contain the user information, in this case just a name value is provided. Finally, we will call a generateToken function and pass userData to return a valid token.

Let's implement the generateToken function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset