Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add ability to define to redirect to app with different base path #352

Merged
merged 27 commits into from
Dec 29, 2021

Conversation

offgriddev
Copy link
Contributor

@offgriddev offgriddev commented Dec 10, 2021

Overview

This PR adds the ability to redirect to other app through the appPageURL and authPageURL config. On the client-side, when an AuthAction.REDIRECT_TO_LOGIN or AuthAction.REDIRECT_TO_APP are used, the current redirect logic always uses the next router, which only redirects within the current app. However, in micro-UI scenarios, these apps, while under the same domain, can be hosted in different apps with different base routes.

Currently, we can set these two URL config parameters as either strings or functions. By allowing users to assign an object to these configs, users can specify the metadata for the URL.

Changes

This PR adds support for this scenario by allowing the authPageURL and appPageURL to take object literals with the following schema:

{
   "destination": string,
   "basePath": boolean 
}

Due to the increased complexity of the shared redirect logic around locating the destination URL in withAuthUser and withAuthUserTokenSSR, I've extracted some common redirect functions in src/redirects. In doing so, I've unified the error messaging in the two components, which were just slightly different. I've also organized the component withAuthUser and separated the now more complex routing logic into a separate useCallback. The routing is done through window.location.assign if the user is being redirected to another app or router.route if the user is being redirect to the current app. The SSR component takes the whole object literal when assigned as it represents the SSR redirect object.

I've also cleaned up some documentation and added the favor around the new schema for authPageURL and appPageURL.

Checks

  • Non-breaking API changes
  • Tests

Issues

@vercel
Copy link

vercel bot commented Dec 10, 2021

@offgriddev is attempting to deploy a commit to the Gladly Team on Vercel.

A member of the Team first needs to authorize it.

@prescottprue
Copy link
Contributor

prescottprue commented Dec 10, 2021

👍 Super helpful for us since it addresses #187

@offgriddev offgriddev changed the title feat: add ability to specify custom redirect auth action feat: add ability to define appPageURL and authPageURL with object literal with ability to redirect to app with different base path Dec 13, 2021
@offgriddev offgriddev changed the title feat: add ability to define appPageURL and authPageURL with object literal with ability to redirect to app with different base path feat: add ability to define to redirect to app with different base path Dec 13, 2021
@offgriddev offgriddev changed the title feat: add ability to define to redirect to app with different base path [wip] feat: add ability to define to redirect to app with different base path Dec 13, 2021
@offgriddev offgriddev marked this pull request as draft December 13, 2021 23:47
@offgriddev offgriddev changed the title [wip] feat: add ability to define to redirect to app with different base path feat: add ability to define to redirect to app with different base path Dec 13, 2021
@codecov
Copy link

codecov bot commented Dec 14, 2021

Codecov Report

Merging #352 (bb1185c) into main (0b76d60) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #352   +/-   ##
=======================================
  Coverage   99.27%   99.27%           
=======================================
  Files          25       26    +1     
  Lines         551      554    +3     
  Branches      202      188   -14     
=======================================
+ Hits          547      550    +3     
  Misses          4        4           
Impacted Files Coverage Δ
src/redirects.js 100.00% <100.00%> (ø)
src/withAuthUser.js 100.00% <100.00%> (ø)
src/withAuthUserTokenSSR.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0b76d60...bb1185c. Read the comment docs.

@offgriddev offgriddev marked this pull request as ready for review December 14, 2021 03:40
@offgriddev
Copy link
Contributor Author

@kmjennison Hope you are well. I've reworked this PR to focus on resolving only issue #187. In the PR, I've allowed an object to be set or resolved to authPageURL or appPageURL that allows for redirecting to another app through a window.location.assign call instead of a call to the next router. I also included a focused refactor of some of the redirect code that I saw duplicated across the SSR and CSR code. One side effect to that is the error messaging changed slightly. The errors were slightly different in the two components, but I found the more generic messaged in the CSR component worked the best, so I kept that one. Let me know what you think or if you have any questions! 👋🏼

src/redirects.js Outdated Show resolved Hide resolved
@kmjennison
Copy link
Contributor

@offgriddev Thanks for this reworking!

One question I have is how Next.js handles the base path in its server-side redirect returned in getServerSideProps. I couldn't find documentation on this. Do you know if it always uses the base path, never uses the base path, or if that's configurable?

