2017-05-12 00:23:56 +00:00
|
|
|
// @flow
|
2016-06-26 06:31:22 +00:00
|
|
|
import { observable, action, computed } from 'mobx';
|
2017-05-12 00:23:56 +00:00
|
|
|
import invariant from 'invariant';
|
2016-06-04 22:53:08 +00:00
|
|
|
import { client } from 'utils/ApiClient';
|
2017-05-12 00:23:56 +00:00
|
|
|
import type { User, Team } from 'types';
|
2016-06-04 22:53:08 +00:00
|
|
|
|
|
|
|
const USER_STORE = 'USER_STORE';
|
|
|
|
|
2016-06-26 06:31:22 +00:00
|
|
|
class UserStore {
|
2017-05-12 00:23:56 +00:00
|
|
|
@observable user: ?User;
|
|
|
|
@observable team: ?Team;
|
2016-06-04 22:53:08 +00:00
|
|
|
|
2017-05-12 00:23:56 +00:00
|
|
|
@observable token: ?string;
|
|
|
|
@observable oauthState: string;
|
2016-06-04 22:53:08 +00:00
|
|
|
|
2017-05-12 00:23:56 +00:00
|
|
|
@observable isLoading: boolean = false;
|
2016-06-04 22:53:08 +00:00
|
|
|
|
|
|
|
/* Computed */
|
|
|
|
|
2017-05-12 00:23:56 +00:00
|
|
|
@computed get authenticated(): boolean {
|
2016-06-04 22:53:08 +00:00
|
|
|
return !!this.token;
|
|
|
|
}
|
|
|
|
|
2017-05-12 00:23:56 +00:00
|
|
|
@computed get asJson(): string {
|
2016-06-04 22:53:08 +00:00
|
|
|
return JSON.stringify({
|
|
|
|
user: this.user,
|
|
|
|
team: this.team,
|
|
|
|
token: this.token,
|
|
|
|
oauthState: this.oauthState,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Actions */
|
|
|
|
|
2017-05-17 07:11:13 +00:00
|
|
|
@action logout = (cb: Function) => {
|
2016-06-04 22:53:08 +00:00
|
|
|
this.user = null;
|
|
|
|
this.token = null;
|
2017-05-17 07:11:13 +00:00
|
|
|
cb();
|
2016-06-04 22:53:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
@action getOauthState = () => {
|
|
|
|
const state = Math.random().toString(36).substring(7);
|
|
|
|
this.oauthState = state;
|
|
|
|
return this.oauthState;
|
2017-04-27 04:47:03 +00:00
|
|
|
};
|
2016-06-04 22:53:08 +00:00
|
|
|
|
2017-05-17 07:11:13 +00:00
|
|
|
@action authWithSlack = async (code: string, state: string) => {
|
2016-06-04 22:53:08 +00:00
|
|
|
if (state !== this.oauthState) {
|
2017-05-17 07:11:13 +00:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
};
|
2016-06-04 22:53:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let res;
|
|
|
|
try {
|
2016-10-28 05:37:26 +00:00
|
|
|
res = await client.post('/auth.slack', { code });
|
2016-06-04 22:53:08 +00:00
|
|
|
} catch (e) {
|
2017-05-17 07:11:13 +00:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
};
|
2016-06-04 22:53:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 00:23:56 +00:00
|
|
|
invariant(
|
|
|
|
res && res.data && res.data.user && res.data.team && res.data.accessToken,
|
|
|
|
'All values should be available'
|
|
|
|
);
|
2016-06-04 22:53:08 +00:00
|
|
|
this.user = res.data.user;
|
|
|
|
this.team = res.data.team;
|
|
|
|
this.token = res.data.accessToken;
|
2017-05-17 07:11:13 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
};
|
2017-04-27 04:47:03 +00:00
|
|
|
};
|
2016-06-04 22:53:08 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
// Rehydrate
|
2016-07-15 07:26:34 +00:00
|
|
|
const data = JSON.parse(localStorage.getItem(USER_STORE) || '{}');
|
2016-06-04 22:53:08 +00:00
|
|
|
this.user = data.user;
|
|
|
|
this.team = data.team;
|
|
|
|
this.token = data.token;
|
|
|
|
this.oauthState = data.oauthState;
|
|
|
|
}
|
2016-10-28 05:37:26 +00:00
|
|
|
}
|
2016-06-04 22:53:08 +00:00
|
|
|
|
2016-06-26 06:31:22 +00:00
|
|
|
export default UserStore;
|
2017-04-27 04:47:03 +00:00
|
|
|
export { USER_STORE };
|