-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
726 additions
and
30 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 @@ | ||
web: node server.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const SECRET = process.env.SECRET; | ||
|
||
module.exports = function (req, res, next) { | ||
let token = req.get('Authorization') || req.query.token || req.body.token; | ||
if (token) { | ||
token = token.replace('Bearer ', ''); | ||
jwt.verify(token, SECRET, function (err, decoded) { | ||
if (err) { | ||
next(err); | ||
} else { | ||
req.user = decoded.user; | ||
next(); | ||
} | ||
}); | ||
} else { | ||
next(); | ||
} | ||
}; |
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,15 +1,45 @@ | ||
const User = require('../models/user'); | ||
const jwt = require('jsonwebtoken'); | ||
const SECRET = process.env.SECRET; | ||
|
||
module.exports - { | ||
signup | ||
module.exports = { | ||
signup, | ||
login | ||
}; | ||
|
||
async function signup(req, res) { | ||
const user = new User(req.body); | ||
try { | ||
console.log('controllers/users/13'); | ||
await user.save(); | ||
res.join(user); | ||
const token = createJWT(user); | ||
res.json({ token }); | ||
} catch (err) { | ||
res.status(400).json(err); | ||
}; | ||
} | ||
|
||
async function login(req, res) { | ||
try { | ||
const user = await User.findOne({ email: req.body.email }); | ||
if (!user) return res.status(401).json({ err: 'bad credentials' }); | ||
user.comparePassword(req.body.pw, (err, isMatch) => { | ||
if (isMatch) { | ||
const token = createJWT(user); | ||
res.json({ token }); | ||
} else { | ||
return res.status(401).json({ err: 'bad credentials' }); | ||
} | ||
}); | ||
} catch (err) { | ||
return res.status(401).json(err); | ||
} | ||
} | ||
|
||
function createJWT(user) { | ||
return jwt.sign( | ||
{ user }, | ||
SECRET, | ||
{ expiresIn: '24h' } | ||
); | ||
} |
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
Oops, something went wrong.