Skip to content
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

Merged
merged 6 commits into from Apr 1, 2024

Conversation

eunjae-lee
Copy link
Contributor

@eunjae-lee eunjae-lee commented Mar 28, 2024

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:

  • returning undefined
  • returning EventHandlerResponse like await sendRedirect(...)
  • throw createError(...)

@eunjae-lee eunjae-lee requested review from a team, demetriusfeijoo, BibiSebi and Dawntraoz and removed request for a team March 28, 2024 15:36
@@ -16,6 +19,10 @@ declare module '@nuxt/schema' {
errorCallback: string;
middleware?: {
ignoredPaths?: string[]; //e.g.: ['/api/endpoint']
afterAuth?: (params: {
Copy link
Contributor

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 });
Copy link
Contributor

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
Copy link
Contributor

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",

@eunjae-lee
Copy link
Contributor Author

eunjae-lee commented Mar 29, 2024

@demetriusfeijoo thanks for the feedback! I've applied them here :)

What do you think about afterAuthenticated?

@eunjae-lee eunjae-lee changed the title feat(common): add afterAuth middleware hook feat(common): add afterAuthenticated middleware hook Mar 29, 2024
Copy link
Contributor

@demetriusfeijoo demetriusfeijoo left a 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. 🙌

Copy link
Contributor

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. 🤔

Copy link
Contributor Author

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 :)

@demetriusfeijoo demetriusfeijoo merged commit 54b6bcb into main Apr 1, 2024
2 checks passed
@demetriusfeijoo demetriusfeijoo deleted the feat/after-auth branch April 1, 2024 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants