-
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 #33 from Shaurya0108/feat/jwt
Added JWT. Added middleware to authorize API requests
- Loading branch information
Showing
7 changed files
with
137 additions
and
18 deletions.
There are no files selected for viewing
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
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,13 +1,15 @@ | ||
export class UnauthorizedError extends Error{ | ||
constructor(message) { | ||
constructor(message, statusCode) { | ||
super(message); | ||
this.name = 'UnauthorizedError'; | ||
this.statusCode = statusCode; | ||
} | ||
} | ||
|
||
export class ConflictError extends Error{ | ||
constructor(message) { | ||
super(message); | ||
this.name = 'ConflictError' | ||
this.name = 'ConflictError'; | ||
this.statusCode = 409; | ||
} | ||
} |
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,12 +1,19 @@ | ||
import { UnauthorizedError } from '../classes/Error.js'; | ||
import dotenv from 'dotenv'; | ||
import jwt from 'jsonwebtoken'; | ||
|
||
dotenv.config(); | ||
|
||
export const auth = ({authorization}, resolve, reject) => { | ||
if (authorization){ | ||
var authorized = true | ||
resolve(authorized) | ||
} | ||
else { | ||
throw new Error('no authorization'); | ||
const token = authorization && authorization.split(' ')[1]; | ||
|
||
if (!token){ | ||
throw new UnauthorizedError("Unauthorized", 401) | ||
} | ||
jwt.verify(token, process.env.secret_access_token, (err, user) => { | ||
if (err) throw new UnauthorizedError("Forbidden", 403); | ||
resolve(true); | ||
}) | ||
|
||
} | ||
} | ||
|
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