简体   繁体   中英

How to authenticate Facebook users using jwt-auth?

I am using Laravel 5 and jwt-auth package to handle JWT. How do I authenticate if the user came from Facebook?

My idea is, I'm going to authenticate using FB ID and email. I did this by changing the password to fb_id, unfortunately it didn't work. Any recommendation and why do i get the error below? Thanks!

Authentication code:

    //facebook
    if(Input::has('fb_id')){
        $credentials = Input::only('email','fb_id');
    }else{
        $credentials = Input::only('email', 'password');
    }

    try {
        // attempt to verify the credentials and create a token for the user
        if (! $token = JWTAuth::attempt($credentials)) {
            return Response::json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return Response::json(['error' => 'could_not_create_token'], 500);
    }

Error:

ErrorException in EloquentUserProvider.php line 108:
Undefined index: password

Update: Managed to make it work by using JWTAuth::fromUser($user).

//find the user using his details.
$user = User::where('email','=',Input::get('email'))->where('fb_token','=',Input::get('fb_id'))->first();

//then use
$token = JWTAuth::fromUser($user);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM