Meteor + IronRouter——如何在登录后重定向用户而不阻止访问我的登陆页面?
Meteor + IronRouter -- how can I redirect the user after logging in without blocking access to my landing page?
在我的应用中,着陆页上有 loginButtons
。
我希望用户在成功登录后自动重定向到 /home
路由。
这段代码似乎完成了:
// Redirect to /home after logging in
Accounts.onLogin(function() {
Router.go("/home");
});
// Make sure the user is logged in when accessing other routes
Router.onBeforeAction((function() {
if (!Meteor.userId() && !Meteor.loggingIn()) {
Router.go("/");
}
this.next();
}), {
except: ["/"]
});
但是,当登录用户随后尝试再次访问登录页面时,他们将被重定向到 /home
路由。这有效地阻止了他们访问登录页面。
如何在登录后将用户重定向到 /home
,而不阻止他们之后访问登录页面?
我在我的应用程序中遇到了同样的问题。我基本上在 home 页面上做了以下操作:
<template name="home">
{{#if currentUser}}
{{> loggedInHome }}
{{else}}
{{> landing }}
{{/if}}
</template>
然后我创建了一条 路线 到 /landing
并提供了导航。结果是:
- 未登录用户到达
/
- 用户看到着陆页内容
- 用户登录或注册
- 用户被带回
/
/
现在显示主应用程序
- 登陆页面在登录用户的导航中可用
它可能不是最优雅的解决方案,但它完成了工作。
我设法使用 u2622:persistent-session
包修复了它:
// Redirect to /home after logging in
Accounts.onLogin(function() {
if (!Session.get("loginRedirected")){
Router.go("/home");
Session.setAuth("loginRedirected", true);
}
});
它添加的 Session.setAuth
方法在 localstorage
中创建会话变量,以便它们在页面刷新时保持不变。它甚至会在用户注销时自动清除它们。
在我的应用中,着陆页上有 loginButtons
。
我希望用户在成功登录后自动重定向到 /home
路由。
这段代码似乎完成了:
// Redirect to /home after logging in
Accounts.onLogin(function() {
Router.go("/home");
});
// Make sure the user is logged in when accessing other routes
Router.onBeforeAction((function() {
if (!Meteor.userId() && !Meteor.loggingIn()) {
Router.go("/");
}
this.next();
}), {
except: ["/"]
});
但是,当登录用户随后尝试再次访问登录页面时,他们将被重定向到 /home
路由。这有效地阻止了他们访问登录页面。
如何在登录后将用户重定向到 /home
,而不阻止他们之后访问登录页面?
我在我的应用程序中遇到了同样的问题。我基本上在 home 页面上做了以下操作:
<template name="home">
{{#if currentUser}}
{{> loggedInHome }}
{{else}}
{{> landing }}
{{/if}}
</template>
然后我创建了一条 路线 到 /landing
并提供了导航。结果是:
- 未登录用户到达
/
- 用户看到着陆页内容
- 用户登录或注册
- 用户被带回
/
/
现在显示主应用程序- 登陆页面在登录用户的导航中可用
它可能不是最优雅的解决方案,但它完成了工作。
我设法使用 u2622:persistent-session
包修复了它:
// Redirect to /home after logging in
Accounts.onLogin(function() {
if (!Session.get("loginRedirected")){
Router.go("/home");
Session.setAuth("loginRedirected", true);
}
});
它添加的 Session.setAuth
方法在 localstorage
中创建会话变量,以便它们在页面刷新时保持不变。它甚至会在用户注销时自动清除它们。