This repository was archived by the owner on Sep 30, 2024. It is now read-only.
forked from gladly-team/next-firebase-auth
-
Notifications
You must be signed in to change notification settings - Fork 1
[WIP] feat: add generalized redirect AuthAction #2
Open
offgriddev
wants to merge
1
commit into
main
Choose a base branch
from
jeryanders/custom-redirects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ const withAuthUser = | |
appPageURL = null, | ||
authPageURL = null, | ||
LoaderComponent = null, | ||
onRedirect = null, | ||
} = {}) => | ||
(ChildComponent) => { | ||
const WithAuthUserHOC = (props) => { | ||
|
@@ -117,6 +118,12 @@ const withAuthUser = | |
? authRequestCompleted | ||
: true) | ||
|
||
const shouldUseCustomRedirect = [ | ||
whenAuthed, | ||
whenUnauthedBeforeInit, | ||
whenUnauthedAfterInit, | ||
].includes(AuthAction.REDIRECT) | ||
|
||
const router = useRouter() | ||
const redirectToApp = useCallback(() => { | ||
logDebug('Redirecting to app.') | ||
|
@@ -160,6 +167,36 @@ const withAuthUser = | |
} | ||
router.replace(destination) | ||
}, [router, AuthUser]) | ||
const redirectToCustom = useCallback(() => { | ||
logDebug('Redirecting to user-specified endpoint') | ||
const redirectConfig = onRedirect || getConfig().onRedirect | ||
if (!redirectConfig) { | ||
throw new Error( | ||
'The "onRedirect" config setting must be set when using `REDIRECT`.' | ||
) | ||
} | ||
|
||
const authStateConfig = isAuthed | ||
? onRedirect.whenAuthed | ||
: onRedirect.whenUnauthed | ||
|
||
if (!authStateConfig || !authStateConfig.destination) { | ||
throw new Error( | ||
`The "destination" in the "onRedirect.whenAuthed" and "onRedirect.whenUnauthed" redirect configs must resolve to a non-empty string` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: We could technically make this only call out the offender here instead of both (with a conditional like you did above) |
||
) | ||
} | ||
|
||
if ( | ||
authStateConfig && | ||
(!authStateConfig.destination || | ||
typeof authStateConfig.destination !== 'string') | ||
) { | ||
throw new Error( | ||
'The "destination" must be set to a non-empty string or resolve to a non-empty string' | ||
) | ||
} | ||
router.replace(authStateConfig.destination) | ||
}, [router, isAuthed]) | ||
|
||
useEffect(() => { | ||
// Only redirect on the client side. To redirect server-side, | ||
|
@@ -171,12 +208,16 @@ const withAuthUser = | |
redirectToApp() | ||
} else if (shouldRedirectToLogin) { | ||
redirectToLogin() | ||
} else if (shouldUseCustomRedirect) { | ||
redirectToCustom() | ||
} | ||
}, [ | ||
shouldRedirectToApp, | ||
shouldRedirectToLogin, | ||
shouldUseCustomRedirect, | ||
redirectToApp, | ||
redirectToLogin, | ||
redirectToCustom, | ||
]) | ||
|
||
// Decide what to render. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ const withAuthUserTokenSSR = | |
whenUnauthed = AuthAction.RENDER, | ||
appPageURL = null, | ||
authPageURL = null, | ||
onRedirect = null, | ||
} = {}, | ||
{ useToken = true } = {} | ||
) => | ||
|
@@ -144,6 +145,39 @@ const withAuthUserTokenSSR = | |
} | ||
} | ||
|
||
if ([whenAuthed, whenUnauthed].includes(AuthAction.REDIRECT)) { | ||
const redirectConfig = onRedirect || getConfig().onRedirect | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice - I like the overridable with local config with fallback to global |
||
if (!redirectConfig) { | ||
throw new Error( | ||
`When "whenAuthed" or "whenUnauthed" are set to AuthAction.REDIRECT, "onRedirect" must be set.` | ||
) | ||
} | ||
|
||
const authStateConfig = AuthUser.id | ||
? onRedirect.whenAuthed | ||
: onRedirect.whenUnauthed | ||
|
||
if (!authStateConfig || !authStateConfig.destination) { | ||
throw new Error( | ||
`The "destination" in the "onRedirect.whenAuthed" and "onRedirect.whenUnauthed" redirect configs must resolve to a non-empty string` | ||
) | ||
} | ||
|
||
if ( | ||
authStateConfig && | ||
(!authStateConfig.destination || | ||
typeof authStateConfig.destination !== 'string') | ||
) { | ||
throw new Error( | ||
'The "destination" must be set to a non-empty string or resolve to a non-empty string' | ||
) | ||
} | ||
|
||
return { | ||
redirect: authStateConfig, | ||
} | ||
} | ||
|
||
// Prepare return data | ||
let returnData = { props: { AuthUserSerialized } } | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although that implies and - was the OR intentional before? Or were you intending to do:So that it is defined, but missing the destination. Thats what the error message seemed to imply anywayThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe I'm misunderstanding since that seems to be what you do below?