/
index.ts
79 lines (65 loc) · 1.96 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import passport from "@outlinewiki/koa-passport";
import { addMonths } from "date-fns";
import Koa from "koa";
import bodyParser from "koa-body";
import Router from "koa-router";
import { AuthenticationError } from "@server/errors";
import auth from "@server/middlewares/authentication";
import { defaultRateLimiter } from "@server/middlewares/rateLimiter";
import { Collection, Team, View } from "@server/models";
import providers from "./providers";
const app = new Koa();
const router = new Router();
router.use(passport.initialize());
router.use(defaultRateLimiter());
// dynamically load available authentication provider routes
providers.forEach((provider) => {
if (provider.enabled) {
router.use("/", provider.router.routes());
}
});
router.get("/redirect", auth(), async (ctx) => {
const { user } = ctx.state;
const jwtToken = user.getJwtToken();
if (jwtToken === ctx.params.token) {
throw AuthenticationError("Cannot extend token");
}
// ensure that the lastActiveAt on user is updated to prevent replay requests
await user.updateActiveAt(ctx, true);
ctx.cookies.set("accessToken", jwtToken, {
httpOnly: false,
sameSite: true,
expires: addMonths(new Date(), 3),
});
const [team, collection, view] = await Promise.all([
Team.findByPk(user.teamId),
Collection.findFirstCollectionForUser(user),
View.findOne({
where: {
userId: user.id,
},
}),
]);
const defaultCollectionId = team?.defaultCollectionId;
if (defaultCollectionId) {
const collection = await Collection.findOne({
where: {
id: defaultCollectionId,
teamId: team.id,
},
});
if (collection) {
ctx.redirect(`${team.url}${collection.url}`);
return;
}
}
const hasViewedDocuments = !!view;
ctx.redirect(
!hasViewedDocuments && collection
? `${team?.url}${collection.url}`
: `${team?.url}/home`
);
});
app.use(bodyParser());
app.use(router.routes());
export default app;