-
Notifications
You must be signed in to change notification settings - Fork 220
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 #3 from Antarctic-penguin/R04
R04
- Loading branch information
Showing
20 changed files
with
1,155 additions
and
549 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
JWT_SECRET= |
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 @@ | ||
web: NODE_ENV=production node app.js |
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
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,5 +1,44 @@ | ||
const passport = require('passport') | ||
const LocalStrategy = require('passport-local') | ||
const passportJWT = require('passport-jwt') | ||
const bcrypt = require('bcryptjs') | ||
const { User } = require('../models') | ||
|
||
const JWTStrategy = passportJWT.Strategy | ||
const ExtractJWT = passportJWT.ExtractJwt | ||
|
||
|
||
passport.use(new LocalStrategy( | ||
// customize user field | ||
{ | ||
usernameField: 'account', | ||
passwordField: 'password', | ||
passReqToCallback: true | ||
}, | ||
// authenticate user or admin | ||
(req, account, password, cb) => { | ||
User.findOne({ where: { account } }) | ||
.then(user => { | ||
if (!user) return cb(Error('帳號不存在'), false) | ||
|
||
bcrypt.compare(password, user.password).then(res => { | ||
if (!res) return cb(Error('密碼輸入錯誤!'), false) | ||
|
||
return cb(null, user) | ||
}) | ||
}) | ||
} | ||
)) | ||
|
||
const jwtOptions = { | ||
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: process.env.JWT_SECRET | ||
} | ||
|
||
passport.use(new JWTStrategy(jwtOptions, (jwtPayload, cb) => { | ||
User.findByPk(jwtPayload.id) | ||
.then(user => cb(null, user)) | ||
.catch(err => cb(err)) | ||
})) | ||
|
||
module.exports = passport |
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,13 @@ | ||
const tweetServices = require('../../services/tweet-services') | ||
const tweetController = { | ||
getTweets: (req, res, next) => { | ||
tweetServices.getTweets(req, (err, data) => err ? next(err) : res.json(data)) | ||
}, | ||
getTweet: (req, res, next) => { | ||
tweetServices.getTweet(req, (err, data) => err ? next(err) : res.json(data)) | ||
}, | ||
postTweet: (req, res, next) => { | ||
tweetServices.postTweet(req, (err, data) => err ? next(err) : res.json({ status: 'success', data })) | ||
}, | ||
} | ||
module.exports = tweetController |
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,20 @@ | ||
const jwt = require('jsonwebtoken') | ||
const userController = { | ||
signIn: (req, res, next) => { | ||
try { | ||
const userData = req.user.toJSON() | ||
delete userData.password | ||
const token = jwt.sign(userData, process.env.JWT_SECRET, { expiresIn: '30d' }) | ||
res.json({ | ||
status: 'success', | ||
data: { | ||
token, | ||
user: userData | ||
} | ||
}) | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
} | ||
module.exports = userController |
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,20 @@ | ||
const passport = require('../config/passport') // 引入 passport | ||
const helpers = require('../_helpers') | ||
//const authenticated = passport.authenticate('jwt', { session: false }) | ||
const authenticated = (req, res, next) => { | ||
passport.authenticate('jwt', { session: false }, (err, user) => { | ||
if (err || !user) return res.status(401).json({ status: 'error', message: 'unauthorized' }) | ||
req.user = user | ||
next() | ||
})(req, res, next) | ||
} | ||
const authenticatedAdmin = (req, res, next) => { | ||
console.log(helpers.getUser(req)) | ||
if (helpers.getUser(req) && helpers.getUser(req).role === 'admin') return next() | ||
return res.status(403).json({ status: 'error', message: 'permission denied' }) | ||
} | ||
|
||
module.exports = { | ||
authenticated, | ||
authenticatedAdmin | ||
} |
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,26 @@ | ||
module.exports = { | ||
generalErrorHandler (err, req, res, next) { | ||
if (err instanceof Error) { | ||
req.flash('error_messages', `${err.name}: ${err.message}`) | ||
} else { | ||
req.flash('error_messages', `${err}`) | ||
} | ||
res.redirect('back') | ||
|
||
next(err) | ||
}, | ||
apiErrorHandler (err, req, res, next) { | ||
if (err instanceof Error) { | ||
res.status(err.status || 500).json({ | ||
status: 'error', | ||
message: `${err.name}: ${err.message}` | ||
}) | ||
} else { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: `${err}` | ||
}) | ||
} | ||
next(err) | ||
} | ||
} |
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,4 @@ | ||
const multer = require('multer') | ||
const upload = multer({ dest: 'temp/' }) | ||
|
||
module.exports = upload |
Oops, something went wrong.