Request auth on first load with existing token
This commit is contained in:
@ -50,9 +50,8 @@ type AuthProps = {
|
|||||||
|
|
||||||
const Auth = ({ children }: AuthProps) => {
|
const Auth = ({ children }: AuthProps) => {
|
||||||
if (stores.auth.authenticated && stores.auth.team && stores.auth.user) {
|
if (stores.auth.authenticated && stores.auth.team && stores.auth.user) {
|
||||||
// Only initialize stores once. Kept in global scope
|
// Only initialize stores once. Kept in global scope because otherwise they
|
||||||
// because otherwise they will get overriden on route
|
// will get overridden on route change
|
||||||
// change
|
|
||||||
if (!authenticatedStores) {
|
if (!authenticatedStores) {
|
||||||
// Stores for authenticated user
|
// Stores for authenticated user
|
||||||
const { user, team } = stores.auth;
|
const { user, team } = stores.auth;
|
||||||
@ -79,6 +78,7 @@ const Auth = ({ children }: AuthProps) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stores.auth.fetch();
|
||||||
authenticatedStores.collections.fetchAll();
|
authenticatedStores.collections.fetchAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import { observable, action, computed, autorun } from 'mobx';
|
import { observable, action, computed, autorun, runInAction } from 'mobx';
|
||||||
import invariant from 'invariant';
|
import invariant from 'invariant';
|
||||||
import Cookie from 'js-cookie';
|
import Cookie from 'js-cookie';
|
||||||
import localForage from 'localforage';
|
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
|
@action
|
||||||
logout = () => {
|
logout = () => {
|
||||||
|
@ -32,7 +32,7 @@ class ApiClient {
|
|||||||
modifiedPath = path;
|
modifiedPath = path;
|
||||||
}
|
}
|
||||||
} else if (method === 'POST' || method === 'PUT') {
|
} else if (method === 'POST' || method === 'PUT') {
|
||||||
body = JSON.stringify(data);
|
body = data ? JSON.stringify(data) : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct headers
|
// Construct headers
|
||||||
|
@ -1,11 +1,24 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import Router from 'koa-router';
|
import Router from 'koa-router';
|
||||||
|
import auth from './middlewares/authentication';
|
||||||
import { presentUser, presentTeam } from '../presenters';
|
import { presentUser, presentTeam } from '../presenters';
|
||||||
import { User, Team } from '../models';
|
import { User, Team } from '../models';
|
||||||
import * as Slack from '../slack';
|
import * as Slack from '../slack';
|
||||||
|
|
||||||
const router = new Router();
|
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 => {
|
router.post('auth.slack', async ctx => {
|
||||||
const { code } = ctx.body;
|
const { code } = ctx.body;
|
||||||
ctx.assertPresent(code, 'code is required');
|
ctx.assertPresent(code, 'code is required');
|
||||||
|
@ -159,9 +159,18 @@ export default function Pricing() {
|
|||||||
|
|
||||||
<h2>Methods</h2>
|
<h2>Methods</h2>
|
||||||
<Methods>
|
<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">
|
<Method method="user.info" label="Get current user">
|
||||||
<Description>
|
<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>
|
</Description>
|
||||||
<Arguments>
|
<Arguments>
|
||||||
<Argument id="id" description="Collection id" required />
|
<Argument id="id" description="Collection id" required />
|
||||||
@ -254,7 +263,7 @@ export default function Pricing() {
|
|||||||
<Arguments pagination>
|
<Arguments pagination>
|
||||||
<Argument
|
<Argument
|
||||||
id="collection"
|
id="collection"
|
||||||
description="Collection id to filter by"
|
description="Collection ID to filter by"
|
||||||
/>
|
/>
|
||||||
</Arguments>
|
</Arguments>
|
||||||
</Method>
|
</Method>
|
||||||
@ -278,7 +287,7 @@ export default function Pricing() {
|
|||||||
<Arguments>
|
<Arguments>
|
||||||
<Argument
|
<Argument
|
||||||
id="id"
|
id="id"
|
||||||
description="Document id or URI identifier"
|
description="Document ID or URI identifier"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</Arguments>
|
</Arguments>
|
||||||
@ -340,7 +349,7 @@ export default function Pricing() {
|
|||||||
<Arguments>
|
<Arguments>
|
||||||
<Argument
|
<Argument
|
||||||
id="id"
|
id="id"
|
||||||
description="Document id or URI identifier"
|
description="Document ID or URI identifier"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
<Argument id="title" description="Title for the document" />
|
<Argument id="title" description="Title for the document" />
|
||||||
@ -361,7 +370,7 @@ export default function Pricing() {
|
|||||||
<Arguments>
|
<Arguments>
|
||||||
<Argument
|
<Argument
|
||||||
id="id"
|
id="id"
|
||||||
description="Document id or URI identifier"
|
description="Document ID or URI identifier"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
<Argument
|
<Argument
|
||||||
@ -379,7 +388,7 @@ export default function Pricing() {
|
|||||||
<Arguments>
|
<Arguments>
|
||||||
<Argument
|
<Argument
|
||||||
id="id"
|
id="id"
|
||||||
description="Document id or URI identifier"
|
description="Document ID or URI identifier"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</Arguments>
|
</Arguments>
|
||||||
@ -393,7 +402,7 @@ export default function Pricing() {
|
|||||||
<Arguments>
|
<Arguments>
|
||||||
<Argument
|
<Argument
|
||||||
id="id"
|
id="id"
|
||||||
description="Document id or URI identifier"
|
description="Document ID or URI identifier"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</Arguments>
|
</Arguments>
|
||||||
@ -406,7 +415,7 @@ export default function Pricing() {
|
|||||||
<Arguments>
|
<Arguments>
|
||||||
<Argument
|
<Argument
|
||||||
id="id"
|
id="id"
|
||||||
description="Document id or URI identifier"
|
description="Document ID or URI identifier"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</Arguments>
|
</Arguments>
|
||||||
@ -419,7 +428,7 @@ export default function Pricing() {
|
|||||||
<Arguments>
|
<Arguments>
|
||||||
<Argument
|
<Argument
|
||||||
id="id"
|
id="id"
|
||||||
description="Document id or URI identifier"
|
description="Document ID or URI identifier"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</Arguments>
|
</Arguments>
|
||||||
|
Reference in New Issue
Block a user