Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined index: avatar_url... with facebook socialite #30

Closed
m2ura opened this issue Mar 8, 2017 · 2 comments
Closed

Undefined index: avatar_url... with facebook socialite #30

m2ura opened this issue Mar 8, 2017 · 2 comments

Comments

@m2ura
Copy link

m2ura commented Mar 8, 2017

I modified the code on AuthController so can add more socialite providers buy changing public function redirectToProvider()
{
return Socialite::driver($github)->redirect();
}
to
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}

here is a full code

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

/**
 * Obtain the user information from GitHub.
 *
 * @return Response
 */
public function handleProviderCallback($provider)
{
    $User = Socialite::driver($provider)->user();
    $user = $this->user->getByProviderId($User->id);

    if (auth()->check()) {
        $currentUser = auth()->user();

        if ($currentUser->provider_id) {
            return redirect()->back();
        } else {
            if ($user) {
                return redirect()->back();
            } else {
                $this->bindProvider($currentUser, $User);

                return redirect()->back();
            }
        }
    } else {
        if ($user) {
            auth()->loginUsingId($user->id);
            return redirect()->to('article');
        } else {
            $this->registerUser($User);
            return redirect()->to('auth/register');
        }
    }
}

/**
 * Bind the github account.
 *
 * @param $currentUser
 * @param $registerData
 * @return mixed
 */
public function bindProvider($currentUser, $registerData)
{
    $currentUser->provider_id = $registerData->user['id'];
    $currentUser->name = $registerData->nickname;
    $currentUser->url = $registerData->user['url'];

    return $currentUser->save();
}

/**
 * Save the register data in session.
 *
 * @param $registerData
 */
public function registerUser($registerData)
{
    $data['avatar'] = $registerData->user['avatar_url'];
    $data['provider_id'] = $registerData->user['id'];
    $data['url'] = $registerData->user['url'];
    $data['name'] = $registerData->nickname;
    $data['nickname'] = $registerData->user['name'];
    $data['email'] = $registerData->user['email'];

    session()->put('oauthData', $data);
}

/**
 * Display the github oauth for register page.
 *
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
 */
public function create()
{
    if (! session()->has('oauthData')) {
        return redirect()->to('login');
    }

    $oauthData = array_merge(session('oauthData'), request()->old());

    return view('auth.register', compact('oauthData'));
}

/**
 * Store a new user.
 *
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
 */
public function store()
{
    if (! session()->has('oauthData')) {
        return redirect('login');
    }

    $this->validator(request()->all())->validate();

    $oauthData = session('oauthData');

    $data = array_merge($oauthData, request()->all());

    $data['password'] = bcrypt($data['password']);

    $data['status'] = true;

    auth()->guard()->login(User::create($data));

    session()->forget('oauthData');

    return redirect()->to('article');
}

But I get this

socialite

@m2ura
Copy link
Author

m2ura commented Mar 8, 2017

this is my login form now
login

@jcc
Copy link
Owner

jcc commented Mar 8, 2017

This error is 'Undefined index: avatar_url'. You need to modify the method of registerUser(). You try to run the code of dd($registerData) to see what in it. If you want to support login for facebook, you need to modify the code.I had only supported login with github.

@jcc jcc closed this as completed Mar 9, 2017
@m2ura m2ura mentioned this issue Aug 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants