Closed tordans closed 11 months ago
Hey @tordans that's weird, I will take a look today
Thanks for reporting this issue @tordans!
I'm having a similar issue, which is causing problems in production wherever we use Page.authenticate = { role: ["ROLE"] }
. It ends up looping and redirecting (if a redirect is present). It worked fine before, and I'm not sure which version introduced this regression.
@chartgerink this issue is specifically for the app router and server components.
The solution Page.authenticate = { role: "ROLE" }
does not work in the server components, which is why the new hook useAuthenticatedBlitzContext
was introduced (which is what this issue is about). For client components in the app router the Page.authenticate
should still work – at least since https://github.com/blitz-js/blitz/pull/4225 was fixed.
Hey @tordans I finally took a look today, I have opened a PR to use authorise the session that we would expect when we define the roles.
Regarding the infinite loop, I feel this is an error in the code you provided.
So what happens here is the following:
ADMIN
/regions
. /regions
.So I would think if the page is to be protected only to ADMIN
users, we can do it in the following way,
await useAuthenticatedBlitzContext({
role: ["user"],
redirectTo: "/auth/login",
redirectAuthenticatedTo: (ctx) => {
const role = ctx.session.$publicData.role
if (role === "admin") {
return "/admin"
}
return "/user"
}
})
Adding logic to automatically handle the session role in https://github.com/blitz-js/blitz/pull/4257
What is the problem?
I am trying to use
useAuthenticatedBlitzContext
to guard an admin area so onlyuser.role==="ADMIN"
is allowed to see the pages.I have a test case in this app: https://github.com/FixMyBerlin/blitz-test/commit/b9c723e7f2d9bed18e838c34a618b0f240aa561f
The
redirectTo
works fineHowever, the
redirectAuthenticatedTo
doesnot
validate therole
properly. Instead it results in an infinite loop on the page.I tried working around the issue using the callback for
redirectAuthenticatedTo
but that does not receive any useful input as far as I can see. What is the use case for the callback, actually?Paste all your error logs here:
-
Paste all relevant code snippets here:
See https://github.com/FixMyBerlin/blitz-test
npx blitz db seed
Test A:
Test B:
What are detailed steps to reproduce this?
-
Run
blitz -v
and paste the output here:Please include below any other applicable logs and screenshots that show your problem:
No response