2017-10-20 90 views
1
FlowRouter.go(redirect); //gets triggered, but site does not actually redirect until refreshed. 

我跟着這個guide構建我的路線:FlowRouter.go(重定向)被觸發,但實際上並不重定向

var authorised = FlowRouter.group(); 

var publicRoutes = FlowRouter.group(); 

FlowRouter.triggers.enter([isUserSignedIn]); 

authorised.route('/',{ 
    name:'root', 
    action(){ 
    BlazeLayout.render('App_body',{ main: 'App_home'}); 
    } 
}); 

publicRoutes.route('/welcome',{ 
    name : 'welcome', 
    action(){ 
    BlazeLayout.render('Unauthorised', { main: 'welcome' }); 
    } 
}); 


function isUserSignedIn(){ 
    if (!Meteor.user() || Meteor.loggingIn()){ 
    var route = FlowRouter.current(); 
    if (route.path != "/welcome") { 
     // Set Session to redirect path after login 
     Session.set("redirectAfterLogin", route.path); 
    } 
    console.log("user is not signed in"); 
    FlowRouter.go('welcome'); 
    } 
}; 

// Redirect After Login 
Accounts.onLogin(function(){ 
    console.log("Accounts.onLogin()"); 
    var redirect = Session.get("redirectAfterLogin"); 
    if (redirect){ 
    console.log("redirect path exists") 
    if(redirect != "/welcome"){ 
     console.log("redirect is not welcome path, redirect to ", redirect); 
     FlowRouter.go(Session.get("redirectAfterLogin")); 
    } 
    } 
    else{ 
    // if redirect doesn't exist, go "/" 
    console.log("no redirection, go root"); 
    FlowRouter.go('root'); 
    } 
}) 

// Not Found 
FlowRouter.notFound = { 
    action() { 
    BlazeLayout.render('Unauthorised', { main: 'App_notFound' }); 
    }, 
}; 

上面的代碼執行以下操作:

案例1:強制Session.set(「redirectAfterLogin」,「/ blah」);

  1. 註銷的應用程序。
  2. 輸入控制檯Session.set("redirectAfterLogin", "/blah");
  3. 登錄
  4. 觀察控制檯輸出如下:
    • Accounts.onLogin()
    • redirect path exists
    • redirect is not welcome path, redirect to /blah

但我仍處於「未經授權」佈局,並帶有「歡迎」模板。

  1. 按刷新按鈕,我將重定向到「未找到」 - 這是正確的結果。

情況2:Session.get( 「redirectAfterLogin」)是未定義的應用程序的

  1. 註銷。
  2. 輸入控制檯Session.set("redirectAfterLogin");
  3. 登錄
  4. 觀察控制檯輸出如下:
    • Accounts.onLogin()
    • no redirection, go root

但我依然對 「未授權」 佈局,用「歡迎」模板「

  1. 按刷新按鈕,我將重定向到「/」 - 這是正確的結果。

究竟是什麼阻礙了這裏的邏輯?請幫忙!

回答

0

我想註銷後重定向在一個事件時,這個問題:

'click .js-logout'() { 
    Meteor.logout(); 

    FlowRouter.go('signin'); 
}, 

我快速的解決方案是增加超時呼叫路由器:

'click .js-logout'() { 
    Meteor.logout(); 

    // we have to do redirect a bit later because logout is interfering the redirection 
    setTimeout( 
    () => { 
     FlowRouter.go('signin'); 
     }, 100 
    ); 
}, 

您可能希望增加超時。

+0

看看我上面的解決方案,它可能也會幫助你! – user2587676

0

這是我的問題的原因,角色沒有正確訂閱,因此我被卡在unauthorised佈局。我希望這有幫助!

FlowRouter.wait() 
// Tracker.autorun -> 
// # if the roles subscription is ready, start routing 
// # there are specific cases that this reruns, so we also check 
// # that FlowRouter hasn't initalized already 
// if Roles.subscription.ready() and !FlowRouter._initialized 
//  FlowRouter.initialize() 
Tracker.autorun(function(){ 
    if (Roles.subscription.ready() && !FlowRouter._initialized){ 
    FlowRouter.initialize(); 
    } 
});