-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
171 lines (156 loc) · 4.12 KB
/
Copy pathserver.js
File metadata and controls
171 lines (156 loc) · 4.12 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import serve from "serve-static";
import express from "express";
import session from "express-session";
import makeMemoryStore from "memorystore";
import compression from "compression";
import * as sapper from "@sapper/server";
import passport from "passport";
import CustomStrategy from "passport-custom";
import MastodonStrategy from "passport-mastodon";
import path from "path";
import creds from "./creds.json";
import store, { picrewApi, htmlListener } from "./server/picrewApi.js";
import fileUpload from "./server/fileUpload.js";
//import { cleanupPicrews } from "./server/cleanupPicrews.js";
const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === "development";
let loggedout = true;
const MemoryStore = makeMemoryStore(session);
const findOrCreateUser = async (name, url, token) => {
// try to find by token
const res = await picrewApi.find("camper", null, {
match: {
token,
},
});
let user = res.payload?.records?.[0] ?? null;
// try to find by name
if (!user) {
const res = await picrewApi.find("camper", null, {
match: {
name,
},
});
user = res.payload?.records?.[0] ?? null;
// update token
if (user) {
await picrewApi.update("camper", [
{
id: user.id,
replace: {
token,
},
},
]);
}
}
if (!user) {
const record = {
name: name,
url: url,
created: new Date(),
token,
};
const created = await picrewApi.create("camper", record);
user = created.payload.records[0];
}
return user;
};
passport.use(
"localhost",
new CustomStrategy(async (req, done) => {
const user = await findOrCreateUser("objelisks", "fgsfds", "token1");
if (dev && loggedout) {
loggedout = false;
return done(null, false);
}
return done(null, user);
})
);
passport.use(
new MastodonStrategy(
creds,
async (accessToken, refreshToken, profile, done) => {
const user = await findOrCreateUser(
profile.username,
profile.profileUrl,
accessToken
);
return done(null, user);
}
)
);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (userId, done) => {
const res = await picrewApi.find("camper", userId);
const user = res.payload?.records?.[0];
done(null, user);
});
const lockedApi = (req, res, next) => {
if ((req.ip === "127.0.0.1") || req.user) {
return next();
}
res.status(401).send("Not authorized");
};
const authenticate = ({ dev }) =>
dev
? passport.authenticate("localhost", {
failureRedirect: "/picramp",
})
: passport.authenticate("mastodon", {
scope: "read:accounts",
failureRedirect: "/picramp",
});
express()
.use(
session({
cookie: { maxAge: 86400000 },
store: new MemoryStore({ checkPeriod: 86400000 }),
resave: false,
saveUninitialized: false,
secret: "fgsfds",
})
)
.use(passport.initialize())
.use(passport.session())
.get("/picramp/login", authenticate({ dev }), (req, res) =>
res.redirect("/picramp")
)
.get(
"/picramp/login/redirect",
passport.authenticate("mastodon", {
failureRedirect: "/picramp",
}),
(req, res) => {
res.redirect("/picramp");
}
)
.get("/picramp/logout", (req, res, next) => {
loggedout = true;
req.session.destroy(() => {
req.logout();
res.redirect("/picramp");
});
})
.use("/picramp/upload", lockedApi, fileUpload)
.use("/picramp/rest", lockedApi, store)
//.use("/picramp/cleanPicrews", (req, res, next) => { cleanupPicrews().then(() => res.status(200).send('cleaned picrews')).catch((e) => res.status(500).send('failed to clean' + e)); })
.use("/picramp/db", dev ? htmlListener : (_, __, next) => next())
.use(
"/picramp",
compression({ threshold: 0 }),
serve(path.join(__dirname, `../../../static/`)),
sapper.middleware({
session: (req, res) => {
return {
authenticated: !!req.user,
camper: req.user,
};
},
})
)
.listen(PORT, (err) => {
if (err) console.log("error", err);
});