2014-03-02 17 views
1

我有一個表單,當你點擊提交時,即使你沒有在表單中輸入任何內容,它也會進入路徑/登錄。不過,我希望它能檢查是否有用戶輸入了電子郵件,並且在登錄前密碼是否正確。如果電子郵件不知道或密碼不正確,那麼我希望它顯示一個錯誤(並保持登錄頁面)。我一直試圖讓這個工作,但我不知道該怎麼做。有誰知道如何做到這一點?這是我到目前爲止的代碼:只有在電子郵件和密碼正確的情況下才會對用戶進行簽名?

HTML:

<div id="topbar"> 
    <h1><strong>chattly</strong></h1> 
     {{#unless currentUser}} 
      {{> signIn}} 
      {{> alert}} 
     {{else}} 
      {{> signOut}} 
     {{/unless}} 
    </div> 

的javascript:

// Sign In Template 
Template.signIn.events({ 
    'submit #signInForm': function(e, t) { 
     e.preventDefault(); 

     var signInForm = $(e.currentTarget), 
      email = trimInput(signInForm.find('.email').val().toLowerCase()), 
      password = signInForm.find('.password').val(); 

     if (isNotEmpty(email) && isEmail(email) && isNotEmpty(password) && isValidPassword(password)) { 
      Meteor.loginWithPassword(email, password, function(err) { 
       if (err) { 
        Session.set('alert', 'We\'re sorry but these credentials are not valid.'); 
       } else { 
        Sesson.set('alert', 'Welcome back New Meteorite!'); 
       } 
      }); 
     } 
     return false; 
    }, 
}); 
+1

你的問題:「我怎麼能檢查的電子郵件/通有效之前我檢查是電子郵件/通有效」 –

+0

哎呀,我的問題應該是「如何在進入網站的主頁之前檢查電子郵件/密碼是否有效?」 – 5AMWE5T

回答

1

你,所以我假設你使用的是鐵路由器的封裝與鐵路由器標記這一點。

通過在受限制頁面的所有路由上掛鉤之前,可以防止未經身份驗證的用戶訪問登錄頁面之外的任何頁面。如果沒有用戶登錄並且可以重新路由到登錄頁面,則前鉤子將檢查Meteor.user()是否不返回對象。

查看鐵路路由器文檔,這裏是掛鉤前後的部分。它甚至顯示使用before hook作爲過濾器來防止未經身份驗證的用戶轉到路由。

https://github.com/EventedMind/iron-router/#before-and-after-hooks

它看起來是這樣的:

Router.map(function() { 
    this.route('postShow', { 
    path: '/posts/:_id', 

    before: function() { 
     if (!Meteor.user()) { 
     // render the login template but keep the url in the browser the same 
     this.render('login'); 

     // stop the rest of the before hooks and the action function 
     this.stop(); 
     } 
    }, 

    action: function() { 
     // render the main template 
     this.render(); 

     // combine render calls 
     this.render({ 
     'myCustomFooter': { to: 'footer' }, 
     'myCustomAside': { to: 'aside' } 
     }); 
    }, 

    after: function() { 
     // this is run after our action function 
    } 
    }); 
}); 
+1

謝謝,我認爲這是我需要的。我將不得不玩弄它,以獲得它的方式,但我認爲這將對我有用。 – 5AMWE5T

相關問題