skip to Main Content

In laravel docs there is a part about getting user from using ->userFromTokenAndSecret however it seems to be not working with facebook and returns an error that this method doesn’t exist.

How do I get the user by token and secret?

I’m using Laravel as API so that it works stateless and I don’t use any redirects.
I sign in using Angular app and send token then. I need to process that.

3

Answers


  1. Chosen as BEST ANSWER

    I've found an answer to my questions and it is userFromToken. What is the idea:

    1) I authorize my user on Angular 6 using ngx-social-login

    2) I receive user data from Facebook

    3) I send authToken to the API

    4) I receive user data from Facebook on my backend API using Socialite::driver('facebook')->stateless()->userFromToken($token)

    5) Do any stuff what I want. In my case I save data to DB or retrieve user if exists and then create a token for that user using JWT and return it


  2. I also got problem but not facebook. I use twitter api with this plugin.

    https://github.com/thujohn/twitter

    I use socialite after get token from twitter plugin.

    This is full that i was try to get token.

    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    use Log;
    use IlluminateSupportFacadesInput;
    use IlluminateSupportFacadesSession;
    use AppRepositoriesUserRepository;
    use Twitter;
    use Redirect;
    use Hash;
    use AppUser;
    use Socialite;
    use Auth;
    
    class SocialController extends Controller
    {
        function login() {
            // your SIGN IN WITH TWITTER  button should point to this route
            $sign_in_twitter = true;
            $force_login = false;
    
            // Make sure we make this request w/o tokens, overwrite the default values in case of login.
            Twitter::reconfig(['token' => '', 'secret' => '']);
            $token = Twitter::getRequestToken(route('twitter.callback'));
    
            if (isset($token['oauth_token_secret']))
            {
                $url = Twitter::getAuthorizeURL($token, $sign_in_twitter, $force_login);
    
                Session::put('oauth_state', 'start');
                Session::put('oauth_request_token', $token['oauth_token']);
                Session::put('oauth_request_token_secret', $token['oauth_token_secret']);
    
                return Redirect::to($url);
            }
    
            return Redirect::route('twitter.error');
        }
    
        function callback() {
            Log::info('callbackk call');
            // You should set this route on your Twitter Application settings as the callback
            // https://apps.twitter.com/app/YOUR-APP-ID/settings
            if (Session::has('oauth_request_token'))
            {
                $request_token = [
                    'token'  => Session::get('oauth_request_token'),
                    'secret' => Session::get('oauth_request_token_secret'),
                ];
    
                Twitter::reconfig($request_token);
    
                $oauth_verifier = false;
    
                if (Input::has('oauth_verifier'))
                {
                    $oauth_verifier = Input::get('oauth_verifier');
                    // getAccessToken() will reset the token for you
                    $token = Twitter::getAccessToken($oauth_verifier);
                    Log::info('token created: ' . print_r($token,true));
                }
    
                if (!isset($token['oauth_token_secret']))
                {
                    Log::info('oauth_token_secret fail');
                    return Redirect::route('twitter.error')->with('flash_error', 'We could not log you in on Twitter.');
                }
    
                $credentials = Twitter::getCredentials([
                    'include_email' => 'true',
                ]);
    
                if (is_object($credentials) && !isset($credentials->error))
                {
                    // $credentials contains the Twitter user object with all the info about the user.
                    // Add here your own user logic, store profiles, create new users on your tables...you name it!
                    // Typically you'll want to store at least, user id, name and access tokens
                    // if you want to be able to call the API on behalf of your users.
    
                    // This is also the moment to log in your users if you're using Laravel's Auth class
                    // Auth::login($user) should do the trick.
    
                    Session::put('access_token', $token);
                    Log::info('access_token: ' . print_r($token,true));
    
                    $users = Twitter::getUsers([
                        'user_id'=>$token['user_id']
                    ]);
    
                    Log::info('users: ' . print_r($users,true));
                    if(count($users) > 0) {
    
                        // pakai socialite untuk mengambil email
                        $user = Socialite::driver('twitter')->userFromTokenAndSecret($token['oauth_token'], $token['oauth_token_secret']);
                        $user_email = $user->getEmail();
                        $user_name = $user->getName();
    
                        if($user_email) {
                            $this->_set_login($user_name, $user_email);
                            Log::info("email sets");
                        } else {
                            Log::info("email not sets and go with id provider");
                        }
    
                    } else {
                        Log::info("users not found");
                    }
    
                    return Redirect::to('/')->with('flash_notice', 'Congrats! You've successfully signed in!');
                }
    
                Log::info('twitter.error');
                return Redirect::route('twitter.error')->with('flash_error', 'Crab! Something went wrong while signing you up!');
            }
    
            Log::info('end callbackk call');
        }
    
        private function _set_login($user_name, $user_email) {
            $UserRepository = new UserRepository;
            $data_user = $UserRepository->getByEmail($user_email);
            if(count($data_user) > 0) {
                // sudah terdaftar
                $user_id = $data_user->id;
            } else {
                // belum terdaftar dan jalankan proses register
                $data_save = [
                    'name' => $user_name,
                    'email' => $user_email,
                    'password' => Hash::make($user_email)
                ];
    
                $user_id = $UserRepository->save_data($data_save);
            }
    
            $user = User::find($user_id);
            Auth::login($user);
        }
    }
    

    maybe it can help you.

    the point code is here

    $user = Socialite::driver('twitter')->userFromTokenAndSecret($token['oauth_token'], $token['oauth_token_secret']);
    

    so with $user i can get the email and another property there.

    Login or Signup to reply.
  3. I had a work around to avoid method doesn’t exist "error"

    $user = call_user_func(array(Socialite::driver("facebook"),"userFromToken"),$token);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search