简体   繁体   English

React-Router v6:如何跳过/替换身份验证流程的多个历史条目?

[英]React-Router v6: How to skip/replace multiple history entries of authentication flow?

According to the documentation https://reactrouter.com/docs/en/v6/examples/auth and common sense it is good practice to 'replace' login routes after success authentication.根据文档https://reactrouter.com/docs/en/v6/examples/auth和常识,在成功验证后“替换”登录路由是一种很好的做法。

The use of navigate("...", { replace: true }) to replace the /login route in the history stack so the user doesn't return to the login page when clicking the back button after logging in使用 navigate("...", { replace: true }) 替换历史堆栈中的 /login 路由,这样用户在登录后点击返回按钮时不会返回登录页面

Everything is clean if there is only one login route.如果只有一个登录路径,一切都是干净的。

Question: what to do if there are multiple routes in authentication flow?问题:如果认证流程中有多个路由怎么办?

For example: /login/step-one -> /login/step-two -> /login/step-blabla .例如: /login/step-one -> /login/step-two -> /login/step-blabla Number of steps may depend on user settings and/or authentication conditions.步骤数可能取决于用户设置和/或验证条件。 it would also be correct to let the user navigate between steps back in the authorization process (for example, choosing a different confirmation method).让用户在授权过程的步骤之间导航也是正确的(例如,选择不同的确认方法)。

Issues:问题:

If there is a multi-step login process ( I don't know why'd you make it more difficult to log in ) then in this case I'd say using redirects to maintain control over the history stack so the browser's/device's back button goes back to the correct previous page is what you'd want to use.如果有一个多步骤登录过程(我不知道你为什么让登录变得更加困难),那么在这种情况下,我会说使用重定向来保持对历史堆栈的控制,以便浏览器/设备返回按钮返回正确的上一页是您想要使用的。 It may be better to think of these as "steps" rather than "pages" that have been navigated to.将这些视为“步骤”而不是已导航到的“页面”可能会更好。 The multi-step form process should have the UI to handle moving forward/backward through steps while any back navigation buttons ( browser or UI ) will correctly navigate back to the page prior to starting the stepper flow.多步骤表单流程应该有 UI 来处理通过步骤向前/向后移动,而任何后退导航按钮(浏览器或 UI )将在开始步进流程之前正确导航回页面。

Example:例子:

UI用户界面 Action行动 History Stack历史堆栈 Path小路 Back Location返回位置
initial最初的 ["/"] "/"
Start login flow开始登录流程 PUSH "/login/step-one" PUSH "/login/step-one" ["/", "/login/step-one"] "/login/step-one" "/"
Step 2第2步 REPLACE "/login/step-two"替换“/登录/第二步” ["/", "/login/step-two"] "/login/step-two" "/"
Step N步骤 N REPLACE "/login/step-blabla"替换“/login/step-blabla” ["/", "/login/step-blabla"] "/login/step-blabla" "/"
Back to Step 2返回第 2 步 REPLACE "/login/step-two"替换“/登录/第二步” ["/", "/login/step-two"] "/login/step-two" "/"
Back to Step 1返回步骤 1 REPLACE "/login/step-one"替换“/登录/第一步” ["/", "/login/step-one"] "/login/step-one" "/"
Forward to Step 2前进到第 2 步 REPLACE "/login/step-two"替换“/登录/第二步” ["/", "/login/step-two"] "/login/step-two" "/"
Forward to Step N前进到步骤 N REPLACE "/login/step-blabla"替换“/login/step-blabla” ["/", "/login/step-blabla"] "/login/step-blabla" "/"
Complete完全的 REPLACE page accessed替换访问的页面 ["/"] "/" "/"

This requires the multi-step login process to know its steps and current step so it can "navigate" between steps within the current history stack entry.这要求多步骤登录过程知道其步骤和当前步骤,以便它可以在当前历史堆栈条目中的步骤之间“导航”。 Note that the "back location" is maintained to be the page entry prior to starting the login flow and that at any time the back button/back navigation is issued the app goes back to this page.请注意,“返回位置”被维护为开始登录流程之前的页面条目,并且在任何时候发出返回按钮/返回导航时,应用程序都会返回此页面。

An alternative is to not use redirects at all and keep a count of how many pages/steps the user stepped through and issue a manual back navigation that many steps when complete.另一种方法是根本不使用重定向,并记录用户经过的页面/步骤数,并在完成发出手动返回导航。

Example:例子:

UI用户界面 Action行动 History Stack历史堆栈 Path小路 Back Location返回位置 Count数数
initial最初的 ["/"] "/" 0 0
Start login flow开始登录流程 PUSH "/login/step-one" PUSH "/login/step-one" ["/", "/login/step-one"] "/login/step-one" "/" 1 1
Step 2第2步 PUSH "/login/step-two" PUSH "/登录/第二步" ["/", "/login/step-one", "/login/step-two"] "/login/step-two" "/login/step-one" 2 2
Step N步骤 N PUSH "/login/step-blabla" PUSH "/login/step-blabla" ["/", "/login/step-one", "/login/step-two", "/login/step-blabla"] "/login/step-blabla" "/login/step-two" N ñ
Back to Step 2返回第 2 步 POP (1)流行音乐 (1) ["/", "/login/step-one", "/login/step-two"] "/login/step-two" "/" 2 2
Back to Step 1返回步骤 1 POP (1)流行音乐 (1) ["/", "/login/step-one"] "/login/step-one" "/" 1 1
Forward to Step 2前进到第 2 步 PUSH "/login/step-two" PUSH "/登录/第二步" ["/", "/login/step-one", "/login/step-two"] "/login/step-two" "/login/step-one" 2 2
Forward to Step N前进到步骤 N PUSH "/login/step-blabla" PUSH "/login/step-blabla" ["/", "/login/step-one", "/login/step-two", "/login/step-blabla"] "/login/step-blabla" "/login/step-two" N ñ
Complete完全的 POP (-Count) POP(-计数) ["/"] "/"

This also requires the multi-step login process to know its steps and current step count so it can "navigate" between steps so when the flow is complete it can correctly navigate back the correct number of page entries.这也需要多步骤登录过程知道其步骤和当前步骤计数,以便它可以在步骤之间“导航”,因此当流程完成时,它可以正确导航回正确数量的页面条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM