Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

@W-17550783 - [Passwordless login] Redirect customer to page prior to login #2221

Open
wants to merge 19 commits into
base: feature-passwordless-social-login
Choose a base branch
from

Conversation

yunakim714
Copy link
Collaborator

@yunakim714 yunakim714 commented Jan 27, 2025

Description

As a shopper, I want to be redirected back to the page I was just on after clicking the passwordless login link so that I can continue shopping from where I left off and save time. For example, when the shopper initiates passwordless login from the checkout page, they should be redirected back to the checkout page after clicking the magic link in their email.

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Breaking change (could cause existing functionality to not work as expected)
  • Other changes (non-breaking changes that does not fit any of the above)

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

  • Add query param redirect_url to the magic link that is sent to the user
  • When user clicks the magic link, this query param is read and the user is redirected if populated
  • Encode the magic link that is sent to users

How to Test-Drive This PR

  • Go to https://wasatch-mrt-passwordless-poc.mrt-storefront-staging.com
  • Initiate the passwordless login flow from different locations then verify that clicking the magic link from your email redirects you back to the correct pages:
    • Checkout page -> Checkout page
    • Login page -> Account page
    • Any other page through the Login modal (i.e. PDP, PLP, Search page) -> Back to same page

Run unit tests:
npm run test app/hooks/use-auth-modal.test.js
npm run test app/pages/login/passwordless-landing.test.js
npm run test app/pages/checkout/partials/contact-info.test.js

Checklists

General

  • Changes are covered by test cases
  • CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

You must check off all items in one of the follow two lists:

  • There are no changes to UI

or...

Localization

  • Changes include a UI text update in the Retail React App (which requires translation)

@yunakim714 yunakim714 added the skip changelog Skip the "Changelog Check" GitHub Actions step even if the Changelog.md files are not updated label Jan 27, 2025
@yunakim714 yunakim714 marked this pull request as ready for review January 27, 2025 18:04
@yunakim714 yunakim714 requested a review from a team as a code owner January 27, 2025 18:04
Copy link
Collaborator

@hajinsuha1 hajinsuha1 left a comment

Choose a reason for hiding this comment

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

wow it was simpler than i thought! nice 🎉

@yunakim714 yunakim714 requested a review from alexvuong January 28, 2025 18:14
@@ -105,6 +105,8 @@ export const AuthModal = ({

const handlePasswordlessLogin = async (email) => {
try {
// Save the path where the user logged in
window.localStorage.setItem('returnToPage', window.location.pathname)
Copy link
Collaborator

@alexvuong alexvuong Jan 28, 2025

Choose a reason for hiding this comment

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

What was the reason behind using localStorage? Can we use a query param to avoid having to handle this variable? Something like https://pwa-kit.storefront.com/login?token&1234&redirectTo=/checkout

Copy link
Collaborator

Choose a reason for hiding this comment

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

There are a few benefits of using query param than localStorage. One of which is it is not tied to a device. Imagine a shopper clicks login with link on a laptop, and then they check email and click on the magic link to logic. Can they resume their shopping from their?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@alexvuong To send the redirect_url I would have to pass it in as part of the callbackURL param when calling the 1st authorizePasswordless API call so I thought this method would be more straightforward (though Blair did say that passing in the query params as part of the callback URL should be fine).

Copy link
Collaborator

Choose a reason for hiding this comment

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

@yunakim714 Why would we need to pass it into the callbackURL ? 🤔

Copy link
Collaborator

@alexvuong alexvuong Jan 28, 2025

Choose a reason for hiding this comment

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

I'd suggest we try the query param solution if possible since the localStorage can potential cause more bugs. There are some edge cases that will potentially disable the redirection. Say, what if users clear their storage before they click on the magic link? or when they use incognito browser?

mockAuthHelperFunctions[AuthHelpers.AuthorizePasswordless].mutateAsync
).toHaveBeenCalledWith({
userid: validEmail,
callbackURI: 'https://webhook.site/27761b71-50c1-4097-a600-21a3b89a546c?redirectUrl=/'
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: is there a way to add a unit test that verfies when passwordlessConfigCallback is a relative path it creates the URL correclty?

Comment on lines 176 to 178
if (redirectPath) {
navigate(redirectPath)
setRedirectPath('')
Copy link
Collaborator

Choose a reason for hiding this comment

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

would we consider adding a unit test to verify that redirectPath is correctly navigated to?

@hajinsuha1
Copy link
Collaborator

hajinsuha1 commented Jan 30, 2025

@yunakim714 i'm finding when I try to login via the /login page, the redirect_url is set to undefined

Screenshot 2025-01-30 at 10 12 54 AM

clicking the link redirects me to a page not found page

Screenshot 2025-01-30 at 10 15 10 AM

@@ -77,7 +79,7 @@ export async function jwksCaching(req, res, options) {

// JWKS rotate every 30 days. For now, cache response for 14 days so that
// fetches only need to happen twice a month
res.set('Cache-Control', 'public, max-age=1209600')
res.set('Cache-Control', 'public, max-age=1209600, stale-while-revalidate=86400')
Copy link
Collaborator

Choose a reason for hiding this comment

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

What was the reason we add this in?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This allows us to use the cached response for 1 day while the server/CDN fetches a fresh response. I added this in to prevent errors from occurring when the cache expires and the server needs to call the /jwks endpoint again.

}
const redirectTo = redirectPath ? redirectPath : '/account'
navigate(redirectTo)
setRedirectPath('')
Copy link
Collaborator

@alexvuong alexvuong Jan 31, 2025

Choose a reason for hiding this comment

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

I feel like this line here can potentially cause react warning about unsettable state. We are navigating away before the state can be set.
I tried to check out code locally to test out, but I keep running into error with typescript from commerce-sdk-react despite some effects with workarounds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip changelog Skip the "Changelog Check" GitHub Actions step even if the Changelog.md files are not updated
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants