2016-04-07 15 views
1

私は、LaravelバックエンドとJWT認証を使用してIonicアプリを開発しています。Laravel/wイオンJWT認証

私の質問は...ユーザー登録時に4つのフィールドを使用しているので、ログインすると2回だけ(電子メールとパス)、登録時にトークンを2つのフィールドのみで作成する必要があります。 (不足している分野があるので)

public function signUp() 
{ 
    $credentials = Input::all(); 

    if (User::whereEmail($credentials['email'])->first()) { 
     return Response::json([ 
      'error' => 'User with given e-mail already exists', 
     ], 409); 
    } elseif (User::wherePhone($credentials['phone'])->first()) { 
     return Response::json([ 
      'error' => 'User with given phone number already exists', 
     ], 409); 
    } else { 
     $user = User::create($credentials); 
     $token = JWTAuth::fromUser($user); 

     return Response::json(compact('token')); 
    } 
} 

は、しかし、私は$credentials = Input::only('email', 'password')を変更した場合、完全なユーザーが作成されません。

これは、作業機能をサインアップしています。

しかし、私はそのままです$credentialsを残し、そして $token = JWTAuth::fromUser(Input::only('email', 'password'))またはJSONに電子メールとパスワードを解析、または類似のもののような組み合わせを作る場合でも...私は「非オブジェクトのプロパティを取得しようとすると、」GETエラー、またはその配列がオブジェクトの代わりにJWTAuthに渡されます。

答えて

1

JWTAuth::fromUser(Input::only('email', 'password'))は、Userオブジェクトが必要です。

あなたはこのような何か行うことができます資格情報を使用する場合:

// grab credentials from the request 
    $credentials = Input::only('email', 'password'); 

    try { 
     // attempt to verify the credentials and create a token for the user 
     if (! $token = JWTAuth::attempt($credentials)) { 
      return Response::json(['error' => 'invalid_credentials'], 401); 
     } 
    } catch (JWTException $e) { 
     // something went wrong whilst attempting to encode the token 
     return Response::json(['error' => 'could_not_create_token'], 500); 
    } 

    // all good so return the token 
    return Response::json(compact('token')); 

https://github.com/tymondesigns/jwt-auth/wiki/Creating-Tokens

+0

を私はドキュメントを読んでいると、これが作業を行います...しかし、その後、私のロジックは欠陥がある:) I登録時にDBにトークンを格納するというアイデアがあり、ユーザーがログインするとトークンが一致する必要があります...しかし、これはDB内にトークンを格納しないよりスマートで信頼性の高いソリューションだと思います。ありがとう – Norgul