Advertisement
shopnilSS

Authorization Permission

Apr 15th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const json_decode = require("jwt-decode");
  2.  
  3. const res = (data) => {
  4.         return (req, res, next) => {
  5.             const token  = req.header("Authorization") //get the token from body input
  6.             const decode = json_decode(token) //decode the token data
  7.             const userType = decode.userType //get the user type
  8.             const queryData = data;//get the data from a route
  9.             const isFind = queryData.find(val => {
  10.                 return val == userType
  11.             })//is it available or not just check it
  12.            
  13.             if(isFind){
  14.                 next()
  15.             }else{
  16.                 next("Restricted Route")
  17.             }
  18.            
  19.     }
  20. }
  21.  
  22.  
  23.  
  24. module.exports = {
  25.     res
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement