0

Here its my controller for logging in. the register controller works well, it creates an user to database but when i try to log in with it fails it results always false pls help.

namespace App\Http\Controllers; use Auth; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use App\User; class loginController extends Controller { public function login(Request $request) { $_email = $request->input('emaillogin'); $_password = $request->input('passwordlogin'); if (Auth::attempt(['email' => $_email, 'password' => $_password])) { Auth::login(Auth::user()); return redirect()->intended('/dashboard'); } else { return redirect()->intended('/'); } } } 
7
  • why not use the built in Auth stuff? Commented Feb 14, 2016 at 19:10
  • how im supposed to do that Commented Feb 14, 2016 at 19:22
  • 1
    Read a bit on documentation in laravel 5 there is a built in auth function with registration and password, even with front end example. In laravel 5.1 the front end is gone but built in auth is still there. And also one thing the Auth::attempt checks for password with bcrypt or hash method, so check if you used hash on your password field upon registration. Also check the user data in your database to see what's saved. Commented Feb 14, 2016 at 19:29
  • I didnt use hash, but thank you anyways :) Commented Feb 14, 2016 at 19:32
  • 1
    your form input names must match your $request ids so $_email = $request->input('email'); since name of your email field in form is email also you must use bcrypt on password field upon registration, check my answer for more details . Commented Feb 14, 2016 at 20:32

2 Answers 2

2

Auth::attempt logs user into your application if passed, no need for second authentication

 if (Auth::attempt(['email' => $_email, 'password' => $_password])) { return redirect()->intended('/dashboard'); } else { return redirect()->guest('/login'); } 
Sign up to request clarification or add additional context in comments.

3 Comments

return was missing in if block.
yes i saw that but still. i saw many people having problem with this. Is this a laravel error ?
Let's troubleshoot .... Replace return redirect()->intended('/dashboard'); with dd('hey I am in'); and try again tell me the result
1

As i said in the comment and i just tested it on my local pc, maybe it's wrong to you but for me it doesn't work if i use plain password save in database, here's my register file

 protected function create(array $data) { $activation_code = str_random(60); $user = User::create([ 'username' => $data['username'], 'email' => $data['email'], 'password' => bcrypt($data['password']), 'activation_code' => $activation_code ]); if ($user->save()) { $data = array( 'name' => $user->username, 'code' => $activation_code, ); \Mail::queue('emails.activate', $data, function($message) use ($user) { $message->to($user->email)->subject('Thank you for registering. Please activate your account!'); }); } return $user; } 

you see i have crypted password

'password' => bcrypt($data['password']), 

and if i test it just with:

'password' =>$data['password'], 

The registration works but authentication fails because password must be encripted you should also remove Auth::login(Auth::user()); that is not necessary

 if (Auth::attempt(['email' => $_email, 'password' => $_password])) { return redirect()->intended('/dashboard'); } 

This happens on my site when i don't use bcrypt on password in registration controller and try to login later

Whoops! There were some problems with your input. These credentials do not match our records. 

Also what i have seen from our login form inputs are not emaillogin and passwordlogin they are just email and password

1 Comment

I bcrypt the password in the registerController and still i get the authentication fail. Yes and the login controller are the same request from input. Would you mind to get my email and try to fix this with teamviewer im begginer in L5.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.