Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: Redirect to '?redirect' query parameter after successful login#307

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bryphe-coder merged 3 commits intomainfrombryphe/fix/304/login-redirect
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletionsite/components/SignIn/SignInForm.test.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,29 @@ describe("SignInForm", () => {
act(() => elem.click())

// Then
// Should redirect because login wassuccessfully
// Should redirect because login wassuccessful
await waitFor(() => expect(singletonRouter).toMatchObject({ asPath: "/" }))
})

it("respects ?redirect query parameter when complete", async () => {
// Given
const loginHandler = (_email: string, _password: string) => Promise.resolve()
// Set a path to redirect to after login is successful
mockRouter.setCurrentUrl("/login?redirect=%2Fsome%2Fother%2Fpath")

// When
// Render the component
const { container } = render(<SignInForm loginHandler={loginHandler} />)
// Set user / password
const inputs = container.querySelectorAll("input")
fireEvent.change(inputs[0], { target: { value: "test@coder.com" } })
fireEvent.change(inputs[1], { target: { value: "password" } })
// Click sign-in
const elem = await screen.findByText("Sign In")
act(() => elem.click())

// Then
// Should redirect to /some/other/path because ?redirect was specified and login was successful
await waitFor(() => expect(singletonRouter).toMatchObject({ asPath: "/some/other/path" }))
})
})
16 changes: 14 additions & 2 deletionssite/components/SignIn/SignInForm.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
import { makeStyles } from "@material-ui/core/styles"
import { FormikContextType, useFormik } from "formik"
import { useRouter } from "next/router"
import {NextRouter,useRouter } from "next/router"
import React from "react"
import { useSWRConfig } from "swr"
import * as Yup from "yup"
Expand All@@ -9,6 +9,7 @@ import { Welcome } from "./Welcome"
import { FormTextField } from "../Form"
import * as API from "./../../api"
import { LoadingButton } from "./../Button"
import { firstOrItem } from "../../util/array"

/**
* BuiltInAuthFormValues describes a form using built-in (email/password)
Expand DownExpand Up@@ -61,7 +62,9 @@ export const SignInForm: React.FC<SignInProps> = ({
await loginHandler(email, password)
// Tell SWR to invalidate the cache for the user endpoint
await mutate("/api/v2/users/me")
await router.push("/")

const redirect = getRedirectFromRouter(router)
await router.push(redirect)
} catch (err) {
helpers.setFieldError("password", "The username or password is incorrect.")
}
Expand DownExpand Up@@ -117,3 +120,12 @@ export const SignInForm: React.FC<SignInProps> = ({
</>
)
}

const getRedirectFromRouter = (router: NextRouter) => {
const defaultRedirect = "/"
if (router.query?.redirect) {
return firstOrItem(router.query.redirect, defaultRedirect)
} else {
return defaultRedirect
}
}

[8]ページ先頭

©2009-2025 Movatter.jp