-
Notifications
You must be signed in to change notification settings - Fork 11
/
simple.js
87 lines (72 loc) · 1.68 KB
/
simple.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
const express = require('express');
const LnurlAuth = require('../index');
const passport = require('passport');
const session = require('express-session');
const app = express();
const config = {
host: 'localhost',
port: 3000,
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('/');
}
next();
},
new LnurlAuth.Middleware({
callbackUrl: `${config.url}/login`,
cancelUrl: config.url
})
);
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);
});