-
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.
Adding custom errors for token validation
Co-authored-by: Daniel Ferreira <[email protected]>
- Loading branch information
1 parent
278310b
commit 0bd0830
Showing
8 changed files
with
44 additions
and
24 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
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ export class EmailService { | |
|
||
async init({ email: user, password: pass }) { | ||
this.email = user; | ||
const transporter = await nodemailer.createTransport({ | ||
/* const transporter = await nodemailer.createTransport({ | ||
pool: true, | ||
host: "smtp.gmail.com", | ||
port: 465, | ||
|
@@ -17,7 +17,15 @@ export class EmailService { | |
}, | ||
connectionTimeout: 30000 | ||
}); | ||
console.log("transporter"); | ||
console.log("transporter");*/ | ||
const transporter = nodemailer.createTransport({ | ||
host: "smtp.ethereal.email", | ||
port: 587, | ||
auth: { | ||
user: "[email protected]", | ||
pass: "NGEVbMnTZzyA3MQD3V" | ||
} | ||
}); | ||
|
||
transporter.use("compile", hbs({ | ||
viewEngine: { | ||
|
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,22 @@ | ||
import jwt from "jsonwebtoken"; | ||
import { APIError, ErrorTypes } from "../api/middleware/errorHandler.js"; | ||
import { StatusCodes as HTTPStatus } from "http-status-codes/build/cjs/status-codes.js"; | ||
import ValidationReasons from "../api/middleware/validators/validationReasons.js"; | ||
|
||
export const generateToken = (data, secret, expiresInSeconds) => jwt.sign( | ||
{ ...data }, | ||
secret, | ||
{ expiresIn: `${expiresInSeconds} seconds`, algorithm: "HS256" } | ||
); | ||
|
||
export const verifyAndDecodeToken = (token, secret) => { | ||
export const verifyAndDecodeToken = (token, secret, next) => { | ||
try { | ||
return jwt.verify(token, secret, { algorithm: "HS256" }); | ||
} catch (err) { | ||
return null; | ||
} catch (jwtErr) { | ||
if (jwtErr.name === "TokenExpiredError") { | ||
return next(new APIError(HTTPStatus.FORBIDDEN, ErrorTypes.FORBIDDEN, ValidationReasons.EXPIRED_TOKEN)); | ||
} else { | ||
return next(new APIError(HTTPStatus.FORBIDDEN, ErrorTypes.FORBIDDEN, ValidationReasons.INVALID_TOKEN)); | ||
} | ||
} | ||
}; |
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