2014-01-23 115 views
21

我使用内置的LoginButtons选项和Meteor,我想在用户登录后重定向。使用内置的web片段意味着我不能使用Meteor的回调.loginwithPassword和我看不到Iron-Router内部的任何钩子来做重定向。使用Meteor和铁路路由器登录后重定向

有什么建议吗?

+0

你尝试这个https://github.com/EventedMind/iron-router#using-hooks Router.before(mustBeSignedIn,{except:['login','signup','forgotPassword']}); – pahan

+0

我看不到任何使用此示例的代码段,看看如何使用它 – user2243825

回答

22

流星往往呈现如此迅速,用户已经定义之前的页面被加载。你需要使用Meteor.loggingIn()来占你在登录过程是情况此代码的工作对我来说:

this.route('myAccount', { 
    path: '/', 
    onBeforeAction: function() { 
    if (! Meteor.user()) { 
     if (!Meteor.loggingIn()) Router.go('login'); 
    } 
    } 
} 
+12

'if(!Meteor.loggingIn())Router.go('login');' –

1

您可以简单地使用你已经在爱尔兰路线

Router.go(“/ myRouterPathToTemplate”)

+0

爱它!谢谢。 – the0ther

+0

您可以添加一个如何实现的示例 – Chris

5

应该很简单,你只添加类似配置现有的途径之一:

Tracker.autorun(function() { 
    var currentRoute = Router.current(); 
    if (currentRoute === null) { 
    return; 
    } 

    if (currentRoute.route.getName() === 'login' && Meteor.user() !== null) 
    Router.go('WelcomeNewUser'); 
    } 

如果用户未登录,您也可以使用与其他模板相同的路线。

就像这样:

this.route('myAccount', { 
    before: function() { 
    if (!Meteor.user()) { 
     this.render('login'); 
     this.stop(); 
    } 
    } 
} 

没有魔法,只是进去看了docs;)

+0

请注意,“Deps”现在称为“Tracker”。 –

+0

此代码将驻留在客户端? – mfq

+0

@mfq是的,因为它仅用于渲染模板。 –

6

这个例子可能是有用的

// main route render a template 
Router.route('/', function() { 
    this.render('main'); 
}); 

// render login template 
Router.route('/login', function() { 
    this.render('login'); 
}); 


// we want to be sure that the user is logging in 
// for all routes but login 
Router.onBeforeAction(function() { 
    if (!Meteor.user() && !Meteor.loggingIn()) { 
     this.redirect('/login'); 
    } else { 
     // required by Iron to process the route handler 
     this.next(); 
    } 
}, { 
    except: ['login'] 
}); 

// add here other routes 

// catchall route 
Router.route('/(.*)', function() { 
    this.redirect('/catchallpage'); 
}); 
相关问题