daily pastebin goal
73%
SHARE
TWEET

Untitled

a guest Feb 16th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. const jwt = require('jsonwebtoken');
  4.  
  5.  
  6. exports.generateToken = async (data) => {
  7.     return jwt.sign(data, global.SALT_KEY, {
  8.         expiresIn: '1d'
  9.     })
  10. };
  11.  
  12. exports.decodeToken = async (token) => {
  13.     var data = await jwt.verify(token, global.SALT_KEY);
  14.     return data;
  15. };
  16.  
  17. exports.authorize = (req, res, next) => {
  18.     var token = req.body.token || req.query.token || req.headers['x-access-token'];
  19.  
  20.     if (!token) {
  21.         res.status(401).json({
  22.             message: 'Access denied'
  23.         });
  24.     } else {
  25.         jwt.verify(token, global.SALT_KEY, (e, decoded) => {
  26.             if (e){
  27.                 res.status(401).json({
  28.                     message: 'Access denied'
  29.                 })
  30.             } else{
  31.                 next();
  32.             }
  33.         });
  34.     }
  35. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top