SocialController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Services\DbUpdateService;
  4. use Exception;
  5. use App\Services\CallApiService;
  6. use App\Http\Controllers\Controller;
  7. use Illuminate\Support\Facades\Route;
  8. use Laravel\Socialite\Facades\Socialite;
  9. use Laravel\Socialite\Two\FacebookProvider;
  10. use Laravel\Socialite\Two\GoogleProvider;
  11. use SocialiteProviders\Apple\Provider;
  12. use SocialiteProviders\Kakao\KakaoProvider;
  13. use App\Providers\Socialite\OAuth2\DaboryProvider;
  14. use SocialiteProviders\Naver\Provider as NaverProvider;
  15. use App\Services\AppleTokenService;
  16. class SocialController extends Controller
  17. {
  18. private $callApiService;
  19. private $dbUpdateService;
  20. private $appleTokenService;
  21. private $oauth2Info;
  22. private $target;
  23. public function __construct(CallApiService $callApiService, DbUpdateService $dbUpdateService, AppleTokenService $appleTokenService)
  24. {
  25. $this->callApiService = $callApiService;
  26. $this->dbUpdateService = $dbUpdateService;
  27. $this->appleTokenService = $appleTokenService;
  28. }
  29. public function redirectToProvider($provider)
  30. {
  31. $this->oauth2Info = request('oauth2Info');
  32. return $this->setConfig($provider, request('target'))->redirect();
  33. }
  34. public function login($provider)
  35. {
  36. [$this->target, $ssoBrand] = explode('-', $provider);
  37. $response = $this->callApiService->callApi([
  38. 'url' => 'setup-page',
  39. 'data' => [
  40. 'PageVars' => [
  41. 'Query' => "(setup_code = 'sso-client' and brand_code = '{$provider}') and is_on_use = '1'",
  42. 'Limit' => 100
  43. ]
  44. ],
  45. ]);
  46. $this->oauth2Info = json_decode($response['Page'][0]['SetupJson'], true);
  47. $loginRoute = $this->target == 'member' ? 'member-login' : 'user-login';
  48. try {
  49. $userSocial = $this->setConfig($ssoBrand, $this->target)->user();
  50. } catch (Exception $e) {
  51. return redirect()->route($loginRoute)->with(['mgs' => $e->getMessage()]);
  52. }
  53. if ($this->target == 'member') {
  54. $response = $this->memberLogin($userSocial, $ssoBrand);
  55. if ($response['success']) {
  56. if (Route::has('member-social-broker')) {
  57. return redirect()->route('member-social-broker');
  58. }
  59. session()->put('member.is_member', true);
  60. $member = session()->get('member');
  61. if ($ssoBrand === 'dabory') {
  62. $redirectUrl = $this->oauth2Info['AfterMemberLoginUri'];
  63. } else {
  64. $redirectUrl = getLoginRedirectUrl($this->oauth2Info['AfterMemberLoginUri'], $member['MemberId']);
  65. if ($redirectUrl === '/my-page/member-edit') {
  66. notify()->info('닉네임, 셩별, 관심국가를 입력하셔야 정상활동 가능합니다.', 'Info', 'bottomRight');
  67. }
  68. }
  69. return redirect()->to($redirectUrl);
  70. } else {
  71. return redirect()->route('member-login')->with(['mgs' => $response['mgs']]);
  72. }
  73. } else {
  74. $response = $this->usersLogin($userSocial, $ssoBrand);
  75. if ($response['success']) {
  76. // IS_SKIP_DBUPDATE 체크
  77. return $this->dbUpdateService->checkIsSkipDbUpdate();
  78. } else {
  79. return redirect()->route('user-login')->with(['mgs' => $response['mgs']]);
  80. }
  81. }
  82. }
  83. public function memberLogin($userSocial, $provider)
  84. {
  85. $response = $this->callApiService->callApi([
  86. 'url' => 'member-sso-login',
  87. 'data' => [
  88. 'Email' => $userSocial->getEmail(),
  89. 'SsoBrand' => $provider,
  90. 'SsoSub' => (String) $userSocial->getId(),
  91. ]
  92. ]);
  93. if (isset($response['apiStatus'])) {
  94. return ['success' => false, 'mgs' => $response['body']];
  95. }
  96. session()->put('member', array_merge($response, ['Ip' => request()->ip()]));
  97. return ['success' => true];
  98. }
  99. public function usersLogin($userSocial, $provider)
  100. {
  101. $response = $this->callApiService->callApi([
  102. 'url' => 'user-sso-login',
  103. 'data' => [
  104. 'Email' => $userSocial->getEmail(),
  105. 'SsoBrand' => $provider,
  106. 'SsoSub' => (String) $userSocial->getId(),
  107. ]
  108. ]);
  109. if (isset($response['apiStatus'])) {
  110. return ['success' => false, 'mgs' => $response['body']];
  111. }
  112. session()->put('user', array_merge($response, ['Ip' => request()->ip()]));
  113. return ['success' => true];
  114. }
  115. public function setConfig($provider, $target)
  116. {
  117. $config = [
  118. 'client_id' => $this->oauth2Info['ClientId'],
  119. 'client_secret' => $this->oauth2Info['ClientSecret'],
  120. 'redirect' => "/social/$target-$provider/callback"
  121. ];
  122. // dd($config);
  123. switch ($provider) {
  124. case 'dabory':
  125. $driver = Socialite::buildProvider(DaboryProvider::class, [
  126. 'client_id' => config('app.api.main.ClientId'),
  127. 'client_secret' => config('app.api.main.ClientSecret'),
  128. 'redirect' => $config['redirect']
  129. ]);
  130. break;
  131. case 'facebook':
  132. $driver = Socialite::buildProvider(FacebookProvider::class, $config);
  133. break;
  134. case 'kakao':
  135. $driver = Socialite::buildProvider(KakaoProvider::class, $config);
  136. break;
  137. case 'google':
  138. $driver = Socialite::buildProvider(GoogleProvider::class, $config);
  139. break;
  140. case 'naver':
  141. $driver = Socialite::buildProvider(NaverProvider::class, $config);
  142. break;
  143. case 'apple':
  144. // dd($this->oauth2Info);
  145. $config['client_secret'] = $this->appleTokenService->generate(
  146. $this->oauth2Info['PrivateKey'],
  147. $this->oauth2Info['ClientId'],
  148. $this->oauth2Info['TeamId'],
  149. $this->oauth2Info['KeyId'],
  150. );
  151. $driver = Socialite::buildProvider(Provider::class, $config);
  152. break;
  153. }
  154. return $driver;
  155. }
  156. }