-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
73 lines (63 loc) · 2 KB
/
server.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
/* eslint-disable no-console */
const http = require('http');
const express = require('express');
const session = require('express-session');
const passport = require('passport');
const SlackStrategy = require('./..').default.Strategy;
// Configure the Slack Strategy
passport.use(new SlackStrategy({
clientID: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
}, (accessToken, scopes, team, extra, profiles, done) => {
done(null, profiles.user);
}));
// When using Passport's session functionality, you need to tell passport how to
// serialize/deserialize the user object to the session store
passport.serializeUser((user, done) => {
// Simplest possible serialization
done(null, JSON.stringify(user));
});
passport.deserializeUser((json, done) => {
// Simplest possible deserialization
done(null, JSON.parse(json));
});
// Initialze Express app and middleware
const app = express();
app.set('view engine', 'ejs');
app.use(session({
cookie: {
// secure should be enabled in a production app, but disabled for simplicity
// secure: true,
},
resave: false,
saveUninitialized: false,
secret: 'CHANGE ME',
}));
app.use(passport.initialize());
app.use(passport.session());
// Home page that doesn't require logging in, but displays login state. See 'views/index.ejs'
app.get('/', (req, res) => {
res.render('index', {
user: req.user,
});
});
// Initiates basic Sign in With Slack flow
app.get('/auth/slack', passport.authenticate('slack'));
// Completes the OAuth flow.
app.get('/auth/slack/callback',
passport.authenticate('slack'), // Failure triggers the default failure handler (401 Unauthorized)
(req, res) => {
// Successful authentication redirects home.
res.redirect('/');
}
);
// Handle removing the user from the session
app.post('/logout', (req, res) => {
req.logout();
res.redirect('/');
});
const server = http.createServer(app);
const port = process.env.PORT;
server.listen(port, () => {
console.log(`server listening on ${port}`);
});