-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (100 loc) · 2.34 KB
/
index.js
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
const express = require("express");
const LnurlAuth = require("passport-lnurl-auth");
const passport = require("passport");
const session = require("express-session");
const cors = require("cors");
const path = require("path");
const app = express();
app.use(
cors({
origin: "http://localhost:5173",
credentials: true,
})
);
const config = {
host: "localhost",
port: 3001,
url: null,
};
if (!config.url) {
config.url = "http://" + config.host + ":" + config.port;
}
app.use(
session({
secret: "12345",
resave: true,
saveUninitialized: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
const map = {
user: new Map(),
};
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
done(null, map.user.get(id) || null);
});
passport.use(
new LnurlAuth.Strategy(function (linkingPublicKey, done) {
let user = map.user.get(linkingPublicKey);
if (!user) {
user = { id: linkingPublicKey };
map.user.set(linkingPublicKey, user);
}
done(null, user);
})
);
app.use(passport.authenticate("lnurl-auth"));
app.get("/", function (req, res) {
if (!req.user) {
return res.send(
'You are not authenticated. To login go <a href="/login">here</a>.'
);
// return res.redirect('/login');
}
res.send("Logged-in");
});
app.get(
"/login",
function (req, res, next) {
if (req.user) {
// Already authenticated.
return res.redirect("http://localhost:5173/");
}
next();
},
new LnurlAuth.Middleware({
callbackUrl: config.url + "/login",
cancelUrl: "http://localhost:5173/browseDevs",
loginTemplateFilePath: path.join(__dirname, "login.html"),
})
);
app.get("/user", (req, res) => {
res.send(req.user);
});
app.get("/logout", function (req, res, next) {
if (req.user) {
req.session.destroy();
res.json({ message: "user logged out" });
// Already authenticated.
return res.redirect("http://localhost:5173");
}
next();
});
const server = app.listen(config.port, config.host, function () {
console.log("Server listening at " + config.url);
});
process.on("uncaughtException", (error) => {
console.error(error);
});
process.on("beforeExit", (code) => {
try {
server.close();
} catch (error) {
console.error(error);
}
process.exit(code);
});