gpt4 book ai didi

Meteor:如果已经登录,则重定向用户

转载 作者:行者123 更新时间:2023-12-04 19:11:45 26 4
gpt4 key购买 nike

我正在使用 meteor-router ,我想将用户重定向到 /user如果他要求 /他已经登录了。
正如预期的那样,这只会呈现 user_index模板而不是更改 URL:

Meteor.Router.add
'/': -> if Meteor.userId() then 'user_index' else 'index'
我想做这样的事情:
Meteor.Router.add
'/': -> if Meteor.userId() then Meteor.Router.to '/user' else 'index'

2014 年 6 月 4 日更新:
这个问题不再相关, iron-router应该改用。

最佳答案

meteor 路由器现在是 deprecated .而是使用 iron-router可以使用以下方法根据登录状态重定向:

Router.configure({layoutTemplate: 'mainLayout'});

Router.map(function() {
this.route('splash', {path: '/'});
this.route('home');
});

var mustBeSignedIn = function(pause) {
if (!(Meteor.user() || Meteor.loggingIn())) {
Router.go('splash');
pause();
}
};

var goToDashboard = function(pause) {
if (Meteor.user()) {
Router.go('home');
pause();
}
};

Router.onBeforeAction(mustBeSignedIn, {except: ['splash']});
Router.onBeforeAction(goToDashboard, {only: ['splash']});

示例取自: Meteor.js - Check logged in status before render

- 或者 -

使用 accounts-entry包裹。来自 their site :

Ensuring signed in users for routes

Use AccountsEntry.signInRequired(this) to require signed in users for a route. Stick that in your before hook function and it will redirect to sign in and stop any rendering. Accounts Entry also tracks where the user was trying to go and will route them back after sign in.

关于Meteor:如果已经登录,则重定向用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14325540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com