Newer
Older
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
import { Router } from "express";
import { prisma } from "./lib/prisma";
const app = Router();
const AUTH_ENDPOINT = "https://auth.fediverse.events";
const AUTH_CLIENT = "canvas";
const AUTH_SECRET = "secret";
app.get("/login", (req, res) => {
const params = new URLSearchParams();
params.set("service", "canvas");
res.redirect(AUTH_ENDPOINT + "/login?" + params);
});
app.get("/callback", async (req, res) => {
const { code } = req.query;
const who = await fetch(AUTH_ENDPOINT + "/api/auth/identify", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${AUTH_CLIENT}:${AUTH_SECRET}`,
},
body: JSON.stringify({
code,
}),
}).then((a) => a.json());
const [username, hostname] = who.user.sub.split("@");
await prisma.user.upsert({
where: {
sub: [username, hostname].join("@"),
},
update: {},
create: {
sub: [username, hostname].join("@"),
},
});
req.session.user = {
service: {
...who.service,
instance: {
...who.service.instance,
hostname,
},
},
user: {
profile: who.user.profile,
username,
},
};
req.session.save();
res.redirect("/");
});
export default app;