Skip to content

Instantly share code, notes, and snippets.

@mix5003
Last active July 31, 2021 03:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mix5003/aaa94d50bf2992915828bac3e451db86 to your computer and use it in GitHub Desktop.
Save mix5003/aaa94d50bf2992915828bac3e451db86 to your computer and use it in GitHub Desktop.
Line service provider for laravel socialite.

Do not use this gist

Go and use this https://socialiteproviders.netlify.com/providers/line.html

Old Data

  1. install socialiteproviders/manager
  2. Remove Laravel\Socialite\SocialiteServiceProvider from your providers[] array in config\app.php if you have added it already.
  3. Add \SocialiteProviders\Manager\ServiceProvider::class to your providers[] array in config\app.php.
  4. create LineExtendSocialite.php and Provider.php
  5. Add autoload to composer.json
  6. Add the Event and Listeners. like
protected $listen = [
    \SocialiteProviders\Manager\SocialiteWasCalled::class => [
        'SocialiteProviders\Line\LineExtendSocialite@handle',
    ],
];
  1. Add config in config/services.php. like
    'line' => [
        'client_id' => env('LINE_CHANNEL_ID'),
        'client_secret' => env('LINE_SECRET'),
        'redirect' => env('LINE_REDIRECT_URI'),
    ],
<?php
namespace SocialiteProviders\Line;
use SocialiteProviders\Manager\SocialiteWasCalled;
class LineExtendSocialite
{
/**
* Register the provider.
*
* @param \SocialiteProviders\Manager\SocialiteWasCalled $socialiteWasCalled
*/
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite(
'line', __NAMESPACE__.'\Provider'
);
}
}
<?php
namespace SocialiteProviders\Line;
use SocialiteProviders\Manager\OAuth2\User;
use Laravel\Socialite\Two\ProviderInterface;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
class Provider extends AbstractProvider implements ProviderInterface
{
/**
* Unique Provider Identifier.
*/
const IDENTIFIER = 'LINE';
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(
'https://access.line.me/dialog/oauth/weblogin', $state
);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://api.line.me/v2/oauth/accessToken';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(
'https://api.line.me/v2/profile', [
'headers' => [
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode($response->getBody()->getContents(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['userId'],
'nickname' => null,
'name' => $user['displayName'],
'email' => null,
'avatar' => $user['pictureUrl'],
]);
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return [
'grant_type' => 'authorization_code',
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'code' => $code,
'redirect_uri' => $this->redirectUrl,
];
}
}
@thongdam
Copy link

create LineExtendSocialite.php and Provider.php ไปสร้างไว้ที่ไหนครับ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment