This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/app/stores/AuthStore.js

159 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-05-30 02:08:03 +00:00
// @flow
import { observable, action, computed, autorun, runInAction } from 'mobx';
2017-05-30 02:08:03 +00:00
import invariant from 'invariant';
import { getCookie, setCookie, removeCookie } from 'tiny-cookie';
2017-05-30 02:08:03 +00:00
import { client } from 'utils/ApiClient';
2018-11-10 07:40:33 +00:00
import { stripSubdomain } from 'shared/utils/domains';
import RootStore from 'stores/RootStore';
import User from 'models/User';
import Team from 'models/Team';
2017-05-30 02:08:03 +00:00
const AUTH_STORE = 'AUTH_STORE';
export default class AuthStore {
2017-05-30 02:08:03 +00:00
@observable user: ?User;
@observable team: ?Team;
@observable token: ?string;
2018-05-31 19:07:49 +00:00
@observable isSaving: boolean = false;
@observable isSuspended: boolean = false;
@observable suspendedContactEmail: ?string;
rootStore: RootStore;
2017-05-30 02:08:03 +00:00
constructor(rootStore: RootStore) {
// Rehydrate
let data = {};
try {
data = JSON.parse(localStorage.getItem(AUTH_STORE) || '{}');
} catch (_) {
// no-op Safari private mode
}
this.rootStore = rootStore;
this.user = data.user;
this.team = data.team;
this.token = getCookie('accessToken');
if (this.token) setImmediate(() => this.fetch());
autorun(() => {
try {
localStorage.setItem(AUTH_STORE, this.asJson);
} catch (_) {
// no-op Safari private mode
}
});
}
2017-05-30 02:08:03 +00:00
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,
});
}
@action
fetch = async () => {
try {
const res = await client.post('/auth.info');
invariant(res && res.data, 'Auth not available');
runInAction('AuthStore#fetch', () => {
const { user, team } = res.data;
this.user = user;
this.team = team;
if (window.Bugsnag) {
Bugsnag.user = {
id: user.id,
name: user.name,
teamId: team.id,
team: team.name,
};
}
});
} catch (err) {
if (err.error === 'user_suspended') {
this.isSuspended = true;
this.suspendedContactEmail = err.data.adminEmail;
}
}
};
2017-05-30 02:08:03 +00:00
2018-07-07 23:09:39 +00:00
@action
deleteUser = async () => {
await client.post(`/users.delete`, { confirmation: true });
2018-07-07 23:09:39 +00:00
runInAction('AuthStore#updateUser', () => {
this.user = null;
this.team = null;
this.token = null;
});
};
2018-05-31 18:42:39 +00:00
@action
updateUser = async (params: { name?: string, avatarUrl: ?string }) => {
2018-05-31 19:07:49 +00:00
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(`/users.update`, params);
2018-05-31 19:07:49 +00:00
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
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
logout = async () => {
2017-05-30 02:08:03 +00:00
this.user = null;
this.token = null;
2018-11-11 22:23:31 +00:00
// remove authentication token itself
removeCookie('accessToken', { path: '/' });
2018-11-10 07:40:33 +00:00
2018-11-11 22:23:31 +00:00
// remove session record on apex cookie
const team = this.team;
if (team) {
const sessions = JSON.parse(getCookie('sessions') || '{}');
delete sessions[team.id];
2018-11-10 07:40:33 +00:00
setCookie('sessions', JSON.stringify(sessions), {
2018-11-10 07:40:33 +00:00
domain: stripSubdomain(window.location.hostname),
});
this.team = null;
}
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
};
}