Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 17, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Extract helper function out
  • Loading branch information
bryphe-coder committed Feb 16, 2022
commit a88b109eef980aa517a54a1ae84a11211b31b8ec
18 changes: 11 additions & 7 deletions site/components/SignIn/SignInForm.tsx
Original file line number Diff line number Diff 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 Down Expand Up @@ -63,12 +63,7 @@ export const SignInForm: React.FC<SignInProps> = ({
// Tell SWR to invalidate the cache for the user endpoint
await mutate("/api/v2/users/me")

let redirect = "/"
if (router.query?.redirect) {
redirect = firstOrItem(router.query.redirect, redirect)
}

console.log("Using redirect: " + redirect)
const redirect = getRedirectFromRouter(router)
await router.push(redirect)
} catch (err) {
helpers.setFieldError("password", "The username or password is incorrect.")
Expand Down Expand Up @@ -125,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
}
}