3

I try to authenticate user but nothing happens in controller

I do the next

$authUser = User::where('password', md5($user->salt . $request->get('password')))
                  ->where('email', $request->get('email'))
                  ->first(['login', 'email', 'firstname', 'lastname', 'zip_code','phone']);
if ($authUser) {
   Auth::login($authUser);
   return response()->json(['user' => $authUser]);
}

response returns me user but for next request I get error that user not authenticated. Any idea?

3
  • Please post more detailed questions Commented Jul 14, 2015 at 9:23
  • But why you are not directly using auth::attempt ? Commented Jul 14, 2015 at 17:52
  • Because in auth::attempt If the user is found, the hashed password stored in the database will be compared with the hashed password value passed to the method via the array. In database used md5 hashing with custom random generation salt and Laravel used another function for hashing Commented Jul 20, 2015 at 6:35

1 Answer 1

3

Hello it's happens because you forgot to add id field to array of returned fields, so try add it like

$authUser = User::where('password', md5($user->salt . $request->get('password')))
                  ->where('email', $request->get('email'))
                  ->first(['id', 'login', 'email', 'firstname', 'lastname', 'zip_code','phone']);
if ($authUser) {
   Auth::login($authUser);
   return response()->json(['user' => $authUser]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.