Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to login without password using Auth::attempt in Laravel 5.0?

I am using Laravel 5.0. I want do a login only with username (no password) because I am using Single Sign On to access to my Laravel project, so if user success login in Single Sign On, it will check to my database if the username of the user who login has registered or not.
I have looking for this all day but I still have found the clue.

I have got an error:

Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given

if I am using the code below in my LoginController:

$user = User::where('USER_NAME', '=', $_GET['UserName'])->first();
if(Auth::login($user)->USER_ID == $_GET['UserName']){
   return redirect('Home');
}

And if I am using the code below in my LoginController:

if(Auth::attempt(['USER_ID' => $_GET['UserName']])){
   return redirect('Home');
}

I have got an error:

Undefined index: password

like image 493
hendraspt Avatar asked Aug 23 '16 13:08

hendraspt


2 Answers

you can try like this

public function login(Request $request) {    
  $email = $request->email;    
  $user = User::where('email', $email)->first();    
  if ($user) {    
    Auth::login($user);    
    return redirect()->route('dashboard');    
  }
  else {    
    return redirect()->back();    
  }    
}

hope it works.

like image 139
Thamnees Avatar answered Nov 15 '22 17:11

Thamnees


According to the documentation:

the user will be retrieved by the value of the email column. 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.

So, password will must be checked in this method and hence it's a required parameter.

You can login like following:

$user = User::where('USER_NAME', '=', $_GET['UserName'])->first();

if (empty($user)) {
    abort(404);
}

if(Auth::loginUsingId($user->id)){
    return redirect('Home');
}
like image 38
Sovon Avatar answered Nov 15 '22 17:11

Sovon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!