Skip to content

Instantly share code, notes, and snippets.

@joshuaquek
Last active January 11, 2021 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuaquek/4525974085d8981d1e7f0854e55bf70e to your computer and use it in GitHub Desktop.
Save joshuaquek/4525974085d8981d1e7f0854e55bf70e to your computer and use it in GitHub Desktop.
Summary: Authentication for ExpressJS without the need of using PassportJS
Summary: Authentication for ExpressJS without the need of using PassportJS. Slightly modified version of the code mentioned here: https://codeforgeek.com/refresh-token-jwt-nodejs-authentication/
const express = require('express')
const bodyParser = require('body-parser')
const jwt = require('jsonwebtoken')
const tokenCheckerMiddleware = require('./tokenCheckerMiddleware')
const config = require('./config')
const tokenList = {}
const app = express()
app.use(bodyParser.json())
router.get('/', (req,res) => {
res.send('Ok');
})
router.post('/login', (req,res) => {
const postData = req.body;
const user = {
"email": postData.email,
"name": postData.name
}
// do the database authentication here, with user name and password combination.
const token = jwt.sign(user, config.secret, { expiresIn: config.tokenLife})
const refreshToken = jwt.sign(user, config.refreshTokenSecret, { expiresIn: config.refreshTokenLife})
const response = {
"status": "Logged in",
"token": token,
"refreshToken": refreshToken,
}
tokenList[refreshToken] = response
res.status(200).json(response);
})
router.post('/token', (req,res) => {
// refresh the token
const postData = req.body
// if refresh token exists
if((postData.refreshToken) && (postData.refreshToken in tokenList)) {
const user = {
"email": postData.email,
"name": postData.name
}
const token = jwt.sign(user, config.secret, { expiresIn: config.tokenLife})
const response = {
"token": token,
}
// update the token in the list
tokenList[postData.refreshToken].token = token
res.status(200).json(response);
} else {
res.status(404).send('Invalid request')
}
})
// Add tokenCheckerMiddleware as a param to all routes that you want to protect
router.get('/secure', tokenCheckerMiddleware, (req,res) => {
// all secured routes goes here
res.send('I am secured...')
})
app.listen( process.env.PORT || 3000);
{
"secret": "some-secret-stuff-goes-here",
"refreshTokenSecret": "some-secret-refresh-token-stuff",
"tokenLife": 900,
"refreshTokenLife": 86400
}
const jwt = require('jsonwebtoken')
const config = require('./config')
module.exports = (req,res,next) => {
const token = req.body.token || req.query.token || req.headers['x-access-token']
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, config.secret, function(err, decoded) {
if (err) {
return res.status(401).json({"error": true, "message": 'Unauthorized access.' });
}
req.decoded = decoded;
next();
});
} else {
// if there is no token
// return an error
return res.status(403).send({
"error": true,
"message": 'No token provided.'
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment