Skip to content

Commit

Permalink
add logic to auto handle role with usage of redirectAuthenticatedTo
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhsuresh committed Nov 28, 2023
1 parent fb232d1 commit 59a7221
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/blitz-auth/src/server/auth-sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,17 @@ export async function useAuthenticatedBlitzContext({
? redirectAuthenticatedTo
: formatWithValidation(redirectAuthenticatedTo)
debug("[useAuthenticatedBlitzContext] Redirecting to", redirectUrl)
log.info("Authentication Redirect: " + customChalk.dim("(Authenticated)"), redirectUrl)
redirect(redirectUrl)
if (role) {
try {
ctx.session.$authorize(role)
} catch (e) {
log.info("Authentication Redirect: " + customChalk.dim(`Role ${role}`), redirectTo)
redirect(redirectUrl)
}
} else {
log.info("Authentication Redirect: " + customChalk.dim("(Authenticated)"), redirectUrl)
redirect(redirectUrl)
}
}
if (redirectTo && role) {
debug("[useAuthenticatedBlitzContext] redirectTo and role are both defined.")
Expand Down

0 comments on commit 59a7221

Please sign in to comment.