Guest User

Untitled

a guest
Jan 26th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import DefineMap from 'can-define/map/map';
  2. import feathers from 'feathers/client';
  3. import io from 'steal-socket.io';
  4. import socketio from 'feathers-socketio/client';
  5. import auth from 'feathers-authentication-client';
  6. import hooks from 'feathers-hooks';
  7. import Debug from '~/utils/debug';
  8. import loader from '@loader';
  9.  
  10. const socket = io(loader.serviceBaseURL, {
  11. transports: ['websocket']
  12. });
  13.  
  14. export const FeathersModel = DefineMap.extend({ seal: false },{
  15. init () {
  16. this.debug = new Debug(window, {debug: false, control: 'FeathersClient'});
  17. },
  18. key: {
  19. value () {
  20. return 'feathers-jwt';
  21. }
  22. },
  23. feathersClient: {
  24. type: 'function',
  25. value () {
  26. return feathers()
  27. .configure(socketio(socket))
  28. .configure(hooks())
  29. .configure(auth({
  30. storageKey: this.key,
  31. storage: window.localStorage
  32. }));
  33. }
  34. },
  35. auth (email, pass) {
  36. let token,
  37. strat = undefined;
  38.  
  39. if (email !== undefined && pass !== undefined) {
  40. strat = {
  41. strategy: 'local',
  42. email: email,
  43. password: pass
  44. };
  45. }
  46.  
  47. return this.feathersClient.authenticate(strat)
  48. .then(response => {
  49. token = response.accessToken;
  50. return this.feathersClient.passport.verifyJWT(token);
  51. })
  52. .then(payload => {
  53. return this.feathersClient.service('users').get(payload.userId);
  54. })
  55. .then(user => {
  56. this.feathersClient.set('user', user);
  57. return (email === undefined) ? this.feathersClient.get('user') : token;
  58. })
  59. .catch(err => {
  60. return err;
  61. });
  62. }
  63. });
  64.  
  65. export default FeathersModel;
Add Comment
Please, Sign In to add comment