2017-05-30 02:08:03 +00:00
|
|
|
// @flow
|
2017-11-27 06:26:02 +00:00
|
|
|
import { observable, action, computed, autorun, runInAction } from 'mobx';
|
2017-05-30 02:08:03 +00:00
|
|
|
import invariant from 'invariant';
|
2017-10-11 07:14:45 +00:00
|
|
|
import Cookie from 'js-cookie';
|
2017-11-12 00:02:49 +00:00
|
|
|
import localForage from 'localforage';
|
2017-05-30 02:08:03 +00:00
|
|
|
import { client } from 'utils/ApiClient';
|
|
|
|
import type { User, Team } from 'types';
|
|
|
|
|
|
|
|
const AUTH_STORE = 'AUTH_STORE';
|
|
|
|
|
|
|
|
class AuthStore {
|
|
|
|
@observable user: ?User;
|
|
|
|
@observable team: ?Team;
|
|
|
|
@observable token: ?string;
|
2018-05-31 19:07:49 +00:00
|
|
|
@observable isSaving: boolean = false;
|
2017-05-30 02:08:03 +00:00
|
|
|
@observable isLoading: boolean = false;
|
2018-03-05 06:18:23 +00:00
|
|
|
@observable isSuspended: boolean = false;
|
2018-03-07 07:38:52 +00:00
|
|
|
@observable suspendedContactEmail: ?string;
|
2017-05-30 02:08:03 +00:00
|
|
|
|
|
|
|
/* Computed */
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get authenticated(): boolean {
|
2017-05-30 02:08:03 +00:00
|
|
|
return !!this.token;
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get asJson(): string {
|
2017-05-30 02:08:03 +00:00
|
|
|
return JSON.stringify({
|
|
|
|
user: this.user,
|
|
|
|
team: this.team,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:26:02 +00:00
|
|
|
@action
|
|
|
|
fetch = async () => {
|
|
|
|
try {
|
|
|
|
const res = await client.post('/auth.info');
|
|
|
|
invariant(res && res.data, 'Auth not available');
|
|
|
|
|
|
|
|
runInAction('AuthStore#fetch', () => {
|
|
|
|
this.user = res.data.user;
|
|
|
|
this.team = res.data.team;
|
|
|
|
});
|
2018-02-13 06:44:43 +00:00
|
|
|
} catch (err) {
|
2018-03-07 08:00:58 +00:00
|
|
|
if (err.error.error === 'user_suspended') {
|
2018-03-05 06:18:23 +00:00
|
|
|
this.isSuspended = true;
|
2018-03-07 08:00:58 +00:00
|
|
|
this.suspendedContactEmail = err.error.data.adminEmail;
|
2018-03-05 06:18:23 +00:00
|
|
|
}
|
2017-11-27 06:26:02 +00:00
|
|
|
}
|
|
|
|
};
|
2017-05-30 02:08:03 +00:00
|
|
|
|
2018-07-07 23:09:39 +00:00
|
|
|
@action
|
|
|
|
deleteUser = async () => {
|
|
|
|
await client.post(`/user.delete`, { confirmation: true });
|
|
|
|
|
|
|
|
runInAction('AuthStore#updateUser', () => {
|
|
|
|
this.user = null;
|
|
|
|
this.team = null;
|
|
|
|
this.token = null;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-31 18:42:39 +00:00
|
|
|
@action
|
2018-05-31 19:07:49 +00:00
|
|
|
updateUser = async (params: { name: string, avatarUrl: ?string }) => {
|
|
|
|
this.isSaving = true;
|
2018-05-31 18:42:39 +00:00
|
|
|
|
2018-05-31 19:07:49 +00:00
|
|
|
try {
|
|
|
|
const res = await client.post(`/user.update`, params);
|
|
|
|
invariant(res && res.data, 'User response not available');
|
|
|
|
|
|
|
|
runInAction('AuthStore#updateUser', () => {
|
|
|
|
this.user = res.data;
|
2018-05-31 19:44:32 +00:00
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
this.isSaving = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@action
|
2018-08-19 23:06:39 +00:00
|
|
|
updateTeam = async (params: {
|
|
|
|
name?: string,
|
|
|
|
avatarUrl?: ?string,
|
|
|
|
sharing?: boolean,
|
|
|
|
}) => {
|
2018-05-31 19:44:32 +00:00
|
|
|
this.isSaving = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await client.post(`/team.update`, params);
|
|
|
|
invariant(res && res.data, 'Team response not available');
|
|
|
|
|
|
|
|
runInAction('AuthStore#updateTeam', () => {
|
|
|
|
this.team = res.data;
|
2018-05-31 19:07:49 +00:00
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
this.isSaving = false;
|
|
|
|
}
|
2018-05-31 18:42:39 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2018-02-13 06:44:43 +00:00
|
|
|
logout = async () => {
|
2017-05-30 02:08:03 +00:00
|
|
|
this.user = null;
|
|
|
|
this.token = null;
|
2018-02-13 06:44:43 +00:00
|
|
|
|
2018-05-29 04:14:43 +00:00
|
|
|
Cookie.remove('accessToken', { path: '/' });
|
2018-02-13 06:44:43 +00:00
|
|
|
await localForage.clear();
|
2018-05-30 05:18:11 +00:00
|
|
|
|
|
|
|
// add a timestamp to force reload from server
|
|
|
|
window.location.href = `${BASE_URL}?done=${new Date().getTime()}`;
|
2017-05-30 02:08:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
// Rehydrate
|
2018-02-14 07:30:56 +00:00
|
|
|
let data = {};
|
|
|
|
try {
|
|
|
|
data = JSON.parse(localStorage.getItem(AUTH_STORE) || '{}');
|
|
|
|
} catch (_) {
|
|
|
|
// no-op Safari private mode
|
|
|
|
}
|
2017-05-30 02:08:03 +00:00
|
|
|
this.user = data.user;
|
|
|
|
this.team = data.team;
|
2018-05-29 05:32:36 +00:00
|
|
|
|
|
|
|
// load token from state for backwards compatability with
|
|
|
|
// sessions created pre-google auth
|
2018-05-29 04:14:43 +00:00
|
|
|
this.token = Cookie.get('accessToken') || data.token;
|
2017-05-30 02:08:03 +00:00
|
|
|
|
2018-05-30 05:18:11 +00:00
|
|
|
if (this.token) setImmediate(() => this.fetch());
|
|
|
|
|
2017-11-11 22:42:29 +00:00
|
|
|
autorun(() => {
|
2018-02-14 07:30:56 +00:00
|
|
|
try {
|
|
|
|
localStorage.setItem(AUTH_STORE, this.asJson);
|
|
|
|
} catch (_) {
|
|
|
|
// no-op Safari private mode
|
|
|
|
}
|
2017-05-30 02:08:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AuthStore;
|