Advertisement
Guest User

Untitled

a guest
Jun 13th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. /**
  2. * accessToken
  3. *
  4. * @module :: Policy
  5. * @description :: Simple policy to allow any authenticated user
  6. * @docs :: http://sailsjs.org/#!/documentation/concepts/Policies
  7. *
  8. */
  9. var UserRepo = require('../services/repo/UserRepo');
  10.  
  11. module.exports = function(req, res, next) {
  12.  
  13. if (req.headers['Authorization']) { // Checks for authorization header in request
  14.  
  15. var authKey = req.headers['Authorization']; // Bearer 4#fic890dwudhgcein...some fucking long auth token
  16.  
  17. var authSegments = authKey.split(' ');
  18.  
  19. var authSchema = authKey[0]; // Bearer
  20.  
  21. var accessToken = authKey[1]; // 4#fic890dwudhgcein...some fucking long pseudo code
  22.  
  23. var decodedData = UserRepo.crackToken(accessToken); // { username: 'jopa', password: 'jopa', ... }
  24.  
  25. req.currentUser = decodedData; // This is very important!!!! From this point further controller action can access `req.currentUser` to get currently autheticated User data
  26.  
  27. return next();
  28.  
  29. } else {
  30.  
  31. return res.forbidden(); // Do not allow request to hit controller action, return 403 FORBIDDEN to the client (angular)
  32.  
  33. }
  34. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement