SocialTalkController copy.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Themes\kbgolf\pro\app\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\CallApiService;
  5. use Illuminate\Support\Facades\Redirect;
  6. use SocialiteProviders\Apple\Provider;
  7. use Laravel\Socialite\Facades\Socialite;
  8. use Laravel\Socialite\Two\GoogleProvider;
  9. use Laravel\Socialite\Two\FacebookProvider;
  10. use SocialiteProviders\Kakao\KakaoProvider;
  11. use App\Providers\Socialite\OAuth2\DaboryProvider;
  12. use App\Services\AppleTokenService;
  13. use SocialiteProviders\Naver\Provider as NaverProvider;
  14. use Illuminate\Http\Request;
  15. class SocialTalkController extends Controller
  16. {
  17. private $appleTokenService;
  18. private $callApiService;
  19. private $oauth2Info;
  20. private $target;
  21. public function __construct(CallApiService $callApiService, AppleTokenService $appleTokenService)
  22. {
  23. $this->callApiService = $callApiService;
  24. $this->appleTokenService = $appleTokenService;
  25. }
  26. public function redirectToProvider($provider)
  27. {
  28. $this->oauth2Info = request('oauth2Info');
  29. // [$this->target, $ssoBrand] = explode('-', $provider);
  30. $brandCode = 'member-'.$provider;
  31. $response = $this->callApiService->callApi([
  32. 'url' => 'setup-page',
  33. 'data' => [
  34. 'PageVars' => [
  35. 'Query' => "(setup_code = 'sso-client' and brand_code = '{$brandCode}') and is_on_use = '1'",
  36. 'Limit' => 100
  37. ]
  38. ],
  39. ]);
  40. // dd($this->oauth2Info);
  41. // dd($response);
  42. if (!empty($response['Page'])) {
  43. $setupJson = $response['Page'][0]['SetupJson'];
  44. // dd($setupJson);
  45. $setupArray = json_decode($setupJson, true);
  46. $redirectUri = route('social.login', ['provider' => $brandCode]);
  47. if (json_last_error() === JSON_ERROR_NONE) {
  48. $clientId = $setupArray['ClientId'] ?? null;
  49. // dd($clientId);
  50. }
  51. }
  52. if ($provider == 'naver') {
  53. $url = 'https://nid.naver.com/oauth2.0/authorize?' . http_build_query([
  54. 'response_type' => 'code',
  55. 'client_id' => $clientId,
  56. 'redirect_uri' => $redirectUri,
  57. 'state' => csrf_token(),
  58. ]);
  59. } elseif ($provider == 'kakao') {
  60. $url = 'https://kauth.kakao.com/oauth/authorize?' . http_build_query([
  61. 'response_type' => 'code',
  62. 'client_id' => $clientId,
  63. 'redirect_uri' => $redirectUri,
  64. 'state' => csrf_token(),
  65. ]);
  66. }
  67. // dd($url);
  68. return redirect($url);
  69. }
  70. public function login($provider)
  71. {
  72. // dd($provider);
  73. [$this->target, $ssoBrand] = explode('-', $provider);
  74. if ($this->target == 'member') {
  75. if ($ssoBrand == 'naver') {
  76. // return Redirect::away('https://talk.naver.com/ct/w5u3g9');
  77. echo "<script>window.open('https://talk.naver.com/ct/w5u3g9', '_blank', 'width=600,height=600');</script>";
  78. return;
  79. }elseif($ssoBrand == 'kakao'){
  80. echo "<script>window.open('https://pf.kakao.com/_aAAGj/chat', '_blank', 'width=600,height=600');</script>";
  81. return;
  82. // return Redirect::away('https://pf.kakao.com/_aAAGj/chat');
  83. }
  84. }
  85. }
  86. public function setConfig($provider, $target)
  87. {
  88. $config = [
  89. 'client_id' => $this->oauth2Info['ClientId'],
  90. 'client_secret' => $this->oauth2Info['ClientSecret'],
  91. 'redirect' => "/social/$target-$provider/callback"
  92. ];
  93. switch ($provider) {
  94. case 'dabory':
  95. $driver = Socialite::buildProvider(DaboryProvider::class, [
  96. 'client_id' => config('app.api.main.ClientId'),
  97. 'client_secret' => config('app.api.main.ClientSecret'),
  98. 'redirect' => $config['redirect']
  99. ]);
  100. break;
  101. case 'facebook':
  102. $driver = Socialite::buildProvider(FacebookProvider::class, $config);
  103. break;
  104. case 'kakao':
  105. $driver = Socialite::buildProvider(KakaoProvider::class, $config);
  106. break;
  107. case 'google':
  108. $driver = Socialite::buildProvider(GoogleProvider::class, $config);
  109. break;
  110. case 'naver':
  111. $driver = Socialite::buildProvider(NaverProvider::class, $config);
  112. break;
  113. case 'apple':
  114. $config['client_secret'] = $this->appleTokenService->generate(
  115. $this->oauth2Info['PrivateKey'],
  116. $this->oauth2Info['ClientId'],
  117. $this->oauth2Info['TeamId'],
  118. $this->oauth2Info['KeyId'],
  119. );
  120. $driver = Socialite::buildProvider(Provider::class, $config);
  121. break;
  122. }
  123. return $driver;
  124. }
  125. }