-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
session.ts
101 lines (86 loc) · 2.35 KB
/
session.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* SPDX-FileCopyrightText: 2016-present Kriasoft <hello@kriasoft.com> */
/* SPDX-License-Identifier: MIT */
import cookie from "cookie";
import { Request, RequestHandler, Response } from "express";
import jwt from "jsonwebtoken";
import type { User } from "../db";
import db from "../db";
import env from "../env";
// The name of the session (ID) cookie.
const cookieName = env.isProduction
? "id"
: `id_${env.APP_NAME?.replace(/^W/g, "")}`;
async function getUser(req: Request): Promise<User | null> {
const cookies = cookie.parse(req.headers.cookie || "");
const sessionCookie = cookies[cookieName];
if (sessionCookie) {
try {
const token = jwt.verify(sessionCookie, env.JWT_SECRET, {
issuer: env.APP_ORIGIN,
audience: env.APP_NAME,
}) as { sub: string };
const user = await db.table("user").where({ id: token.sub }).first();
return user || null;
} catch (err) {
console.error(err);
}
}
return null;
}
async function signIn(
req: Request,
res: Response,
user: User | null | undefined
): Promise<User | null> {
if (!user) {
return null;
}
[user] = await db
.table<User>("user")
.where({ id: user.id })
.update({ last_login: db.fn.now() })
.returning("*");
if (!user) {
req.user = null;
return null;
}
const sessionCookie = jwt.sign({}, env.JWT_SECRET, {
issuer: env.APP_ORIGIN,
audience: env.APP_NAME,
subject: String(user.id),
expiresIn: env.JWT_EXPIRES,
});
res.setHeader(
"Set-Cookie",
cookie.serialize(cookieName, sessionCookie, {
httpOnly: true,
maxAge: env.JWT_EXPIRES,
secure: env.isProduction,
path: "/",
})
);
return (req.user = user);
}
function signOut(req: Request, res: Response): void {
req.user = null;
res.clearCookie(cookieName);
}
const session: RequestHandler = async function session(req, res, next) {
try {
req.user = await getUser(req);
req.signIn = signIn.bind(undefined, req, res);
req.signOut = signOut.bind(undefined, req, res);
// In some cases it might be useful to ensure that the API
// request fails when the user was not authenticated.
if (req.query.authorize !== undefined && !req.user) {
res.status(401);
res.end();
} else {
next();
}
} catch (err) {
console.error(err);
next();
}
};
export default session;