-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi_key.js
More file actions
79 lines (70 loc) · 2.59 KB
/
api_key.js
File metadata and controls
79 lines (70 loc) · 2.59 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
'use strict';
var randomstring = require('randomstring');
var mongoose = require('mongoose');
var User = mongoose.model('User');
module.exports = {
serializeUser: function(user, done) {
done(null, user._id);
},
deserializeUser: function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
},
getUserStrategy: function(token, tokenSecret, profile, done) {
User.findById(profile.id, function(err, user) {
if(user)
{
done(null, user);
}
else
{
var user = new User();
user._id = profile.id;
user.name = profile.nickname;
user.api_key = randomstring.generate();
user.save(function(err, user){
done(null, user);
});
}
});
},
configurePassport: function(app) {
/**
* Configure passport
*/
var passport = require('passport');
passport.serializeUser(module.exports.serializeUser);
passport.deserializeUser(module.exports.deserializeUser);
// Strategy for logging in Avans
var AvansStrategy = require('passport-avans').Strategy;
passport.use(new AvansStrategy({
consumerKey: '201cb65cc4f38f60d4f35e677dd5d119a12d9294',
consumerSecret: '2c9cbe2064fa43d9b57fb47982de3d986f319eda',
callbackURL: '/api_key/callback'
},
module.exports.getUserStrategy
));
/**
* Configure App
*/
app.use(passport.initialize());
app.use(passport.session());
app.get('/api_key', function(req, res) {
if(!req.user) {
return res.redirect('/api_key/login')
}
res.send(`<html>
<body style="text-align: center; margin: 4em; font-family: Helvetica;">
<h1>De API key voor `+req.user.name+` is:</h1>
<input type="text" value="`+req.user.api_key+`" style="font-size: 2em; padding: 1.2em; width: 80%; font-family: monospace; text-align: center;">
<br><br>
Gebruik deze in de URL van de API, bijvoorbeeld:<br>
<a href="/api/games?api_key=`+req.user.api_key+`">https://strategoavans.herokuapp.com/api/games?api_key=`+req.user.api_key+`</a>
</body>
</html>`);
});
app.get('/api_key/login', passport.authenticate('avans'))
app.get('/api_key/callback', passport.authenticate('avans', { successRedirect: '/api_key', failureRedirect: '/api_key/login' }));
}
}