Skip to content

Commit 2faf192

Browse files
authored
fix: respect return to when switching between flows (#243)
1 parent 2840e03 commit 2faf192

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

src/routes/login.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ export const createLoginRoute: RouteCreator =
6666
.getLoginFlow({ id: flow, cookie: req.header("cookie") })
6767
.then(({ data: flow }) => {
6868
// Render the data using a view (e.g. Jade Template):
69-
7069
const initRegistrationQuery = new URLSearchParams({
71-
return_to: return_to.toString(),
70+
return_to:
71+
(return_to && return_to.toString()) || flow.return_to || "",
7272
})
7373
if (flow.oauth2_login_request?.challenge) {
7474
initRegistrationQuery.set(

src/routes/recovery.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ export const createRecoveryRoute: RouteCreator =
2424
new URLSearchParams({ return_to: return_to.toString() }),
2525
)
2626

27-
const initLoginUrl = getUrlForFlow(
28-
kratosBrowserUrl,
29-
"login",
30-
new URLSearchParams({ return_to: return_to.toString() }),
31-
)
32-
3327
// The flow is used to identify the settings and registration flow and
3428
// return data like the csrf_token and so on.
3529
if (!isQuerySet(flow)) {
@@ -43,6 +37,15 @@ export const createRecoveryRoute: RouteCreator =
4337
return frontend
4438
.getRecoveryFlow({ id: flow, cookie: req.header("cookie") })
4539
.then(({ data: flow }) => {
40+
const initLoginUrl = getUrlForFlow(
41+
kratosBrowserUrl,
42+
"login",
43+
new URLSearchParams({
44+
return_to:
45+
(return_to && return_to.toString()) || flow.return_to || "",
46+
}),
47+
)
48+
4649
res.render("recovery", {
4750
card: UserAuthCard({
4851
title: "Recover your account",

src/routes/registration.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ export const createRegistrationRoute: RouteCreator =
2222
(createHelpers) => (req, res, next) => {
2323
res.locals.projectName = "Create account"
2424

25-
const { flow, return_to = "", login_challenge } = req.query
25+
const { flow, return_to, after_verification_return_to, login_challenge } =
26+
req.query
2627
const { frontend, kratosBrowserUrl, logoUrl } = createHelpers(req, res)
2728

2829
const initFlowQuery = new URLSearchParams({
29-
return_to: return_to.toString(),
30+
...(return_to && { return_to: return_to.toString() }),
31+
...(after_verification_return_to && {
32+
after_verification_return_to: after_verification_return_to.toString(),
33+
}),
3034
})
3135

3236
if (isQuerySet(login_challenge)) {
@@ -59,7 +63,8 @@ export const createRegistrationRoute: RouteCreator =
5963
.then(({ data: flow }) => {
6064
// Render the data using a view (e.g. Jade Template):
6165
const initLoginQuery = new URLSearchParams({
62-
return_to: return_to.toString(),
66+
return_to:
67+
(return_to && return_to.toString()) || flow.return_to || "",
6368
})
6469
if (flow.oauth2_login_request?.challenge) {
6570
initLoginQuery.set(

src/routes/verification.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ export const createVerificationRoute: RouteCreator =
2424
new URLSearchParams({ return_to: return_to.toString() }),
2525
)
2626

27-
const initRegistrationUrl = getUrlForFlow(
28-
kratosBrowserUrl,
29-
"registration",
30-
new URLSearchParams({
31-
return_to: return_to.toString(),
32-
}),
33-
)
34-
3527
// The flow is used to identify the settings and registration flow and
3628
// return data like the csrf_token and so on.
3729
if (!isQuerySet(flow)) {
@@ -46,6 +38,14 @@ export const createVerificationRoute: RouteCreator =
4638
frontend
4739
.getVerificationFlow({ id: flow, cookie: req.header("cookie") })
4840
.then(({ data: flow }) => {
41+
const initRegistrationUrl = getUrlForFlow(
42+
kratosBrowserUrl,
43+
"registration",
44+
new URLSearchParams({
45+
return_to:
46+
(return_to && return_to.toString()) || flow.return_to || "",
47+
}),
48+
)
4949
// Render the data using a view (e.g. Jade Template):
5050
res.render("verification", {
5151
card: UserAuthCard({

0 commit comments

Comments
 (0)