Chatwoot/app/javascript/dashboard/store/modules/auth.js
Sojan Jose b7a583b2c4
Feature: Ability to switch between multiple accounts (#881)
* Feature: Ability to switch between multiple accounts

* Fix rubocop

* Fix assigned inboxes

* fix auth json

* Add account switcher in UI

* fix ordering on administrate

* Add switch accounts to sidebar

* add account id

* Fix schema.rb timestamp

* Revert "add account id"

This reverts commit 27570f50ef584cb9a5f69454f43f630b318c8807.

* Add a check for account

Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
2020-05-26 22:38:48 +05:30

148 lines
3.5 KiB
JavaScript

/* eslint no-param-reassign: 0 */
import axios from 'axios';
import moment from 'moment';
import Vue from 'vue';
import * as types from '../mutation-types';
import authAPI from '../../api/auth';
import createAxios from '../../helper/APIHelper';
import actionCable from '../../helper/actionCable';
import { setUser, getHeaderExpiry, clearCookiesOnLogout } from '../utils/api';
const state = {
currentUser: {
id: null,
account_id: null,
channel: null,
email: null,
name: null,
provider: null,
uid: null,
subscription: {
state: null,
expiry: null,
},
},
currentAccountId: null,
};
// getters
export const getters = {
isLoggedIn($state) {
return !!$state.currentUser.id;
},
getCurrentUserID(_state) {
return _state.currentUser.id;
},
getCurrentAccountId(_state) {
return _state.currentAccountId;
},
getCurrentRole(_state) {
const { accounts = [] } = _state.currentUser;
const [currentAccount = {}] = accounts.filter(
account => account.id === _state.currentAccountId
);
return currentAccount.role;
},
getCurrentUser(_state) {
return _state.currentUser;
},
getSubscription(_state) {
return _state.currentUser.subscription === undefined
? null
: _state.currentUser.subscription;
},
getTrialLeft(_state) {
const createdAt =
_state.currentUser.subscription === undefined
? moment()
: _state.currentUser.subscription.expiry * 1000;
const daysLeft = moment(createdAt).diff(moment(), 'days');
return daysLeft < 0 ? 0 : daysLeft;
},
};
// actions
export const actions = {
login({ commit }, credentials) {
return new Promise((resolve, reject) => {
authAPI
.login(credentials)
.then(() => {
commit(types.default.SET_CURRENT_USER);
window.axios = createAxios(axios);
actionCable.init(Vue);
window.location = '/';
resolve();
})
.catch(error => {
reject(error);
});
});
},
async validityCheck(context) {
try {
const response = await authAPI.validityCheck();
setUser(response.data.payload.data, getHeaderExpiry(response));
context.commit(types.default.SET_CURRENT_USER);
} catch (error) {
if (error.response.status === 401) {
clearCookiesOnLogout();
}
}
},
setUser({ commit, dispatch }) {
if (authAPI.isLoggedIn()) {
commit(types.default.SET_CURRENT_USER);
dispatch('validityCheck');
} else {
commit(types.default.CLEAR_USER);
}
},
logout({ commit }) {
commit(types.default.CLEAR_USER);
},
updateProfile: async ({ commit }, params) => {
try {
const response = await authAPI.profileUpdate(params);
setUser(response.data, getHeaderExpiry(response));
commit(types.default.SET_CURRENT_USER);
} catch (error) {
// Ignore error
}
},
setCurrentAccountId({ commit }, accountId) {
commit(types.default.SET_CURRENT_ACCOUNT_ID, accountId);
},
};
// mutations
const mutations = {
[types.default.CLEAR_USER](_state) {
_state.currentUser.id = null;
},
[types.default.SET_CURRENT_USER](_state) {
const currentUser = {
...authAPI.getAuthData(),
...authAPI.getCurrentUser(),
};
Vue.set(_state, 'currentUser', currentUser);
},
[types.default.SET_CURRENT_ACCOUNT_ID](_state, accountId) {
Vue.set(_state, 'currentAccountId', Number(accountId));
},
};
export default {
state,
getters,
actions,
mutations,
};