-
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.
- Loading branch information
Showing
5 changed files
with
175 additions
and
12 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,66 @@ | ||
const Router = require("express").Router; | ||
const jwt = require("jsonwebtoken"); | ||
|
||
const config = require("../../../../config"); | ||
const UserAccess = require('../../../../data-access/user-db'); | ||
|
||
const resetRouter = new Router(); | ||
|
||
resetRouter.get("/:email", async (req, res) => { | ||
try { | ||
const user = await UserAccess.findUserBy('email', req.params.email); | ||
if(!user) throw new Error('There is no such user!'); | ||
|
||
const secret = user.password + config.process.env.JWT_SECRET; | ||
const token = jwt.sign( | ||
{ | ||
sub: user.id, | ||
iss: config.JWT_ISSUER, | ||
}, | ||
secret, | ||
{ | ||
expiresIn: "15m", | ||
} | ||
); | ||
|
||
return res.send({ | ||
status: "success", | ||
reset_token: token | ||
}); | ||
} catch (e) { | ||
return res.send({ | ||
status: "fail", | ||
errorList: [e.message], | ||
}); | ||
} | ||
|
||
|
||
|
||
}); | ||
|
||
resetRouter.get("/:email/confirm", async (req, res) => { | ||
try { | ||
const { reset_token, password } = req.body; | ||
if(!password) throw new Error('Please give proper credentials!'); | ||
|
||
const user = UserAccess.findUserById('email', req.body.email); | ||
const secret = user.password + config.process.env.JWT_SECRET; | ||
|
||
const data = jwt.verify(reset_token, secret); | ||
if(data.sub !== user.id) throw new Error('Token didnt match!'); | ||
|
||
const updatedUser = UserAccess.updateUser(user.id, { password }); | ||
|
||
return res.send({ | ||
status: 'success', | ||
updatedUser | ||
}); | ||
|
||
} catch(e){ | ||
return res.send({ | ||
status: "fail", | ||
errorList: [e.message], | ||
}); | ||
} | ||
|
||
}); |
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,71 @@ | ||
const Router = require("express").Router; | ||
const jwt = require("jsonwebtoken"); | ||
|
||
const config = require("../../config"); | ||
const UserAccess = require('../../data-access/user-db'); | ||
|
||
const resetRouter = new Router(); | ||
|
||
resetRouter.get("/:email", async (req, res) => { | ||
try { | ||
const user = await UserAccess.findUserBy('email', req.params.email); | ||
if(!user) throw new Error('There is no such user!'); | ||
|
||
const secret = user.password + config.JWT_SECRET; | ||
const token = jwt.sign( | ||
{ | ||
sub: user.id, | ||
iss: config.JWT_ISSUER, | ||
}, | ||
secret, | ||
{ | ||
expiresIn: "15m", | ||
} | ||
); | ||
|
||
return res.send({ | ||
status: "success", | ||
reset_token: token | ||
}); | ||
} catch (e) { | ||
return res.send({ | ||
status: "fail", | ||
errorList: [e.message], | ||
}); | ||
} | ||
|
||
|
||
|
||
}); | ||
|
||
resetRouter.post("/:email", async (req, res) => { | ||
try { | ||
const reset_token = req.headers['authorization'].split(' ')[1]; | ||
const user = await UserAccess.findUserBy('email', req.params.email); | ||
|
||
if(!user) throw new Error('No user found with email: ' + req.params.email); | ||
|
||
const secret = user.password + config.JWT_SECRET; | ||
const data = jwt.verify(reset_token, secret); | ||
|
||
const { password } = req.body; | ||
if(!password) throw new Error('Please give proper credentials!'); | ||
|
||
const updatedUser = UserAccess.updateUser(user.id, { password }); | ||
|
||
return res.send({ | ||
status: 'success', | ||
updatedUser | ||
}); | ||
|
||
} catch(e){ | ||
return res.send({ | ||
status: "fail", | ||
errorList: [e.message], | ||
}); | ||
} | ||
|
||
}); | ||
|
||
|
||
module.exports = resetRouter; |