-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from rockpell/feature/github-login
Feature/GitHub login
- Loading branch information
Showing
16 changed files
with
532 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const passport = require('passport') | ||
const GitHubStrategy = require('passport-github').Strategy | ||
const JWTStrategy = require('passport-jwt').Strategy | ||
const { User } = require('../model/User') | ||
|
||
require('dotenv').config() | ||
|
||
const githubStrategyOption = { | ||
clientID: process.env.GITHUB_CLIENT_ID, | ||
clientSecret: process.env.GITHUB_CLIENT_SECRET, | ||
callbackURL: process.env.GITHUB_CALLBACK_URL, | ||
} | ||
|
||
async function gitStrategyLogin(profiles) { | ||
try { | ||
let user = await User.findOne({ OAuthId: profiles.id }) | ||
if (user === null) { | ||
const data = await User.create({ | ||
OAuthId: profiles.id, | ||
fullName: profiles.username, | ||
isDeleted: false, | ||
}) | ||
return { | ||
success: true, | ||
id: data._id, | ||
} | ||
} | ||
return { | ||
success: true, | ||
id: user._id, | ||
} | ||
} catch (err) { | ||
return { success: false } | ||
} | ||
} | ||
|
||
async function githubVerify(accessToken, refreshToken, profile, done) { | ||
try { | ||
const result = await gitStrategyLogin(profile) | ||
const user = { id: result.id } | ||
|
||
if (result.success) { | ||
return done(null, user) | ||
} | ||
return done(null, false, { message: 'κΉνλΈ λ‘κ·ΈμΈμ μ€ν¨νμ΅λλ€.' }) | ||
} catch (err) { | ||
return done(null, false, { message: 'GitHub verify err λ°μ' }) | ||
} | ||
} | ||
|
||
const cookieExtractor = req => { | ||
if (req.signedCookies) return req.signedCookies.token | ||
if (req.cookies) return req.cookies | ||
} | ||
|
||
const isExist = async userId => { | ||
try { | ||
let user = await User.findOne({ _id: userId }) | ||
return { | ||
success: true, | ||
id: user._id, | ||
} | ||
} catch (err) { | ||
return { success: false } | ||
} | ||
} | ||
|
||
const jwtStrategyOption = { | ||
jwtFromRequest: cookieExtractor, | ||
secretOrKey: process.env.JWT_SECRET, | ||
} | ||
async function jwtVerify(payload, done) { | ||
try { | ||
const result = await isExist(payload.id) | ||
if (!result.success) { | ||
return done(null, false, { message: 'JWT ν ν° μΈμ¦μ μ€ν¨νμ΅λλ€.' }) | ||
} | ||
return done(null, result) | ||
} catch (err) { | ||
return done(null, false, { message: 'JWT verify err λ°μ' }) | ||
} | ||
} | ||
|
||
module.exports = () => { | ||
passport.use(new GitHubStrategy(githubStrategyOption, githubVerify)) | ||
passport.use(new JWTStrategy(jwtStrategyOption, jwtVerify)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import express from 'express' | ||
import userController from './user' | ||
import channelController from './channel' | ||
|
||
const router = express.Router() | ||
|
||
router.use('/channel', channelController) | ||
|
||
router.use('/user', userController) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import express from 'express' | ||
const router = express.Router() | ||
const controller = require('./userController') | ||
|
||
router.get('/sign-in/github', controller.githubLogin) | ||
router.get('/sign-in/github/callback', controller.githubCallback) | ||
router.get('/auth', controller.authCheck) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const passport = require('passport') | ||
const jwt = require('jsonwebtoken') | ||
|
||
exports.githubLogin = passport.authenticate('github') | ||
|
||
exports.githubCallback = async (req, res, next) => { | ||
const frontHost = process.env.FRONTEND_HOST | ||
passport.authenticate('github', (err, id) => { | ||
if (err || !id) { | ||
return res.status(200).redirect(frontHost) | ||
} | ||
req.login(id, { session: false }, err => { | ||
if (err) { | ||
res.send(err) | ||
} | ||
|
||
const token = jwt.sign(id, process.env.JWT_SECRET, { expiresIn: '1H' }) | ||
res.cookie('token', token, { | ||
maxAge: 1000 * 60 * 60, | ||
httpOnly: true, | ||
signed: true, | ||
}) | ||
return res.status(200).redirect(frontHost) | ||
}) | ||
})(req, res) | ||
} | ||
|
||
exports.authCheck = (req, res) => { | ||
let token = req.signedCookies.token | ||
if (token) { | ||
try { | ||
let decoded = jwt.verify(token, process.env.JWT_SECRET) | ||
return res.json({ verify: true }) | ||
} catch (err) { | ||
return res.json({ verify: false }) | ||
} | ||
} else { | ||
return res.json({ verify: false, message: 'token does not exist' }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const passport = require('passport') | ||
require('dotenv').config() | ||
|
||
exports.Auth = (req, res, next) => { | ||
passport.authenticate('jwt', { session: false }, (err, user) => { | ||
if (err || !user || !user.success) { | ||
next({ status: 403, message: 'auth error' }) | ||
} | ||
req.user = user | ||
next() | ||
})(req, res, next) | ||
} |
Oops, something went wrong.