-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(Authentication-flow): modify auth logic and flow
created an auth decorator - @AuthGuard, instead of pass it authHandler as a middleware, we can extend it to check for role base access permission
- Loading branch information
1 parent
8bb76c1
commit 6baedd6
Showing
23 changed files
with
338 additions
and
107 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { INTERNAL_SERVER_ERROR } from 'http-status'; | ||
import { TYPES } from 'di/types'; | ||
import { AppError } from 'utils'; | ||
import container from 'di/container'; | ||
import { RouteHandler } from 'utils/catchAsync'; | ||
import { AuthHandler } from 'middlewares/authHandler'; | ||
|
||
export function AuthGuard() { | ||
return function (target: object, _: string, descriptor: PropertyDescriptor) { | ||
const originalMethod = descriptor.value; | ||
descriptor.value = async function (...args: RouteHandler) { | ||
if (!container) { | ||
throw new AppError('Container not set', INTERNAL_SERVER_ERROR); | ||
} | ||
const authHandler = container.get<AuthHandler>(TYPES.AuthHandler); | ||
const handlerMiddleware = await authHandler.handler(); | ||
|
||
return new Promise((resolve, reject) => { | ||
handlerMiddleware(args[0], args[1], (error) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
originalMethod.apply(this, args).then(resolve).catch(reject); | ||
} | ||
}); | ||
}); | ||
}; | ||
return descriptor; | ||
}; | ||
} |
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,3 +1,4 @@ | ||
export * from './route'; | ||
export * from './auth-guard'; | ||
export * from './validator'; | ||
export * from './controller'; |
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
Oops, something went wrong.