class SaraminExtendSocialite
{
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite('saramin', \App\Oauth\SaraminProvider::class);
}
}
saramin의 socialite가 불렸을때 생성한 Provider를 실행할 수 있도록 Provider를 생성해주어야 한다.
4. EventServiceProvider의 listen에 ProviderManager 추가
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를 추가해줄 수 있다.