Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const Boom = require('boom');
- const Stormpath = require('Stormpath');
- const Config = require('../../../srv/config/config');
- const prefix = Config.api.prefix + '/' + Config.api.version;
- const _ = require('lodash');
- exports.register = (server, options, next) => {
- const UserService = options.userService
- server.route([{
- path: '/users',
- method: 'GET',
- config: {
- auth: false,
- tags: ['api'],
- handler: function(request, reply) {
- UserService.getAccounts({
- expand: "groups"
- }, function(err, accounts) {
- //console.log('accounts', accounts);
- reply(accounts).code(200);
- // accounts.each(function(account, cb) {
- // console.log('account:', account);
- // cb()
- // }, (err) => {
- // console.log('finished iterating over accounts');
- // });
- });
- }
- }
- }, {
- path: '/users/login',
- method: 'POST',
- config: {
- auth: false,
- handler: function(request, reply) {
- console.log('payload', request.payload);
- var authenticator = new Stormpath.OAuthAuthenticator(UserService);
- UserService.authenticateAccount({
- username: request.payload.username,
- password: request.payload.password,
- accountStore: {
- nameKey: request.payload.accountStore
- }
- }, function(err, result) {
- if (err) throw err;
- let roles = [];
- let permissions = [];
- result.getAccount(function(err, account) {
- if (err) throw err;
- account.getGroups({
- expand: 'customData'
- }, (err, groups) => {
- groups.items.forEach((group) => {
- permissions = _.concat(permissions, group.customData.permissions);
- roles.push(group.name);
- });
- var jwt = result.getJwt();
- permissions = _.map(permissions).join(' ');
- jwt.body.scope = permissions
- let accessToken = jwt.compact();
- reply({
- account: account,
- roles: roles,
- permissions: permissions,
- groups: groups,
- token: accessToken
- });
- });
- });
- });
- }
- }
- }
- ]);
- next();
- };
- exports.register.attributes = {
- name: 'user-routes',
- version: '0.1.0'
- dependencies: 'stormpath-auth'
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement