2017-09-13 8 views
1

私はMeteor and Reactフレームワーク内に簡単なログインフォームを構築しようとしています。ユーザー管理はアプリケーションのどこかで行われるため、MeteorsのloginWithPassword関数を使用しようとしました。Meteor - loginWithPasswordだけで成功しても何もしない

export default class Login extends React.Component{ 

constructor(props){ 
    super(props); 
} 
render() { 
    return (

     <div id="login"> 
      <form method="POST" id="loginForm" className="boxCont"> 
       <div> 
        <label>Username</label> 
        <input id="mail" type="text" name="mail" placeholder="Please enter your username here"/> 
       </div> 
       <div> 
        <label>Password</label> 
        <input id="password" type="password" name="password" placeholder="And your password here"/> 
       </div> 
       <div> 
        <button id="signIn" onClick={() => this.handleLogin()} name="signIn" className="btn left">Sign In</button> 
       </div> 
      </form><a id="forgotpass" href="#">Forgot Your Password?</a> 
     </div> 
    ); 
} 
handleLogin(){ 
    Meteor.loginWithPassword({username: '[email protected]'}, 'test', function (err) { 
     console.log(err) 
    }); 

}} 

のログイン中にエラーがアップする場合、関数は正しい動作しているようですし、期待される結果(「が見つかりませんユーザー」、「間違ったパスワード」...)で応答します。 しかし、指定されたデータが正しい場合、エラーもMeteor.userまたはuserId()も定義されていません。

私はアカウントのパスワードや流星に必要なプロパティをロードするのを忘れましたか?

User-Dbのエントリは上記のように存在します。

//編集:私は特定のルートへのアクセスを認証するために、フロー・ルータを使用してい

const exposed = FlowRouter.group({ name: 'exposed' }); 
const loggedIn = FlowRouter.group({ 
    name: 'loggedIn', 
    triggersEnter: [() => { 
      if (Meteor.loggingIn() || Meteor.userId()){ 
       FlowRouter.route = FlowRouter.current(); 
      } 
      if (FlowRouter.route.name === 'auth'){ 
       Session.set('redirectAfterLogin', FlowRouter.route.path); 
      } 
      FlowRouter.go('auth'); 
    }], 
}); 

Accounts.onLogin(() => { 
    console.log("onLogin") 
    redirect = Session.get('redirectAfterLogin'); 
    if (redirect !== '/auth'){ 
     FlowRouter.go(redirect) 
    } 
}); 
exposed.route('/auth', { 
    name: 'auth', action(params, queryParams){ 
     ReactLayout.render(Empty, { yield: <Login /> }); 
    } 
}); 
exposed.route('/', { 
    name: 'Dashboard', action(params, queryParams){ 
     ReactLayout.render(App, { yield: <Dashboard /> }); 
    } 
}); 

Meteor-docsによると、loginWithPasswordがトリガーされた後にonLoginメソッドを呼び出す必要があります。 私は、次のチュートリアルからこれらのスニペットを得た:私はあなたが何かをすることを求めていないので、何も起こらないことを推測している

https://medium.com/@satyavh/using-flow-router-for-authentication-ba7bb2644f42

+0

https://docs.meteor.com/api/accounts.html#Meteor-loginWithPassword ユーザーオブジェクトまたは文字列 ユーザー名または電子メールとして解釈される文字列。単一のキーを持つオブジェクト:電子メール、ユーザー名またはID。大文字と小文字を区別せずにユーザー名またはメールが一致します。 '' ' Meteor.loginWithPassword({email:' [email protected] '}、' test '、function(err)' '' –

+0

私は、 prop、too。同じ結果を得るパラメータの1つが正しくない/見つからない場合は、予期したエラーが発生しています。 )。 – Bl3g3

+0

ログイン成分 –

答えて

0

。あなたがいると仮定すると、

render() { 
    if (this.state.loggedIn) 
    return <Redirect to="/dashboard" /> 

あなたは再レンダリングするコンポーネントをトリガし、あなたがこのような何かを行う必要があり、あなたの方法をレンダリングでます

this.setState({loggedIn: true}) 

ようなことをすべきではありませんReact Router v4とRedirectコンポーネント(history.push()よりも)を使用してください

+0

私は実際に上記のチュートリアルのようなFlow Routerを使用しています。 Accounts-docsを正しく理解していれば、meteor.userはloginWithPassword()によって自動的に設定されるべきですか? – Bl3g3

関連する問題