Merge pull request #454 from outline/issue-448

Request auth on first load with existing token
This commit is contained in:
Jori Lallo
2017-11-28 23:00:00 -08:00
committed by GitHub
5 changed files with 50 additions and 15 deletions

View File

@@ -50,9 +50,8 @@ type AuthProps = {
const Auth = ({ children }: AuthProps) => {
if (stores.auth.authenticated && stores.auth.team && stores.auth.user) {
// Only initialize stores once. Kept in global scope
// because otherwise they will get overriden on route
// change
// Only initialize stores once. Kept in global scope because otherwise they
// will get overridden on route change
if (!authenticatedStores) {
// Stores for authenticated user
const { user, team } = stores.auth;
@@ -79,6 +78,7 @@ const Auth = ({ children }: AuthProps) => {
};
}
stores.auth.fetch();
authenticatedStores.collections.fetchAll();
}

View File

@@ -1,5 +1,5 @@
// @flow
import { observable, action, computed, autorun } from 'mobx';
import { observable, action, computed, autorun, runInAction } from 'mobx';
import invariant from 'invariant';
import Cookie from 'js-cookie';
import localForage from 'localforage';
@@ -32,7 +32,20 @@ class AuthStore {
});
}
/* Actions */
@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 (e) {
// Failure to update user info is a non-fatal error.
}
};
@action
logout = () => {

View File

@@ -32,7 +32,7 @@ class ApiClient {
modifiedPath = path;
}
} else if (method === 'POST' || method === 'PUT') {
body = JSON.stringify(data);
body = data ? JSON.stringify(data) : undefined;
}
// Construct headers