We'd want identical redirect URLs regardless of the redirect happening client-side or server-side. I haven't had a chance to look more closely, but it seems like they'd differ because the server-side redirect doesn't have any base path logic. Is that right?

@offgriddev
Copy link
Contributor Author

offgriddev commented Dec 15, 2021

One question I have is how Next.js handles the base path in its server-side redirect returned in getServerSideProps. I couldn't find documentation on this. Do you know if it always uses the base path, never uses the base path, or if that's configurable?

@kmjennison it actually looks like basePath is an undocumented property for redirect on getServerSideProps: https://github.com/vercel/next.js/blob/59f7676966570093f56ff35fb0d83eb97b394b7b/packages/next/server/base-server.ts#L1899

I’m going to look into this a bit more, but this looks to be the place where the redirect is handled on the server. If this is true, there is a little bit more I can do here in the SSR component.

@offgriddev
Copy link
Contributor Author

@kmjennison Let me know what you think of the change I pushed. With the discovery of the basePath functionality implemented on the next server, the object literal for authPageURL and appPageURL could match the redirect object and be passed directly. To me, it cleans things up a bit and reduces the mapping, but let me know your thoughts!

@offgriddev
Copy link
Contributor Author

@kmjennison NextJS responded to my issue quickly, and now the basePath is documented for SSR :)

Copy link
Contributor

@kmjennison kmjennison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pushing this forward! It's great that Next supports what we need for this feature.

Requested changes. LMK if you have any questions.

src/withAuthUser.js Outdated Show resolved Hide resolved
src/__tests__/withAuthUser.test.js Show resolved Hide resolved
src/redirects.js Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
src/redirects.js Outdated Show resolved Hide resolved
src/redirects.js Show resolved Hide resolved
src/redirects.js Outdated Show resolved Hide resolved
src/__tests__/withAuthUserTokenSSR.test.js Outdated Show resolved Hide resolved
src/__tests__/withAuthUserTokenSSR.test.js Outdated Show resolved Hide resolved
@offgriddev
Copy link
Contributor Author

@kmjennison Thanks for the review! I’ll get a revision up later today :)

@kmjennison
Copy link
Contributor

With the greater flexibility in the authPageURL and appPageURL types, we'll also have to update the config section of the README and the TypeScript definitions.

@offgriddev offgriddev force-pushed the offgriddev/custom-redirects branch from 623b5d5 to fd09c86 Compare December 20, 2021 20:48
@offgriddev
Copy link
Contributor Author

@kmjennison 👋🏼 I hope your Monday is going well. I worked through your feedback. I just pushed some changes for your review when you get a chance.

In adding tests for the redirect module, I moved the logic for getting the item from the component config or the global config for the redirect. This was the code that looked like this const redirectDestination = appPageURL || getConfig().appPageURL. This allowed the caller to only need getRedirectToLoginDestination({ AuthUser, ctx }).

I also updated the TS and the README. Let me know if you want me to add any additional context there. Let me know if you'd like me to make any further adjustments!

Copy link
Contributor

