5

I'm trying to setup 'Login with facebook' using laravel socialite. When I try to login, it gets a successful callback from the facebook, I'm storing the data fetched into the database and try to redirect to home page. While doing so, I am redirected back to the login page and never reaching the homepage.

While debugging the error I found that my Auth::login($user) is not working properly.

Here is the code-

AuthController.php

use App\Http\Controllers\Controller;
use Laravel\Socialite\Two\InvalidStateException;
use Auth;
use Socialite;
use App\User;

public function redirectToProvider($provider)
{
    return Socialite::driver($provider)->redirect();
}

public function handleProviderCallback($provider)
{
    $user = Socialite::driver($provider)->user();
    // dd($user);
    $authUser = $this->findOrCreateUser($user, $provider);
    // dd($authUser);
    if(Auth::login($authUser, true)){            // here is the error
      return redirect($this->redirectTo);
    }
    else{
      return 'Login not done';                 //this prints out to the screen
    }
}

public function findOrCreateUser($user, $provider)
 {
     $authUser = User::where('id', $user->id)->first();
     if ($authUser) {
         return $authUser;
     }
     return User::create([
         'name'     => $user->name,
         'email'    => $user->email,
         'avatar'    => $user->avatar,
         'password'    => bcrypt('password'),
         'provider' => $provider,
         'id' => $user->id
     ]);
 }

Do let me know what am I doing wrong.

This is what I'm getting while dd($authUser)

enter image description here

23
  • Does Auth::login return something? (From the first peek at the auth docs I could not see that there is a return value) Commented Dec 31, 2018 at 9:58
  • it returns null @Roland Starke Commented Dec 31, 2018 at 10:01
  • So an if else around it is useless. (Does not tell you if the login worked or not). You could just remove the if else and always redirect somewhere. Commented Dec 31, 2018 at 10:02
  • but when I do dd($authUser), it does return an array Commented Dec 31, 2018 at 10:05
  • redirecting directly won't serve the purpose of authenticating the user. Commented Dec 31, 2018 at 10:06

4 Answers 4

1
+50

Since you are grabbing user details and saving them to your database you in order to login you need to use attempt and passing the details that you get from $authUser variable, but you need a way to get the password since the attempt convert the password string to the hash in order to login the user

$authUser = $this->findOrCreateUser($user, $provider);
  $credentials = $request->only($authUser->email, 'password');

        if (Auth::attempt($credentials)) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
0

In my end, I just resolved it by making the domain null in my session.php. I dont know if this is the correct answer but I can now redirect to my homepage after logging in with google. Maybe if you deploy this to production you will change the domain thing to the actual url.

/*
    |--------------------------------------------------------------------------
    | Session Cookie Domain
    |--------------------------------------------------------------------------
    |
    | Here you may change the domain of the cookie used to identify a session
    | in your application. This will determine which domains the cookie is
    | available to in your application. A sensible default has been set.
    |
    */

    'domain' => null,

    /*
    |--------------------------------------------------------------------------
    | HTTPS Only Cookies
    |--------------------------------------------------------------------------
    |
    | By setting this option to true, session cookies will only be sent back
    | to the server if the browser has a HTTPS connection. This will keep
    | the cookie from being sent to you if it can not be done securely.
    |
    */

    'secure' => false,
0

Auth::login() return void. use

Auth::login($authUser, true);

if(Auth::check()){
  return redirect($this->redirectTo);
}
else{
  return 'Login not done';
}
-2

you should use auth()->login($user); function after create user to add user to auth

3
  • 3
    Incorrect. Both Auth::login($user) as auth()->login($user); are correct.
    – John
    Commented Oct 16, 2019 at 23:02
  • if(\Auth::check()){} try this Commented Jun 24, 2020 at 11:24
  • Please add some explanation to your answer such that others can learn from it - is there any good reason to use your code, and explicitly not the other?
    – Nico Haase
    Commented Jun 26, 2020 at 11:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.