var { Actions } = require('flummox'); var Promise = require('bluebird'); var assign = require('object-assign'); class SessionActions extends Actions { constructor(api, flux) { super(); this.api = api; this.flux = flux; } fetchSession(username, password) { return this.api.auth.login(username, password).then(res => res.body); } login(username, password) { var fetchSession = this.fetchSession(username, password); var fetchUser = fetchSession.then(res => { // Normally you wouldn't have to worry about the access token but it is not set on // the api client until both fetchSession and fetchUser resolve. this.receiveSession(res.access_token, res.user_id); return this.flux.getActions('core.users').fetchUser(res.user_id); }); return Promise.join( fetchSession, fetchUser, (session, user) => assign({}, session, { user })); } logout() { // No payload. return true; } receiveSession(accessToken, userId) { return { accessToken, userId }; } } module.exports = SessionActions;