2018-05-28 18:36:37 +00:00
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
import bodyParser from "koa-bodyparser";
|
|
|
|
import Koa from "koa";
|
|
|
|
import Router from "koa-router";
|
2020-07-18 18:02:40 +00:00
|
|
|
import addMonths from "date-fns/add_months";
|
2020-06-20 20:59:15 +00:00
|
|
|
import validation from "../middlewares/validation";
|
|
|
|
import auth from "../middlewares/authentication";
|
2020-07-18 18:02:40 +00:00
|
|
|
import { getCookieDomain } from "../utils/domains";
|
2020-06-20 20:59:15 +00:00
|
|
|
import { Team } from "../models";
|
|
|
|
|
|
|
|
import slack from "./slack";
|
|
|
|
import google from "./google";
|
|
|
|
import email from "./email";
|
2018-05-28 18:36:37 +00:00
|
|
|
|
2018-11-11 22:23:31 +00:00
|
|
|
const app = new Koa();
|
2018-05-28 18:36:37 +00:00
|
|
|
const router = new Router();
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
router.use("/", slack.routes());
|
|
|
|
router.use("/", google.routes());
|
|
|
|
router.use("/", email.routes());
|
2018-05-28 18:36:37 +00:00
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
router.get("/redirect", auth(), async ctx => {
|
2018-11-11 22:23:31 +00:00
|
|
|
const user = ctx.state.user;
|
2018-05-28 18:36:37 +00:00
|
|
|
|
2018-11-12 00:24:05 +00:00
|
|
|
// transfer access token cookie from root to subdomain
|
2020-06-20 20:59:15 +00:00
|
|
|
const rootToken = ctx.cookies.get("accessToken");
|
2020-05-22 04:42:46 +00:00
|
|
|
const jwtToken = user.getJwtToken();
|
|
|
|
|
|
|
|
if (rootToken === jwtToken) {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.cookies.set("accessToken", undefined, {
|
2020-05-22 04:42:46 +00:00
|
|
|
httpOnly: true,
|
|
|
|
domain: getCookieDomain(ctx.request.hostname),
|
|
|
|
});
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.cookies.set("accessToken", jwtToken, {
|
2020-05-22 04:42:46 +00:00
|
|
|
httpOnly: false,
|
|
|
|
expires: addMonths(new Date(), 3),
|
|
|
|
});
|
|
|
|
}
|
2018-11-11 22:23:31 +00:00
|
|
|
|
2019-06-23 22:49:45 +00:00
|
|
|
const team = await Team.findByPk(user.teamId);
|
2020-02-27 05:10:20 +00:00
|
|
|
ctx.redirect(`${team.url}/home`);
|
2018-11-11 22:23:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.use(bodyParser());
|
|
|
|
app.use(validation());
|
|
|
|
app.use(router.routes());
|
|
|
|
|
|
|
|
export default app;
|