Request auth on first load with existing token

This commit is contained in:
Tom Moor 2017-11-26 22:26:02 -08:00
parent bd8829d057
commit a573c76512
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

View File

@ -1,11 +1,24 @@
// @flow
import Router from 'koa-router';
import auth from './middlewares/authentication';
import { presentUser, presentTeam } from '../presenters';
import { User, Team } from '../models';
import * as Slack from '../slack';
const router = new Router();
router.post('auth.info', auth(), async ctx => {
const user = ctx.state.user;
const team = await Team.findOne({ where: { id: user.teamId } });
ctx.body = {
data: {
user: await presentUser(ctx, user),
team: await presentTeam(ctx, team),
},
};
});
router.post('auth.slack', async ctx => {
const { code } = ctx.body;
ctx.assertPresent(code, 'code is required');

View File

@ -159,9 +159,18 @@ export default function Pricing() {
<h2>Methods</h2>
<Methods>
<Method method="auth.info" label="Get current auth">
<Description>
This method returns the user and team info for the user identified
by the token.
</Description>
<Arguments />
</Method>
<Method method="user.info" label="Get current user">
<Description>
This method returns the information for currently logged in user.
This method returns the profile info for the user identified by
the token.
</Description>
<Arguments>
<Argument id="id" description="Collection id" required />
@ -254,7 +263,7 @@ export default function Pricing() {
<Arguments pagination>
<Argument
id="collection"
description="Collection id to filter by"
description="Collection ID to filter by"
/>
</Arguments>
</Method>
@ -278,7 +287,7 @@ export default function Pricing() {
<Arguments>
<Argument
id="id"
description="Document id or URI identifier"
description="Document ID or URI identifier"
required
/>
</Arguments>
@ -340,7 +349,7 @@ export default function Pricing() {
<Arguments>
<Argument
id="id"
description="Document id or URI identifier"
description="Document ID or URI identifier"
required
/>
<Argument id="title" description="Title for the document" />
@ -361,7 +370,7 @@ export default function Pricing() {
<Arguments>
<Argument
id="id"
description="Document id or URI identifier"
description="Document ID or URI identifier"
required
/>
<Argument
@ -379,7 +388,7 @@ export default function Pricing() {
<Arguments>
<Argument
id="id"
description="Document id or URI identifier"
description="Document ID or URI identifier"
required
/>
</Arguments>
@ -393,7 +402,7 @@ export default function Pricing() {
<Arguments>
<Argument
id="id"
description="Document id or URI identifier"
description="Document ID or URI identifier"
required
/>
</Arguments>
@ -406,7 +415,7 @@ export default function Pricing() {
<Arguments>
<Argument
id="id"
description="Document id or URI identifier"
description="Document ID or URI identifier"
required
/>
</Arguments>
@ -419,7 +428,7 @@ export default function Pricing() {
<Arguments>
<Argument
id="id"
description="Document id or URI identifier"
description="Document ID or URI identifier"
required
/>
</Arguments>