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

fix(router): Include queryParams in redirectTo #1526

Merged
merged 5 commits into from
Dec 11, 2020

Conversation

dac09
Copy link
Collaborator

@dac09 dac09 commented Nov 27, 2020

Fixed #1453

e.g. Say you're going to https://mysite.com/dashboard?param1=something&token=hunter2

It used to redirect to https://mysite.com/login?redirectTo=/dashboard. Now it'll redirect to https://mysite.com/login?redirectTo=/dashboard%3Fparam1%3Dsomething%26token%3Dhunter2

Adds tests for:

  • redirectTo
  • redirectTo with query params
  • Figure out why the test fails (see below) thanks @peterp

…e-routes

Only stop rendering on Private routes
@dac09 dac09 requested review from peterp and Tobbe November 27, 2020 14:33
@dac09
Copy link
Collaborator Author

dac09 commented Nov 27, 2020

This test, where the unauthenticated route is home passes, but not the login one. Shouldn't make a difference right?

test('unauthenticated user is redirected including search params', async () => {
  mockAuth(false)
  const TestRouter = () => (
    <Router useAuth={window.__REDWOOD__USE_AUTH}>
      <Route path="/" page={HomePage} name="home" />
      <Route path="/login" page={LoginPage} name="login" />
      <Private unauthenticated="home">
        <Route path="/private" page={PrivatePage} name="private" />
      </Private>
    </Router>
  )
  const screen = render(<TestRouter />)

  // starts on home page
  await waitFor(() => screen.getByText(/Home Page/i))

  // navigate to private page
  // should redirect to login
  act(() => navigate(routes.private({ bazinga: 'yeah' })))

  await waitFor(() => {
    expect(screen.queryByText(/Private Page/i)).toBeNull()
    expect(window.location.pathname).toBe('/')
    expect(window.location.search).toBe(
      `?redirectTo=/private${encodeURIComponent('?bazinga=yeah')}`
    )
    screen.getByText(/Home Page/i)
  })
})

test('authenticated user can access private page', async () => {
  mockAuth(true)
  const TestRouter = () => (
    <Router useAuth={window.__REDWOOD__USE_AUTH}>
      <Route path="/" page={HomePage} name="home" />
      <Private unauthenticated="home">
        <Route path="/private" page={PrivatePage} name="private" />
      </Private>
    </Router>
  )
  const screen = render(<TestRouter />)

  // starts on home page
  await waitFor(() => screen.getByText(/Home Page/i))

  // navigate to private page
  // should not redirect
  act(() => navigate(routes.private()))
  await waitFor(() => {
    expect(screen.getByText(/Private Page/)).toBeTruthy()
    expect(screen.queryByText(/Home Page/)).toBeNull()
  })
})

@dthyresson
Copy link
Contributor

This test, where the unauthenticated route is home passes, but not the login one. Shouldn't make a difference right?

@dac09 Could this recent change have affected the routes?

#1519

@peterp peterp added this to the Next Release milestone Dec 11, 2020
@github-actions
Copy link

github-actions bot commented Dec 11, 2020

@Tobbe Tobbe merged commit aae335d into redwoodjs:main Dec 11, 2020
@dac09 dac09 deleted the fix/redirect-to-full-path branch December 11, 2020 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

redirectTo in router does not maintain query params
4 participants