socialiteproviders ์ด์ฉํ๋ ๋ฐฉ๋ฒ
socialite๋ฅผ ์ด์ฉํ์ฌ ์ปค์คํ
provider๋ฅผ ๋ง๋ค์ด ์ ์ฉํ๋ ค๊ณ ํ ๋ ๊ทธ๋ฅ ์ ์ฉํ๋ ๊ฒ์ ๋ณต์กํ ์ ์๋๋ฐ socialiteproviders/manager
๋ผ๋ ๋ฒค๋๋ฅผ ์ด์ฉํ๋ฉด ์ปค์คํ
์ ์ฉ์ด ์ฝ๊ฒ ๊ฐ๋ฅํ๋ค.
ํ์คํ ๊ตฌ์กฐ๋ฅผ ์๊ณ ์ถ๋ค๋ฉด socialiteproviders/naver
์ ๊ฐ์ ๋ค๋ฅธ ์๋น์ค์ oauth ๋ฒค๋๋ฅผ ๋ค์ด๋ฐ์ ๋ฏ์ด๋ณด๋ฉด ๋๊ณ ํ์ฌ ์์ฑํ๋ ๊ธ๋ ๋ค๋ฅธ ์๋น์ค์ ๊ตฌ์กฐ๋ฅผ ๋ฏ์ด ๋น์ทํ๊ฒ ์ ์ฉํ ์ฌ๋ก์ด๋ค.
์ฐ์ socialiteproviders/manager
๋ฅผ ์ค์นํด์ฃผ๋ฉด๋๋ค.
1. ํ๊ฒฝ ์ค์ ํด์ฃผ๊ธฐ
//config/services.php
return [
//...
'saramin' => [
'client_id' => env('SARAMIN_ID'),
'client_secret' => env('SARAMIN_SECRET'),
'redirect' => env('SARAMIN_CALLBACK'),
],
//...
];
//config/app.php
'providers' => [
//...
\SocialiteProviders\Manager\ServiceProvider::class,
],
'aliases' => [
//...
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
]
์์ ๊ฐ์ด services.php์ app.php์ ์ถ๊ฐํด์ฃผ๊ณ .env
์ ๊ฐ์ services.php์ 3๊ฐ์ ํญ๋ชฉ์ ๋ํด ์ ๋ณด๋ฅผ ๊ธฐ์
ํด์ฃผ์ด์ผ ํ๋ค.
2. Provider ์ถ๊ฐ
class SaraminProvider extends AbstractProvider
{
public const IDENTIFIER = 'SARAMIN';
private const OAUTH_URL = 'url';
private const GATEWAY_URL = 'url';
protected $scopes = [
"openid",
"scopes",
//...
];
protected $scopeSeparator = ' ';
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase(
self::OAUTH_URL . '/oauth/authorize',
$state
);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl(): string
{
return self::OAUTH_URL . '/oauth/token';
}
/**
* {@inheritdoc}
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(
self::GATEWAY_URL . '/api/user/oauth/user',
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token,
],
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject($user): User
{
return (new User())->setRaw($user)->map([
'id' => Arr::get($user, 'sub'),
'name' => Arr::get($user, 'name'),
'email' => Arr::get($user, 'email'),
]);
}
}
3. ProviderManager ์์ฑ
class SaraminExtendSocialite
{
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite('saramin', \App\Oauth\SaraminProvider::class);
}
}
saramin์ socialite๊ฐ ๋ถ๋ ธ์๋ ์์ฑํ Provider๋ฅผ ์คํํ ์ ์๋๋ก Provider๋ฅผ ์์ฑํด์ฃผ์ด์ผ ํ๋ค.
4. EventServiceProvider์ listen์ ProviderManager ์ถ๊ฐ
//...
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
'App\Oauth\SaraminExtendSocialite@handle',
],
];
5. Controller ์์ฑ
class SocialController extends Controller
{
/**
* Handle social login process.
*
* @param Request $request
* @param string $provider
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function execute(Request $request, string $provider){
//์ธ์ฆ์๋ฒ๋ก redirect
if (! $request->has('code')){
return $this->redirectToProvider($provider);
}
//token์ ๊ฐ์ง๊ณ ์๋ค๋ฉด token์๋ฒ๋ก redirect
return $this->handleProviderCallback($provider);
}
/**
* Redirect the user to the Social Login Provider's authentication page.
*
* @param string $provider
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* @throws BadRequestException
*/
protected function redirectToProvider(string $provider): \Symfony\Component\HttpFoundation\RedirectResponse
{
switch ($provider){
case 'saramin' :
return Socialite::driver($provider)
->redirect();
default:
throw new BadRequestException("์๋ชป๋ ์์ฒญ์
๋๋ค.");
}
}
/**
* Obtain the user information from the Social Login Provider.
*
* @param string $provider
* @return Application|Redirector|RedirectResponse
* @throws UnauthorizeException
* @throws InternalServerException
*/
protected function handleProviderCallback(string $provider){
$socialData = Socialite::driver($provider)->user();
//์ธ์ฆ ๋ก์ง...
return redirect('/success');
}
}
์์ ๊ฐ์ด Controller๊น์ง ์์ฑํด์คํ web.php์ ๋ผ์ฐํฐ๋ง ๋ฑ๋ก์ ์์ผ์ฃผ๋ฉด ์ ์ฉ์ด ๋๋ค!!
Socialiteproviders๊ฐ ์๋ Socialite๋ง ์ด์ฉํ ๋ฐฉ๋ฒ
1. ์ค์ ํ์ผ๊ณผ Provider ์์ฑ
์ฒซ๋ฒ์งธ์ ๋ฐฉ๋ฒ๊ณผ ๊ฑฐ์ ๋์ผํ๋ app.php์์ providers์ ์ ๋ณด๋ง ์์ ํด์ฃผ๋ฉด ๋๋ค.
//์์ ์
\SocialiteProviders\Manager\ServiceProvider::class,
//์์ ํ
Laravel\Socialite\SocialiteServiceProvider::class,
2. AppServiceProvider์ ์ฌ๋์ธProvider ๋ฑ๋ก
//AppserviceProvider ํด๋์ค ๋ด๋ถ
public function boot(){
$socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
$socialite->extend(
'saramin',
function ($app) use ($socialite) {
$config = $app['config']['services.saramin'];
return $socialite->buildProvider(SaraminProvider::class, $config);
}
);
}
boot()๋ฉ์๋๋ ๋ค๋ฅธ ์๋น์ค ํ๋ก๋ฐ์ด๋๋ค์ด ๋ฑ๋ก๋ ์ดํ์ ํธ์ถ๋๊ธฐ ๋๋ฌธ์ Socialite์ ์์ ๊ฐ์ด ์ ๊ทผํ์ฌ Provider๋ฅผ ์ถ๊ฐํด์ค ์ ์๋ค.