Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const Boom = require('boom');
  4. const Stormpath = require('Stormpath');
  5. const Config = require('../../../srv/config/config');
  6. const prefix = Config.api.prefix + '/' + Config.api.version;
  7. const _ = require('lodash');
  8.  
  9. exports.register = (server, options, next) => {
  10.  
  11. const UserService = options.userService
  12.  
  13. server.route([{
  14. path: '/users',
  15. method: 'GET',
  16. config: {
  17. auth: false,
  18. tags: ['api'],
  19. handler: function(request, reply) {
  20.  
  21. UserService.getAccounts({
  22. expand: "groups"
  23. }, function(err, accounts) {
  24. //console.log('accounts', accounts);
  25.  
  26. reply(accounts).code(200);
  27.  
  28. // accounts.each(function(account, cb) {
  29. // console.log('account:', account);
  30. // cb()
  31. // }, (err) => {
  32. // console.log('finished iterating over accounts');
  33. // });
  34. });
  35.  
  36. }
  37. }
  38. }, {
  39. path: '/users/login',
  40. method: 'POST',
  41. config: {
  42. auth: false,
  43. handler: function(request, reply) {
  44.  
  45. console.log('payload', request.payload);
  46.  
  47.  
  48. var authenticator = new Stormpath.OAuthAuthenticator(UserService);
  49.  
  50. UserService.authenticateAccount({
  51. username: request.payload.username,
  52. password: request.payload.password,
  53. accountStore: {
  54. nameKey: request.payload.accountStore
  55. }
  56. }, function(err, result) {
  57.  
  58. if (err) throw err;
  59.  
  60. let roles = [];
  61. let permissions = [];
  62.  
  63. result.getAccount(function(err, account) {
  64. if (err) throw err;
  65.  
  66. account.getGroups({
  67. expand: 'customData'
  68. }, (err, groups) => {
  69. groups.items.forEach((group) => {
  70.  
  71. permissions = _.concat(permissions, group.customData.permissions);
  72. roles.push(group.name);
  73. });
  74.  
  75. var jwt = result.getJwt();
  76. permissions = _.map(permissions).join(' ');
  77. jwt.body.scope = permissions
  78. let accessToken = jwt.compact();
  79.  
  80. reply({
  81. account: account,
  82. roles: roles,
  83. permissions: permissions,
  84. groups: groups,
  85. token: accessToken
  86. });
  87. });
  88.  
  89. });
  90.  
  91. });
  92.  
  93. }
  94. }
  95. }
  96.  
  97.  
  98. ]);
  99.  
  100. next();
  101. };
  102.  
  103. exports.register.attributes = {
  104. name: 'user-routes',
  105. version: '0.1.0'
  106. dependencies: 'stormpath-auth'
  107. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement