diff --git a/app/index.js b/app/index.js index 4b1a1084c..76f7f71f8 100644 --- a/app/index.js +++ b/app/index.js @@ -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(); } diff --git a/app/stores/AuthStore.js b/app/stores/AuthStore.js index 6ee2d9a38..e5b41bee7 100644 --- a/app/stores/AuthStore.js +++ b/app/stores/AuthStore.js @@ -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 = () => { diff --git a/app/utils/ApiClient.js b/app/utils/ApiClient.js index ab1567128..08789da23 100644 --- a/app/utils/ApiClient.js +++ b/app/utils/ApiClient.js @@ -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 diff --git a/server/api/auth.js b/server/api/auth.js index 01bc6f1ad..223f4d2ed 100644 --- a/server/api/auth.js +++ b/server/api/auth.js @@ -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'); diff --git a/server/pages/Api.js b/server/pages/Api.js index b546f2520..af5e2d883 100644 --- a/server/pages/Api.js +++ b/server/pages/Api.js @@ -159,9 +159,18 @@ export default function Pricing() {

Methods

+ + + This method returns the user and team info for the user identified + by the token. + + + + - This method returns the information for currently logged in user. + This method returns the profile info for the user identified by + the token. @@ -254,7 +263,7 @@ export default function Pricing() { @@ -278,7 +287,7 @@ export default function Pricing() { @@ -340,7 +349,7 @@ export default function Pricing() { @@ -361,7 +370,7 @@ export default function Pricing() { @@ -393,7 +402,7 @@ export default function Pricing() { @@ -406,7 +415,7 @@ export default function Pricing() { @@ -419,7 +428,7 @@ export default function Pricing() {