0

How to force the user to fill the form after login with google or facebook. I ma using laravel 5.4 with socials. In my app i want to allow google and facebook login but user can create a standard user too. When login with one of the social providers (facebook or google) i want the user to fill other fields in the form to complete the registration.

The handleProviderCallback:

public function handleProviderCallback($provider)
{
    try{
        $socialUser = Socialite::driver($provider)->stateless()->user();
    }catch (Exception $e){
        return redirect('/');
    }

    // check if we have logged PROVIDER
    $socialProvider = Social::where('provider_id', $socialUser->getId())->first();

    // if we don`t have a provider create a user and provider
    if(!$socialProvider){

        // i want the user to go back in the form and finish the registration 
        return view('auth.socials')->with('socialUser', $socialUser)->with('provider', $provider);

    }
    else{
        // return the user
        $user = $socialProvider->user;
    }

    // log the user in our app :)
    auth()->login($user);
    return redirect('/home');

the problem is when submiting the form i get an exception:

Client error: `POST https://accounts.google.com/o/oauth2/token` resulted in 
   a `400 Bad Request` response:
{
  "error" : "invalid_grant",
  "error_description" : "Invalid code."
}

If i use the method handleProviderCallback like this:

 public function handleProviderCallback($provider)
{
    try{
        $socialUser = Socialite::driver($provider)->stateless()->user();
    }catch (Exception $e){
        return redirect('/');
    }

    // check if we have logged PROVIDER
    $socialProvider = Social::where('provider_id', $socialUser->getId())->first();

    // if we don`t have a provider create a user and provider
    if(!$socialProvider){
        $user = User::firstOrCreate(
            ['email' => $socialUser->getEmail()],
            // i have to manualy add the missing fields
            ['name' => $socialUser->getName(), 'forename' => 'test', 'password' => '' ,'address' => 'adasda', 'country' => 'asasfasf', 'town' => 'asfasfsfsfs',  'legal_person' => '0', 'newsletter' => '1', 'phone' => '1235241521']

        );          

        // add provider for this user
        $user->socials()->create(
            ['provider_id' => $socialUser->getId(), 'provider' => $provider]
        );
    }
    else{
        // return the user
        $user = $socialProvider->user;
    }

    // log the user in our app :)
    auth()->login($user);
    return redirect('/home');
}

the user get logged in, but as you can see i have to manually insert the missing fields in the firstOrCreate method (which are required in the users table)

Any suggestion how to do this?

calin24
  • 905
  • 3
  • 21
  • 43
  • You need to fix the `invalid code` error first. – Martin Bean Jun 27 '17 at 09:27
  • is there a better way to do this? The ideea is that I obtain the info what i need (email, provider_id, etc). It redirect me to that view...i have the client email and what i need....but when submit - to register the user (save in the DB)...with all the data....it fails whith that error. – calin24 Jun 27 '17 at 09:33
  • 1
    I’d store the ID from Google/Facebook in the session, then redirect to a registration form with the inputs pre-filled with the data you have. Then when you submit, check the session for a Google/Facebook ID and associate it with the user you’re creating. – Martin Bean Jun 27 '17 at 09:35
  • 1
    worked with Session::put and Session::get :) thanks @MartinBean – calin24 Jun 27 '17 at 16:42
  • I have another problem now.....the session don't work online. I put my project on a server but session doesn't work. Local is working fine. Have any ideea why? The files with sessions are created in /storage/framework/sessions – calin24 Jun 28 '17 at 08:48
  • Check the error log on your server. – Martin Bean Jun 28 '17 at 09:26
  • on the server there are no errors from today (storage/logs) – calin24 Jun 28 '17 at 10:02
  • It depends what you mean by “it doesn’t work”. This sounds like another, different issue so you’ll need to create a new Stack Overflow question for it. – Martin Bean Jun 28 '17 at 10:09

0 Answers0