Laravel 5 Social authentification (Facebook)

2019-08-09 07:38发布

问题:

I have a few questions that I can't answer with the Laravel official doc.. I am trying to use this practical Facebook (or other SNS) login that we see everywhere.

I had looked for a good tutorial about it but I couldn't find better than that one.

I followed it and I think I pretty much understand the process involved in here.

  • Our website goes to FB
  • We authenticate ourselves on FB
  • We then get redirected back to our app. With a 'token' saved somewhere and the user data.

But then what ? How do we access the protected part of our website ? I mean how do we pass the, out of the box middleware, that we already have ?

Because I have my FB login button on the standard login page (www.mywebsite/auth/login) but then what I don't quite understand is what callback in my service.php should I have ?

Currently

'facebook' => [
    'client_id' => '12321452415',
    'client_secret' => '675f2d7f77232b0fb20762261db6dcd072',
    'redirect' => 'http://mywebsite/',
],

But off course if I call my main page I just got redirected to my login page again because of the middleware.. right ?? I mean did I do something wrong or I just didn't do enough ?

Hope I am enough clear..

Any help very much appreciated

回答1:

The redirect you have defined under services.php should be a route of your website lets say http://website.com/callback ; once you have authenticated your request on facebook you will be redirected back to this route. If you are working with the basic facebook app without any extra permissions then you will get Facebook name of the user, fb profile id, photo and few extra details as mentioned in the tutorial you have referred. You can utilize these details to authenticate user on your website e.g you have a table users with one extra column i.e fb_id once you get response back from facebook you check your db if the fb_id you got from response exist in the database ? if yes then use

Auth::loginUsingId($fetched->id);
{
    return redirect()->to('some-route');
}

if you do not get any record matching this fb_id you insert a new record in the database and then loginUsingId

I hope I am clear enough with this.



回答2:

Ok, I am sure I can do much better than that but I have something working (after scratching my head a couple of time)

My routes:

Route::get('connect/{provider}', 'AccountController@redirectToProvider');
Route::get('account/{provider}', 'AccountController@handleProviderCallback');

And my controller:

class AccountController extends Controller {

public function redirectToProvider($provider) {
  return Socialize::with($provider)->redirect();
}

public function handleProviderCallback($provider) {
    $user   = Socialize::with($provider)->user();

    // Define the SNS variable
    $id     = $user->getId();
    $name   = $user->getName();
    $email  = $user->getEmail();
    $avatar = $user->getAvatar();

    // check if the user exists in the DB already
    $users = DB::table('users')->where($provider.'_id', $id)->first();

    // if the user doesn't exist insert a new record and get the user_id
    if(empty($users)){
        DB::table('users')->insert(
            ['name' => $name,'email' => $email,'profile_picture' => $avatar,$provider.'_id' => $id]
        );
        $users = DB::table('users')->where($provider.'_id', $id)->first();
    }

    // use the auth facade to login with the user ID
    Auth::loginUsingId($users->id);
    {
        // redirect to the home page
        return redirect()->to('/');
    }
}
}

And my login page:

<a href="{!!URL::to('connect/facebook')!!}">Login with Facebook</a><br>
<a href="{!!URL::to('connect/twitter')!!}">Login with Twitter</a><br>
<a href="{!!URL::to('connect/google')!!}">Login with Google</a>

Any improvements is welcomed. I know it's not perfect but it might help someone :)