mercredi 9 mars 2016

Testing a Facebook login flow with Laravel 5.1

I am implementing a Facebook login functionality into my website. I am using Laravel 5.1 and Socialite. I came across a few questions. In the best case scenario everything works like a charm, however, right now I am not handling what happens if the users click on the Facebook login button but then when asked for giving permissions to my app he/ she clicks cancel. I am not handling what happens if someone removes my app from Facebook via app settings and revisits my app also. My app should detect this and prompt the person to log back in and what happens if someone disabled Facebook platform via app settings and logs in to my app. And last but not least what happens if my app token has expired and someone tries to log in?

Route:

Route::get('/signup/facebook/',[
    'uses' => 'FacebookController@getSocialAuth',
    'as'   => 'auth.getSocialAuth'
]);


Route::get('/signup/facebook/callback/',[
    'uses' => 'FacebookController@getSocialAuthCallback',
    'as'   => 'auth.getSocialAuthCallback'
]);

FacebookCotroller:

<?php namespace Podobri\Http\Controllers;

use Podobri\Models\User;
use Auth;
use Socialite;

class FacebookController extends Controller {



    public function getSocialAuth(){
        return Socialite::driver('facebook')->redirect();
    }

    public function getSocialAuthCallback()
    {
        try {
            $user = Socialite::driver('facebook')->user();
        } catch (Exception $e) {
            return redirect('signup/facebook');
        }

        $authUser = $this->findOrCreateUser($user);

        Auth::login($authUser, true);

        return redirect()->route('problems.index');
    }


    private function findOrCreateUser($facebookUser) {
        $authUser = User::where('facebook_id', $facebookUser->id)->where('email', '!=', $facebookUser->user['email'])->first();

        if ($authUser) {
            return $authUser;
        }

        return User::create([
                    'first_name' => $facebookUser->user['first_name'],
                    'last_name' => $facebookUser->user['last_name'],
                    'email' => $facebookUser->user['email'],
                    'facebook_id' => $facebookUser->id,
        ]);
    }

}

Facebook login button code:

<a class="fb-log" href="{{ route('auth.getSocialAuth') }}">Facebook</a>

I am so looking forward to guidelines.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire