khoanguyenme's avatar

Store Laravel Socialite Access Token

I'm writing a custom Socialite Pushbullet provider for pushing notication to my user.

I have to admit, with Laravel Socialite, everything are damn easy. But I need to store access_token for later use. Here is my PushbulletProvider

<?php namespace Quiz\lib\ThirdParty\Socialite;

use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;

use Symfony\Component\HttpFoundation\RedirectResponse;

class PushbulletProvider extends AbstractProvider implements ProviderInterface {

    /**
     * {@inheritdoc}
     */
    protected function getAuthUrl($state)
    {
        return $this->buildAuthUrlFromBase('https://www.pushbullet.com/authorize', $state);
    }

    /**
     * {@inheritdoc}
     */
    protected function getTokenUrl()
    {
        return 'https://api.pushbullet.com/oauth2/token';
    }

    /**
     * Get the access token for the given code.
     *
     * @param  string  $code
     * @return string
     */
    public function getAccessToken($code)
    {
        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
            'body' => $this->getTokenFields($code),
        ]);

        return $this->parseAccessToken($response->getBody());
    }

    /**
     * Get the POST fields for the token request.
     *
     * @param  string  $code
     * @return array
     */
    protected function getTokenFields($code)
    {
        return array_add(
            parent::getTokenFields($code), 'grant_type', 'authorization_code'
        );
    }

    /**
     * {@inheritdoc}
     */
    protected function getUserByToken($token)
    {
        // Can catch token here
        dd($token);

        $response = $this->getHttpClient()->get('https://api.pushbullet.com/v2/users/me', [
            'headers' => [
                'Accept' => 'application/json',
                'Authorization' => 'Bearer '.$token,
            ],
        ]);

        return json_decode($response->getBody(), true);
    }

    /**
     * {@inheritdoc}
     */
    protected function mapUserToObject(array $user)
    {
        return (new User)->setRaw($user)->map([
            'id' => $user['iden'],
            'nickname' => null,
            'name' => $user['name'],
            'email' => $user['email'],
            'avatar' => array_get($user, 'image_url'),
        ]);
    }

}

As you can see, I use dd($token); . That is the token. What is the best to to catch it and saved it into database and associate with user?

0 likes
6 replies
digitalrisks's avatar
Level 16

Hi,

I would do that from outside your Socialite Provider, it isn't up to the provider to save things to the database.

You should be doing that after you've had a successful auth, for example if you've added your provider like from my article..

$user = Socialize::with('pushbullet')->user();

$user->token;
2 likes
Amine_Mejri's avatar

@digitalrisks When I try to save the access token in the 'remember_token' field of users table it returns this error: 'SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'remember_token' at row 1' where should I save the token because I am going to use it multiple times in the futur

khoanguyenme's avatar

@morrislaptop Thank you so much. This is a dead simple solution. I wasted a night to add command handler to add this access token into database

Please or to participate in this conversation.