This repository has been archived by the owner on Apr 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
58 lines (51 loc) · 1.38 KB
/
auth.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
const express = require('express');
const session = require('express-session');
const passport = require('passport');
const TwitterStrategy = require('passport-twitter').Strategy;
const config = require('./config');
passport.serializeUser((user, done) => { done(null, user); });
passport.deserializeUser((obj, done) => { done(null, obj); });
passport.use(new TwitterStrategy({
consumerKey: config.twitter.consumer_key,
consumerSecret: config.twitter.consumer_secret,
callbackURL: config.twitter.callbackUrl
}, (token, tokenSecret, profile, done) => {
const user = {
token: token,
tokenSecret: tokenSecret,
profile: profile
};
return done(null, user);
}));
const app = express();
app.use(session({ secret: '1234' }));
app.use(passport.initialize());
app.use(passport.session());
app.get('/auth', (req, res) => {
const body = `
<html>
<body>
<a href="/auth/twitter">Auth Twitter</a>
</body>
</html>`
res.send(body);
});
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get(
'/auth/twitter/callback',
passport.authenticate('twitter', { failureRedirect: '/auth' }),
(req, res) => {
const body = `
<html>
<body>
<pre>
${JSON.stringify(req.user, null, 2)}
</pre>
</body>
</html>`;
res.send(body);
}
);
app.listen(3000, function webStarted() {
console.log('Created web server');
});