Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * To get started install
- * express bodyparser jsonwebtoken express-jwt
- * via npm
- * command :-
- * npm install express bodyparser jsonwebtoken express-jwt --save
- */
- // Bringing all the dependencies in
- const express = require('express');
- const bodyParser = require('body-parser');
- const jwt = require('jsonwebtoken');
- const exjwt = require('express-jwt');
- // Instantiating the express app
- const app = express()
- // Setting up bodyParser to use URL ENCODED and set it to req.body
- app.use(bodyParser.urlencoded({ extended: true }))
- // INstantiating the express-jwt middleware
- const jwtMW = exjwt({
- secret: "keyboard cat 4 ever"
- })
- // MOCKING DB just for test
- let users = [
- {
- id: 1,
- username: "test",
- password: "asdf123"
- },
- {
- id: 2,
- username: "test2",
- password: "asdf12345"
- }
- ]
- // LOGIN ROUTE
- app.post('/login', (req, res) => {
- const { username, password } = req.body;
- // Use your DB ORM logic here to find user and compare password
- for (let user of users) { // I am using a simple array users which i made above
- if (username == user.username && password == user.password /* Use your password hash checking logic here !*/) {
- //If all credentials are correct do this
- let token = jwt.sign({ id: user.id }, "keyboard cat 4 ever", { expiresIn: 129600 }) // Sigining the token
- res.json({
- sucess: true,
- err: null,
- token
- })
- break;
- }
- else {
- res.json({
- sucess: false,
- token: null,
- err: "Username or password is incorrect"
- })
- }
- }
- })
- app.get('/', jwtMW /* Using the express jwt MW here */, (req, res) => {
- res.send("You are authenticated") //Sending some response when authenticated
- })
- // Starting the app on PORT 3000
- const PORT = 3000;
- app.listen(PORT, () => {
- console.log(`Magic happens on port ${PORT}`)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement