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

140 lines
3.2 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 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;
@observable isSuspended: boolean = false;
@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,
});
}
@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;
});
} catch (err) {
if (err.error.error === 'user_suspended') {
this.isSuspended = true;
this.suspendedContactEmail = err.error.data.adminEmail;
}
}
};
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
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;
Cookie.remove('accessToken', { path: '/' });
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
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;