Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import DefineMap from 'can-define/map/map';
- import feathers from 'feathers/client';
- import io from 'steal-socket.io';
- import socketio from 'feathers-socketio/client';
- import auth from 'feathers-authentication-client';
- import hooks from 'feathers-hooks';
- import Debug from '~/utils/debug';
- import loader from '@loader';
- const socket = io(loader.serviceBaseURL, {
- transports: ['websocket']
- });
- export const FeathersModel = DefineMap.extend({ seal: false },{
- init () {
- this.debug = new Debug(window, {debug: false, control: 'FeathersClient'});
- },
- key: {
- value () {
- return 'feathers-jwt';
- }
- },
- feathersClient: {
- type: 'function',
- value () {
- return feathers()
- .configure(socketio(socket))
- .configure(hooks())
- .configure(auth({
- storageKey: this.key,
- storage: window.localStorage
- }));
- }
- },
- auth (email, pass) {
- let token,
- strat = undefined;
- if (email !== undefined && pass !== undefined) {
- strat = {
- strategy: 'local',
- email: email,
- password: pass
- };
- }
- return this.feathersClient.authenticate(strat)
- .then(response => {
- token = response.accessToken;
- return this.feathersClient.passport.verifyJWT(token);
- })
- .then(payload => {
- return this.feathersClient.service('users').get(payload.userId);
- })
- .then(user => {
- this.feathersClient.set('user', user);
- return (email === undefined) ? this.feathersClient.get('user') : token;
- })
- .catch(err => {
- return err;
- });
- }
- });
- export default FeathersModel;
Add Comment
Please, Sign In to add comment