Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 810
Wreeecks Avatar asked Dec 03 '25 23:12

Wreeecks


1 Answers

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);
like image 181
Wreeecks Avatar answered Dec 06 '25 12:12

Wreeecks