Hebilicious / authjs-nuxt

AuthJS edge-compatible authentication Nuxt module.
https://authjs-nuxt.pages.dev/
MIT License
247 stars 28 forks source link

Add `globalAppMiddleware` similar to sidebase module #143

Open septatrix opened 7 months ago

septatrix commented 7 months ago

Describe the feature

For many applications it is desirable to default to requiring authentication and have specific pages be able to opt out. Currently this is not possible and one has to manually add definePageMeta({ auth: true }); to every page. This is cumbersome and easy to forget. Please add a globalAppMiddleware similar to the one found in the sidebase package.

Additional information

Hebilicious commented 7 months ago

You should be able to use layouts to avoid repeating logic for all pages that needs authentication.

https://nuxt.com/docs/guide/directory-structure/layouts

peterbud commented 7 months ago

When I have tried to put a definePageMeta inside a layout file, I got the following warning: definePageMeta() is a compiler-hint helper that is only usable inside the script block of a single file component which is also a page. Its arguments should be compiled away and passing it at runtime has no effect.

So I'm not sure what is the designated way to add definePageMeta and auth to the layouts.

Maybe if one of the playgrounds would use such technique that would help.

Hebilicious commented 7 months ago

When I have tried to put a definePageMeta inside a layout file, I got the following warning: definePageMeta() is a compiler-hint helper that is only usable inside the script block of a single file component which is also a page. Its arguments should be compiled away and passing it at runtime has no effect.

So I'm not sure what is the designated way to add definePageMeta and auth to the layouts.

Maybe if one of the playgrounds would use such technique that would help.

Oh right, this is a page only feature. In that case you have a couple other options :

I think documenting either of these would be preferable than adding a global option.

peterbud commented 7 months ago

Thanks for the quick reply. The global middleware seems to be the best solution.

I have a default layout which signs in the user if not already signed in, and that is applied without specifying any definePageMeta on every individual page. And there is another layout ("guest") which does not require authentication. For pages which does not require authentication (or opt out), one should define the guest layout using definePageMeta.

So I think this is indeed better than the global option.

mrleblanc101 commented 6 months ago

I agree, this should be natively supported. We shouldn't have to recreate this feature from scratch for every project.