samedi 1 septembre 2018

how to use socialite facebook login with ajax call in laravel 5

I am trying to use Socialite facebook login with ajax call in Laravel 5, someone have any idea?

I am using this controller

class FacebookController extends Controller
{

    public function redirectToFacebook()
    {
         return Socialite::driver('facebook')->fields([
             'name', 'email', 'first_name'
        ])->scopes([
            'email'
        ])->asPopup()->redirect();

    } 
    public function handleFacebookCallback()
    {
        try {

            $user = Socialite::driver('facebook')->user();
            $create['name'] = $user->getName();
            $create['email'] = $user->getEmail();
            $create['username'] = $user->getFirstName();
            $create['facebook_id'] = $user->getId();

            $userModel = new User;

            $createdUser = $userModel->addNew($create);
            Auth::loginUsingId($createdUser->id);

            return redirect()->route('/');

        } catch (Exception $e) {

            return redirect('/');

        }
    }

Routes

Route::get('facebook', function () { return view('facebook');});
Route::get('auth/facebook', 'Auth\FacebookController@redirectToFacebook');
Route::get('auth/facebook/callback','Auth\FacebookController@handleFacebookCallback');

This code is working fine but facebook login works only with redirect page, I need to login without update the page, any idea I will appreciate.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire