1

I have asked this question already but still I didn't get an answer.

How to login using Github, Facebook, Gmail and Twitter in Laravel 5.1?

auth not working


I am able to store data from gmail, github in the database.

Controller

if (Auth::attempt(['email' => $email, 'password' => $user_id]))
{
    return redirect()->intended('user/UserDashboard');
} 
else
{
    //here i am going to insert if user not logged in already
    return redirect()->intended('user/UserDashboard');
}

My problem is if I echo any data instead of return redirect()->intended('user/UserDashboard'); then it displays. If I add redirect then it doesn't work.

Community
  • 1
  • 1
scott
  • 3,112
  • 19
  • 52
  • 90
  • The code you posted is for the standard Laravel authentication, not [Socialite](http://laravel.com/docs/5.1/authentication#social-authentication)... – Jeemusu Sep 03 '15 at 06:52
  • If you remove the `&& Auth::user()->role === 'user'` does it work? Alternatively try dd(Auth::user()) on another route, to make sure the session isn't being lost. What session driver are you using? – Jeemusu Sep 03 '15 at 07:46

1 Answers1

2

I'm not entirely sure how it works when using google for oAuth, but I assume your desired flow is:

  1. User returns from google.
  2. Check if user already exists, otherwise create new user record.
  3. Log user in.
  4. Redirect to dashboard.

Assuming the above your methods would look something like this.

Controller

public function google()
{
    // Redirect user to google for authentication
    return Socialite::driver('google')->redirect();

    // In Laravel 5.0 this would be
    // return Socialize::with('google')->redirect();
}

public function googleCallback()
{
    // Return from google with user object
    $googleUser = Socialite::driver('google')->user();
    
    // In Laravel 5.0 the above would be
    // $user = Socialize::with('google')->user();

    // If user exists, retrieve the first() record, 
    // otherwise create a new record
    $user = User::firstOrCreate([
        'user_id'  => $googleUser->id,
        'name'     => $googleUser->name,
        'password' => $googleUser->id,
        'email'    => $googleUser->email,
        'avatar'   => $googleUser->avatar
    ]);
    
    // Login the user
    Auth::login($user, true);
    
    // Redirect to the dashboard
    return Redirect::intended('user/UserDashboard');
}
Community
  • 1
  • 1
Jeemusu
  • 10,415
  • 3
  • 42
  • 64
  • @jeemusu.nnow i am getting error.Undefined property: App\Http\Controllers\UserController::$socialite – scott Sep 03 '15 at 07:18
  • @tester Sorry, I had used dependency injection but not included the class `__construct()`, I updated my answer to use the Facades. – Jeemusu Sep 03 '15 at 07:21
  • @Jeemusu.i treid that but it redirect to login only.in url of the browser it generate # end of the url http://localhost/demo/public/login# – scott Sep 03 '15 at 07:23
  • Auth::login($user, true); method i tried 3 days back .but didnt work .same error got – scott Sep 03 '15 at 07:24
  • I wonder if the problem is somewhere else then... Have you set up the routes and middleware correctly for your login and userDashboard pages? Try to `dd(Auth::user())` at the end of `googleCallback()` before the redirect and see if you get a result, should tell you if the user is logged in or not? Check your logs and see if there are any error messages related to something else? – Jeemusu Sep 03 '15 at 07:26
  • if i comment redirect and print dd(Auth::user()) then it will print – scott Sep 03 '15 at 07:28
  • print what? the user details? If thats a case then Socialite is working fine, your user details are in the database, and the user is logged in. The problem is most likely with your middleware, or how it is defined on your routes. – Jeemusu Sep 03 '15 at 07:31
  • yes.now you are right.i am also thinking same.i will update my post – scott Sep 03 '15 at 07:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88662/discussion-between-tester-and-jeemusu). – scott Sep 03 '15 at 07:41