-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (100 loc) · 3.28 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require('dotenv').config()
const passport = require('passport')
const GitHubStrategy = require('passport-github2').Strategy
const cookieSession = require('cookie-session')
const express = require('express')
const cookieParser = require('cookie-parser')
const uuidv4 = require('uuid/v4')
const cors = require('cors')
const knex = require('./knex')
const port = process.env.PORT || 3000
const app = express()
const usersRouter = require('./routes/users')
const teamsRouter = require('./routes/teams')
const eventsRouter = require('./routes/events')
const skillsRouter = require('./routes/skills')
app.use(cors())
app.use(cookieParser())
app.use(cookieSession({ secret: process.env.COOKIE_SECRET, httpOnly: false }))
// passport middleware
app.use(passport.initialize())
passport.serializeUser((user, done) => {
done(null, user.id)
})
passport.deserializeUser((id, done) => {
// what do i need to do when user visits the site and already has a cookie?
// i.e. what needs to be stored in req.user?
knex('users').first().where('id', id)
.then(user => {
done(null, user)
})
.catch(err => { next(err) } )
})
passport.use(new GitHubStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: process.env.CALLBACK,
userAgent: process.env.DOMAIN,
scope: 'user:email'
},
function onSuccess(token, refreshToken, profile, done) {
// split name from GitHub to prepopulate first and last name
let firstName, lastName
let fullName = profile.displayName
let firstSpaceIndex = fullName.indexOf(' ')
if (firstSpaceIndex === -1) {
firstName = fullName
}
else {
firstName = fullName.slice(0, firstSpaceIndex)
lastName = fullName.slice(firstSpaceIndex + 1)
}
knex('users').first().where('email', profile.emails[0].value)
.then(user => {
// check db to see if user already exists (by email)
if (user) {
done(null, user)
}
// if not, make a db entry for them
else {
knex('users').insert({ key: uuidv4(), email: profile.emails[0].value,
first_name: firstName, last_name: lastName, user_picture_url: profile._json.avatar_url })
.returning('*')
.then(user => {
done(null, user[0])
})
.catch(err => next(err))
}
})
.catch(err => next(err))
}
))
app.use(passport.session())
app.use(express.json())
app.use(express.static('public'))
// passport route to initiate GitHub OAuth
app.get('/users/auth/github', passport.authenticate('github'))
// passport callback route
app.get('/users/auth',
passport.authenticate('github', {
successRedirect: 'https://hackateam-cat.herokuapp.com/html/user-profile.html',
failureRedirect: 'https://hackateam-cat.herokuapp.com/',
scope: 'user:email'
}))
// routers
app.use('/users', usersRouter)
app.use('/teams', teamsRouter)
app.use('/events', eventsRouter)
app.use('/skills', skillsRouter)
// error handler
app.use((err, req, res, next) => {
err.status = err.status || 500
err.message = err.message || 'Internal server error'
res.json(err)
})
// 404
app.use((req, res, next) => {
res.json({ status: 404, message: 'Not found'})
})
app.listen(port, () => { console.log(`Listening on port ${port}`) })