Added cookie to indicate logged in status

This commit is contained in:
Jori Lallo
2017-10-11 00:14:45 -07:00
parent 4dacef7f1e
commit 42ed9616b5
6 changed files with 48 additions and 21 deletions

View File

@ -1,6 +1,7 @@
// @flow
import { observable, action, computed, autorunAsync } from 'mobx';
import invariant from 'invariant';
import Cookie from 'js-cookie';
import { client } from 'utils/ApiClient';
import type { User, Team } from 'types';
@ -33,6 +34,7 @@ class AuthStore {
@action logout = () => {
this.user = null;
this.token = null;
Cookie.remove('loggedId', { path: '/' });
};
@action getOauthState = () => {

View File

@ -52,6 +52,7 @@ class ApiClient {
body,
headers,
redirect: 'follow',
credentials: 'include',
});
// Handle request promises and return a new promise

View File

@ -77,7 +77,7 @@
"babel-preset-react": "6.11.1",
"babel-preset-react-hmre": "1.1.1",
"babel-regenerator-runtime": "6.5.0",
"bcrypt": "^0.8.7",
"bcrypt": "1.0.3",
"boundless-arrow-key-navigation": "^1.0.4",
"boundless-popover": "^1.0.4",
"bugsnag": "^1.7.0",
@ -107,6 +107,7 @@
"imports-loader": "0.6.5",
"invariant": "^2.2.2",
"isomorphic-fetch": "2.2.1",
"js-cookie": "^2.1.4",
"js-search": "^1.4.2",
"js-tree": "1.1.0",
"json-loader": "0.5.4",

View File

@ -47,6 +47,14 @@ router.post('auth.slack', async ctx => {
await team.createFirstCollection(user.id);
}
// Signal to backend that the user is logged in.
// This is only used to signal SSR rendering, not
// used for auth.
ctx.cookies.set('loggedId', 'true', {
httpOnly: false,
expires: new Date('2100'),
});
ctx.body = {
data: {
user: await presentUser(ctx, user),

View File

@ -1,21 +1,22 @@
import path from 'path';
import fs from 'fs';
import httpErrors from 'http-errors';
import Koa from 'koa';
import Router from 'koa-router';
import sendfile from 'koa-sendfile';
import subdomainRedirect from './middlewares/subdomainRedirect';
const koa = new Koa();
const router = new Router();
router.get('/service-worker.js', async ctx => {
ctx.set('Content-Type', 'application/javascript');
if (process.env.NODE_ENV === 'production')
ctx.set('Cache-Control', `max-age=${30}`);
await sendfile(ctx, path.join(__dirname, './static/service-worker.js'));
if (!ctx.status) ctx.throw(httpErrors.NotFound());
});
const readFile = src => {
return new Promise((resolve, reject) => {
fs.readFile(src, { encoding: 'utf8' }, (err, data) => {
if (err) return reject(err);
resolve(data);
});
});
};
router.get('/_health', ctx => (ctx.body = 'OK'));
@ -31,6 +32,13 @@ if (process.env.NODE_ENV === 'production') {
);
});
router.get('/', async ctx => {
const html = await readFile(path.join(__dirname, '../dist/index.html'));
ctx.body = html;
if (!ctx.status) ctx.throw(httpErrors.NotFound());
});
router.get('*', async ctx => {
await sendfile(ctx, path.join(__dirname, '../dist/index.html'));
if (!ctx.status) ctx.throw(httpErrors.NotFound());
@ -39,7 +47,10 @@ if (process.env.NODE_ENV === 'production') {
koa.use(subdomainRedirect());
} else {
router.get('*', async ctx => {
await sendfile(ctx, path.join(__dirname, './static/dev.html'));
console.log(ctx.cookies.get('loggedIn'));
const html = await readFile(path.join(__dirname, './static/dev.html'));
ctx.body = html;
if (!ctx.status) ctx.throw(httpErrors.NotFound());
});
}

View File

@ -1041,12 +1041,12 @@ bcrypt-pbkdf@^1.0.0:
dependencies:
tweetnacl "^0.14.3"
bcrypt@^0.8.7:
version "0.8.7"
resolved "https://registry.yarnpkg.com/bcrypt/-/bcrypt-0.8.7.tgz#bc3875a9afd0a7b2cd231a6a7f218a5ce156b093"
bcrypt@1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/bcrypt/-/bcrypt-1.0.3.tgz#b02ddc6c0b52ea16b8d3cf375d5a32e780dab548"
dependencies:
bindings "1.2.1"
nan "2.3.5"
nan "2.6.2"
node-pre-gyp "0.6.36"
beeper@^1.0.0:
version "1.1.1"
@ -1067,10 +1067,6 @@ binary-extensions@^1.0.0:
buffers "~0.1.1"
chainsaw "~0.1.0"
bindings@1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.2.1.tgz#14ad6113812d2d37d72e67b4cacb4bb726505f11"
block-stream@*:
version "0.0.9"
resolved "https://registry.yarnpkg.com/block-stream/-/block-stream-0.0.9.tgz#13ebfe778a03205cfe03751481ebb4b3300c126a"
@ -4858,6 +4854,10 @@ js-beautify@^1.6.11:
mkdirp "~0.5.0"
nopt "~3.0.1"
js-cookie@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/js-cookie/-/js-cookie-2.1.4.tgz#da4ec503866f149d164cf25f579ef31015025d8d"
js-search@^1.4.2:
version "1.4.2"
resolved "https://registry.yarnpkg.com/js-search/-/js-search-1.4.2.tgz#59a91e117d6badb20bf0d7643ba7577d5a81d7e2"
@ -6030,7 +6030,11 @@ mz@2:
object-assign "^4.0.1"
thenify-all "^1.0.0"
nan@2.3.5, nan@^2.3.0:
nan@2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.6.2.tgz#e4ff34e6c95fdfb5aecc08de6596f43605a7db45"
nan@^2.3.0:
version "2.3.5"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.3.5.tgz#822a0dc266290ce4cd3a12282ca3e7e364668a08"
@ -6185,7 +6189,7 @@ node-notifier@^5.0.2:
shellwords "^0.1.0"
which "^1.2.12"
node-pre-gyp@^0.6.36:
node-pre-gyp@0.6.36, node-pre-gyp@^0.6.36:
version "0.6.36"
resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.6.36.tgz#db604112cb74e0d477554e9b505b17abddfab786"
dependencies: