New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(common): add afterAuthenticated middleware hook #40
Conversation
@@ -16,6 +19,10 @@ declare module '@nuxt/schema' { | |||
errorCallback: string; | |||
middleware?: { | |||
ignoredPaths?: string[]; //e.g.: ['/api/endpoint'] | |||
afterAuth?: (params: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @eunjae-lee 🙌
Would be possible to create a type for this function? wdyt?
|
||
const afterAuth = appConfig.auth.middleware?.afterAuth; | ||
if (typeof afterAuth === 'function') { | ||
return await afterAuth({ event, appSession }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is called only when the authentication succeeds, so I was wondering if we could name it more specifically, what do you think?
afterAuthSucceeds
? 🤔
}); | ||
|
||
const isMiddlewareIgnored = ( | ||
currentPath: string, | ||
ignoredPaths: string[] | undefined, | ||
ignoredPaths: string[] | undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: In another task we can add this config to the .prettier file 🤔
"trailingComma": "all",
@demetriusfeijoo thanks for the feedback! I've applied them here :) What do you think about |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about afterAuthenticated?
Perfect 💯
I left a comment but it's just a doubt, not a blocker or anything like that. 🙌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think in the future we could move these types to a types
folder and then configure it to work with auto-import? I don't know if it would work since we use this project as a layer for others. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey that's actually what I wondered about, but didn't go deep enough to test the configuration with this layer. So for now I simply put it under utils, so that it can be auto-imported without any special configs. However, it's worth trying later :)
What?
This PR adds
afterAuth
hook to the auth middleware.Why?
User may want to add more logic right after the authentication. For example, if only admin should enter
/settings
page, then users need to add more logic for that. It can be done by creating a server middleware on the user's project, but currently there is a bug on Nuxt regarding the middleware execution order. But even if the bug is not there, this hook can be a easier way for user to put an extra simple action right after the authentication.For example, the following is possible with
afterAuth
:undefined
await sendRedirect(...)
throw createError(...)