-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (51 loc) · 1.59 KB
/
app.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
const app = require('express')();
const session = require('express-session');
const auth = require('./auth');
const request = require('superagent');
app.use(session({
secret: 'thisisasecretKey',
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 604800000
}
}));
app.get('/login', async (req, res) => {
const tokens = await auth.getTokenFromCode(req.query.code);
if (tokens.e) {
res.end("An error happend: \n" + tokens.e);
} else {
const acc = await new Promise((resolve) => {
//send a request to get user data
request
.get('https://graph.microsoft.com/beta/me')
.set('Authorization', 'Bearer ' + tokens.accessToken)
.end((err, res) => {
if (err) {
resolve(err);
} else {
resolve(res.body);
}
});
});
if (acc.mail != undefined) {
//see result.json for all the information it gives you
req.session.email = acc.mail;
req.session.displayName = acc.displayName;
console.log(acc.mailNickname, "Has logged in!");
}
res.redirect("/");
}
})
app.get('/trylogin', (req, res) => {
//redirect visitor to the right login url
res.redirect(auth.getAuthUrl());
})
app.get('/', (req, res) => {
if (req.session.email != undefined) {
res.end("Logged in!" + req.session.email);
} else {
res.end("<a href='/trylogin'>Click here to login</a>");
}
})
app.listen(3000);