Last active
August 22, 2021 21:20
-
-
Save pantharshit00/444626d3f627e1cfcc1691d90c5bcc67 to your computer and use it in GitHub Desktop.
Simple express app using JWT authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* To get started install | |
* express bodyparser jsonwebtoken express-jwt | |
* via npm | |
* command :- | |
* npm install express body-parser 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(); | |
// See the react auth blog in which cors is required for access | |
app.use((req, res, next) => { | |
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000'); | |
res.setHeader('Access-Control-Allow-Headers', 'Content-type,Authorization'); | |
next(); | |
}); | |
// Setting up bodyParser to use json and set it to req.body | |
app.use(bodyParser.json()); | |
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, username: user.username }, 'keyboard cat 4 ever', { expiresIn: 129600 }); // Sigining the token | |
res.json({ | |
sucess: true, | |
err: null, | |
token | |
}); | |
break; | |
} | |
else { | |
res.status(401).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 | |
}); | |
// Error handling | |
app.use(function (err, req, res, next) { | |
if (err.name === 'UnauthorizedError') { // Send the error rather than to show it on the console | |
res.status(401).send(err); | |
} | |
else { | |
next(err); | |
} | |
}); | |
// Starting the app on PORT 3000 | |
const PORT = 8080; | |
app.listen(PORT, () => { | |
// eslint-disable-next-line | |
console.log(`Magic happens on port ${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update the NPM command to be
npm install express body-parser jsonwebtoken express-jwt --save
. bodyparser is incorrect.