1
1
import { makeStyles } from "@material-ui/core/styles"
2
2
import { FormikContextType , useFormik } from "formik"
3
- import { useRouter } from "next/router"
3
+ import { NextRouter , useRouter } from "next/router"
4
4
import React from "react"
5
5
import { useSWRConfig } from "swr"
6
6
import * as Yup from "yup"
@@ -9,6 +9,7 @@ import { Welcome } from "./Welcome"
9
9
import { FormTextField } from "../Form"
10
10
import * as API from "./../../api"
11
11
import { LoadingButton } from "./../Button"
12
+ import { firstOrItem } from "../../util/array"
12
13
13
14
/**
14
15
* BuiltInAuthFormValues describes a form using built-in (email/password)
@@ -61,7 +62,9 @@ export const SignInForm: React.FC<SignInProps> = ({
61
62
await loginHandler ( email , password )
62
63
// Tell SWR to invalidate the cache for the user endpoint
63
64
await mutate ( "/api/v2/users/me" )
64
- await router . push ( "/" )
65
+
66
+ const redirect = getRedirectFromRouter ( router )
67
+ await router . push ( redirect )
65
68
} catch ( err ) {
66
69
helpers . setFieldError ( "password" , "The username or password is incorrect." )
67
70
}
@@ -117,3 +120,12 @@ export const SignInForm: React.FC<SignInProps> = ({
117
120
</ >
118
121
)
119
122
}
123
+
124
+ const getRedirectFromRouter = ( router : NextRouter ) => {
125
+ const defaultRedirect = "/"
126
+ if ( router . query ?. redirect ) {
127
+ return firstOrItem ( router . query . redirect , defaultRedirect )
128
+ } else {
129
+ return defaultRedirect
130
+ }
131
+ }
0 commit comments