@kmjennison kmjennison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@offgriddev Thanks for these changes. Another round of feedback. A few notes:

  • Some feedback (esp for redirect logic) is opinionated about code clarity. If you'd prefer to ignore it, I can follow up with a PR to take care of those changes.
  • Please push additional commits without squashing. That helps with code review on GitHub to compare what's changed since the last review.
  • Nice work on the tests!

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
src/withAuthUser.js Outdated Show resolved Hide resolved
src/withAuthUser.js Outdated Show resolved Hide resolved
src/withAuthUserTokenSSR.js Outdated Show resolved Hide resolved
src/__tests__/redirects.test.js Show resolved Hide resolved
src/__tests__/withAuthUser.test.js Outdated Show resolved Hide resolved
src/__tests__/withAuthUser.test.js Outdated Show resolved Hide resolved
src/__tests__/withAuthUser.test.js Show resolved Hide resolved
getDestination({
ctx,
AuthUser,
redirectDestination: redirectURL || getConfig()[redirectConfigName],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic really cleaned up

Copy link
Contributor Author

@offgriddev offgriddev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmjennison I just finished working through the feedback. Let me know if you have anything else, but I'm feeling good about these changes. Thanks for the helpful comments and suggestions on this PR!

I pushed up the commits as requested without squashing. Although, not sure how I managed to get yarn.lock changes here. I believe it happened after the last release, so probably user-error on my part.

@offgriddev
Copy link
Contributor Author

@kmjennison I hope you're well. Is there any chance we can get this merged into the next release, or do you think there are some changes required still? Let me know. Happy Holidays to you if I don't hear back from you before next week.

@vercel
Copy link

vercel bot commented Dec 29, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/gladly-team/nfa-example/FescHTjWiz3NQ8YNxQoKFmRSbj9f
✅ Preview: https://nfa-example-git-fork-reside-eng-offgriddev-c-742a68-gladly-team.vercel.app

Copy link
Contributor

@kmjennison kmjennison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! @offgriddev thanks for the good work and iterations. I'll get this out in a release soon.

@kmjennison kmjennison merged commit 86c9bab into gladly-team:main Dec 29, 2021
@offgriddev
Copy link
Contributor Author

@kmjennison my pleasure! It was great to work with you through it!

kmjennison pushed a commit that referenced this pull request Dec 29, 2021
…th (#352)

* feat: add generalized redirect AuthAction

* test: add tests for csr and ssr withAuthUser

* fix: made error message more focused on a given auth state

* fix: removed new AuthAction and address issue 187 with new solution

* test: add main tests for new schema on appPageURL and authPageURL

* refactor: move destination logic into common function

* refactor: add redirects

* refactor: error messaging around redirects

* fix: router and window location

* test: add test coverage for new supported data type

* docs: rework docs

* fix: error message for authPageURL

* fix: bug in ssr component and slightly later url schema

* docs: change schema property

* fix: allow basePath on SSR to be passed based on findings

* chore: make naming consistent between csr / ssr

* fix: rework based on feedback

* refactor: bring config access into redirect module

* style: spacing and formatting

* docs: update readme and types

* refactor: simplify object name

* fix: update typescript

* tests: fix tests causing coverage issues

* fix: rework from feedback

* fix: implement feedback

* test: add additional test

* test: adjust test name
kmjennison added a commit that referenced this pull request Dec 29, 2021
* feat: add ability to define to redirect to app with different base path (#352)

* feat: add generalized redirect AuthAction

* test: add tests for csr and ssr withAuthUser

* fix: made error message more focused on a given auth state

* fix: removed new AuthAction and address issue 187 with new solution

* test: add main tests for new schema on appPageURL and authPageURL

* refactor: move destination logic into common function

* refactor: add redirects

* refactor: error messaging around redirects

* fix: router and window location

* test: add test coverage for new supported data type

* docs: rework docs

* fix: error message for authPageURL

* fix: bug in ssr component and slightly later url schema

* docs: change schema property

* fix: allow basePath on SSR to be passed based on findings

* chore: make naming consistent between csr / ssr

* fix: rework based on feedback

* refactor: bring config access into redirect module

* style: spacing and formatting

* docs: update readme and types

* refactor: simplify object name

* fix: update typescript

* tests: fix tests causing coverage issues

* fix: rework from feedback

* fix: implement feedback

* test: add additional test

* test: adjust test name

* Cherry-pick PR #400

Co-authored-by: Jesse Anderson <jesse.anderson@sideinc.com>
kmjennison added a commit that referenced this pull request Jul 8, 2023
* Debug release action

* 0.14.0-alpha.0

* Revert "0.14.0-alpha.0"

This reverts commit 1e04c95.

* Remove --dry-run from release action

* Display the Firebase version on the demo app

* Update README.md

* Update issue templates

* feat: forceRefresh on getIdToken

* feat: adding description about forceRefresh

* feat: adding argument type to getIdToken

* Update createAuthUser.js

* 0.13.4-alpha.0

* 0.13.4

* Upgrade some dependencies (#325)

* Upgrade some deps

* Upgrade caniuse

* Upgrade Prettier

* Downgrade eslint to satisfy peerdeps

* Minor upgrades for Next and Firebase deps

* Upgrade firebase-admin

* Upgrade firebase-admin in demo

* Minor upgrade demo deps

* Upgrade NFA version in demo

* Upgrade more dependencies

* Upgrade more demo dependencies

* Revert "Upgrade more demo dependencies"

This reverts commit 7da3e58.

* Revert "Minor upgrade demo deps"

This reverts commit 50f928e.

* Minor upgrade Firebase

* Pin typescript version (typing error with 4.4.4) and minor upgrade other types

* Minor bump a few demo deps

* Upgrade more demo deps

* Upgrade some deps

* Remove unused Codecov dependency

* Support Next 12 and Firebase Admin 10 (#328)

* Use Next 12

* Allow latest versions of Next and firebase-admin

* 0.13.5-alpha.0

* Update demo to use Next 12  (#330)

* Use Next 12

* Allow latest versions of Next and firebase-admin

* Upgrade Next to v12

* Update demo

* Update README.md

* Update README.md

* Change example app cookies to use SameSite=lax (#354)

* 0.13.5

* feat: add support for application default credentials (#348)

* feat: fallback to applicationDefault credentials

Co-Authored-By: Jesse Anderson <jeryanders@gmail.com>

* chore: add test for firebaseAdminDefaultCredential

* chore: update README with firebaseAdminDefaultCredential

Co-Authored-By: Jesse Anderson <jeryanders@gmail.com>

* chore: cleanup wording in README comments

* fix: updates based on comments

* chore: update error message in test

Co-authored-by: Jesse Anderson <jeryanders@gmail.com>

* Update README.md

* Upgrade dependencies on v0.x (#356)

* Upgrade most deps

* Upgrade ESLint and Prettier and lint fix

* Remove unneeded jsdom dep

* Upgrade most example app deps

* Upgrade example app lockfile deps

* Upgrade lockfile deps

* Fix peer dependency range syntax for firebase-admin (#358)

* Handle additional token errors in verifyIdToken (#361) (#365)

* fix: check for 'auth/argument-error' when verifying token

* feat: upgrade firebase and firebase-admin

* feat(#174): handle additional errors from `verifyIdToken`

* test(#174): add tests coverage for new errors in `verifyIdToken`

* feat: upgrade dependencies that have non breaking changes

* feat: implement pr feedback

* chore: upgrade dependencies

* Rebuild lockfile

* Include error if empty refreshToken

* Add TODO

Co-authored-by: Zino Hofmann <zino@hofmann.amsterdam>

Co-authored-by: Zino Hofmann <zino@hofmann.amsterdam>

* Remove thrown errors from token refresh & verification logic (#368)

* Add broken tests

* Don't throw on token errors

* Add assertion checks to tests

* Add/modify comments in config validation

* Move tests into describe block

* Add error callbacks to config

* Call error callbacks when we fail to refresh or verify the user's ID token

* Add new config properties to types

* Await error callback functions in case they need to perform something async

* Lint fixes

* Add tests

* Update README.md

* Fix typo in README (#374)

* Cherry pick v1.x #369 (#375)

Co-authored-by: Faris Abusada <abusada@users.noreply.github.com>

* Allow easy error handling for login/logout requests (#376)

* Add error catching to default token changed handler

* Add new config properties

* Update README.md

* Update README.md

* Run Prettier on README (#381)

* 0.14.0-alpha.o

* Update README.md

* 0.14.0-alpha.1

* Update v0.x example (#382)

* Update documentation (#387)

* Update docs on private key formatting and Vercel environment variables (#385)

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Add note about not using API routes in getServerSideProps (#386)

* 0.14.0

* Added troubleshooting step to README (#398)

* Update README.md

* feat: add ability to define to redirect to app with different base path (#352)

* feat: add generalized redirect AuthAction

* test: add tests for csr and ssr withAuthUser

* fix: made error message more focused on a given auth state

* fix: removed new AuthAction and address issue 187 with new solution

* test: add main tests for new schema on appPageURL and authPageURL

* refactor: move destination logic into common function

* refactor: add redirects

* refactor: error messaging around redirects

* fix: router and window location

* test: add test coverage for new supported data type

* docs: rework docs

* fix: error message for authPageURL

* fix: bug in ssr component and slightly later url schema

* docs: change schema property

* fix: allow basePath on SSR to be passed based on findings

* chore: make naming consistent between csr / ssr

* fix: rework based on feedback

* refactor: bring config access into redirect module

* style: spacing and formatting

* docs: update readme and types

* refactor: simplify object name

* fix: update typescript

* tests: fix tests causing coverage issues

* fix: rework from feedback

* fix: implement feedback

* test: add additional test

* test: adjust test name

* Update documentation for redirects (#400)

* Add PageURL type to README

* Typo fix

* Link to PageURL type in docs

* Run Prettier on README

* Fix incorrect documentation args

* Link to PageURL type from example

* Tweak README

* Fix typo

* Add info about ctx

* Remove redundant info

* Tweak code comments

* 0.14.1-alpha.0

* Update example app (#402)

* 0.14.1

* added onLogoutRequestError and onLoginRequestError to InitConfig interface (#427)

* feat: add tenant integration

* Update link to documentation

* 0.14.2

* Bugfix: don't error on unset Firebase admin config values (#436)

* Identify bug

* When debug logging, handle unset Firebase config values

* Lint fix

* 0.14.3-alpha.0

* fix: Add useFirebaseAdminDefaultCredential type definition (#451)

* Fix README typos, grammar (#448)

* Add useFirebaseAdminDefaultCredential type definition

* Fix typos, grammar, and clarify Google default credentials usage

* docs: Fix grammar, remove type addition from PR

* 0.14.3-alpha.1

* 0.14.3

* Upgrade NFA version in demo (#455)

* Upgrade NFA version in demo

* Update min version

* v0.x: Add support for React 18 (#472)

* Add support for React 18

* Upgrade some testing libraries

* v0.x: upgrade dependencies (#477)

* Upgrade most deps

* Upgrade additional deps

* 0.14.4-alpha.0

* v0.x: Update example app (React 18, other dependencies) (#471)

* Update example to use React 18

* Add latest NFA

* Upgrade other deps in example app

* Ignore type error

* Use supported version of react-firebaseui

* Upgrade additional example app dependencies (#479)

* fix typo. add missing "b" to README.md (#485)

* Support firebase-admin v11 peer dependency (#504)

* Upgrade dependencies [v0.x] (#505)

* Upgrade some deps

* Upgrade Prettier

* Upgrade dependencies

* Upgrade firebase-admin and copy-webpack-plugin

* Upgrade example deps [v0.x] (#507)

* Upgrade deps

* Upgrade deps

* Use compatible react-firebaseui

* 0.14.4-alpha.1

* Bump NFA in package.json

* Upgrade NFA in example (#508)

* 0.14.4

* Use NFA 0.14.4 in example app (#509)

* Add info about NextAuth.js to README [v1.x]

* Update README.md

* v0.x: Update bug issue template (#542)

* v0.x: allow Next v13 peer dependency (#588)

* 0.15.0

* Update README.md

* docs: adds missing import to withAuthUserTokenSSR example

* fix: make sure Firebase admin is initialized in getUserFromCookies

* docs: tenantId commented by default

* fix: correct attribute name to tenantId in deserializedUser

* fix: extract tenantId from firebaseClientInitConfig

* tests: add tenantId tests for createAuthUser. adds tests for tenantId in initFirebaseClientSDK.

* merge fixes

* merge fixes

* merge fixes

* merge fixes

* fix: firebaseAdmin test. use getAuth

* remove extra changes from docs

* fix doc formatting

* fix minor issues

* empy line

* fix: change auth instance

* fix: remove admin import

* fix: move tenantId outside firebaseClientInitConfig. Fix typos

* Update src/__tests__/firebaseAdmin.test.ts

---------

Co-authored-by: Kevin Jennison <kevin.jennison1@gmail.com>
Co-authored-by: Guilherme <guiilherme.bayer@gmail.com>
Co-authored-by: Scott Prue <prescottprue@users.noreply.github.com>
Co-authored-by: Jesse Anderson <jeryanders@gmail.com>
Co-authored-by: Zino Hofmann <zino@hofmann.amsterdam>
Co-authored-by: Alexander Cai <alexandercai@outlook.com>
Co-authored-by: Faris Abusada <abusada@users.noreply.github.com>
Co-authored-by: Vinny <vpaladino778@gmail.com>
Co-authored-by: Jesse Anderson <jesse.anderson@sideinc.com>
Co-authored-by: camilo-mujica <84539709+camilo-mujica@users.noreply.github.com>
Co-authored-by: Hegar Garcia <hegargarcia@gmail.com>
Co-authored-by: Hung Vu <hunghvu2017@gmail.com>
Co-authored-by: nori-k <norikatsu.kamiya@gmail.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to ignore the basePath for authPageURL and appPageURL redirects at a global level
3 participants