Open septatrix opened 7 months ago
You should be able to use layouts to avoid repeating logic for all pages that needs authentication.
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.
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 :
You can create a global middleware in your project with this code : https://github.com/Hebilicious/authjs-nuxt/blob/main/packages/authjs-nuxt/src/runtime/middleware/auth.ts
You should also be able to set middlewares for all of your pages programatically : https://nuxt.com/docs/guide/directory-structure/middleware#setting-middleware-at-build-time
I think documenting either of these would be preferable than adding a global option.
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.
I agree, this should be natively supported. We shouldn't have to recreate this feature from scratch for every project.
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 aglobalAppMiddleware
similar to the one found in the sidebase package.Additional information