Advertisement
andreyradkevich

Untitled

Mar 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // CAll use
  3. app.use(callbackFunction);
  4.  
  5. //This function save decode.role in global variable successToken
  6. function haveTokenInformationRole(decode) {
  7.     successToken = decode;
  8. };
  9.  
  10. // Callback for use function
  11. function callbackFunction(req,res,next) {
  12.     var cookies = getCookiesMap(req.headers.cookie);
  13.     var token = cookies["tokenName"];
  14.     if (token){
  15.         jwt.verify(token,'secret_code', function (err,decode) {
  16.             haveTokenInformationRole(decode.role);
  17.         })}else{
  18.         res.redirect(localHost+'/html')
  19.     }
  20.     next();
  21. };
  22. //Check adminRequest
  23. function adminRequest(req,res) {
  24.     if(successToken === 'admin'){
  25.         res.sendFile('C:/nodeJS/public/pageForJWT/admin.html')
  26.     }else{
  27.         res.redirect('/html')
  28.     }
  29. }
  30. //Check guestRequest
  31. function guestRequest(req,res) {
  32.     if(successToken === 'guest'){
  33.         res.sendFile('C:/nodeJS/public/pageForJWT/guest.html')
  34.     }else{
  35.         res.redirect('/html')
  36.     }
  37. }
  38. //Check userRequest
  39. function userRequest(req,res) {
  40.     if(successToken === 'user'){
  41.         res.sendFile('C:/nodeJS/public/pageForJWT/user.html')
  42.     }else{
  43.         res.redirect('/html')
  44.     }
  45. }
  46.  
  47. // Basic page
  48. app.get ('/',function (req,res) {
  49.     res.redirect('/html');
  50. });
  51.  
  52. // Get methods with callbacks who check role
  53. app.get ('/api/admin',adminRequest);
  54. app.get ('/api/guest',guestRequest);
  55. app.get ('/api/user',userRequest);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement