OAuthController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\Helpers;
  4. use App\Components\IP;
  5. use App\Models\User;
  6. use App\Models\UserOauth;
  7. use Auth;
  8. use Illuminate\Http\Request;
  9. use Laravel\Socialite\Facades\Socialite;
  10. use Str;
  11. class OAuthController extends Controller
  12. {
  13. public function route(Request $request, string $type)
  14. {
  15. $action = $request->input('action');
  16. $key = "services.{$type}.redirect";
  17. if ($action === 'binding') {
  18. config([$key => route('oauth.bind', ['type' => $type])]);
  19. } elseif ($action === 'register') {
  20. config([$key => route('oauth.register', ['type' => $type])]);
  21. } else {
  22. config([$key => route('oauth.login', ['type' => $type])]);
  23. }
  24. return Socialite::driver($type)->redirect();
  25. }
  26. public function simple(string $type)
  27. {
  28. $info = Socialite::driver($type)->stateless()->user();
  29. if ($info) {
  30. $user = Auth::user();
  31. if ($user) {
  32. return $this->bind($type, $user, $info);
  33. }
  34. return $this->login($type, $info);
  35. }
  36. return redirect()->route('login')->withErrors(trans('auth.oauth.login_failed'));
  37. }
  38. private function bind(string $type, $user, $info)
  39. {
  40. $auth = $user->userAuths()->whereType($type)->first();
  41. $data = ['type' => $type, 'identifier' => $info->getId(), 'credential' => $info->token];
  42. if ($auth) {
  43. $user->userAuths()->whereType($type)->update($data);
  44. return redirect()->route('profile')->with('successMsg', trans('auth.oauth.rebind_success'));
  45. }
  46. $user->userAuths()->create($data);
  47. return redirect()->route('profile')->with('successMsg', trans('auth.oauth.bind_success'));
  48. }
  49. private function login(string $type, $info)
  50. {
  51. $user = User::whereUsername($info->getEmail())->first();
  52. if (! isset($user)) {
  53. $auth = UserOauth::whereType($type)->whereIdentifier($info->getId())->first();
  54. if (isset($auth)) {
  55. $user = $auth->user;
  56. }
  57. }
  58. if (isset($user)) {
  59. Auth::login($user);
  60. Helpers::userLoginAction($user, IP::getClientIp()); // 用户登录后操作
  61. return redirect()->route('login');
  62. }
  63. return redirect()->route('login')->withErrors(trans('auth.error.not_found_user'));
  64. }
  65. public function unsubscribe(string $type)
  66. {
  67. $user = Auth::user();
  68. if ($user && $user->userAuths()->whereType($type)->delete()) {
  69. return redirect()->route('profile')->with('successMsg', trans('auth.oauth.unbind_success'));
  70. }
  71. return redirect()->route('profile')->with('successMsg', trans('auth.oauth.unbind_failed'));
  72. }
  73. public function binding($type)
  74. {
  75. config(["services.{$type}.redirect" => route('oauth.bind', ['type' => $type])]);
  76. $info = Socialite::driver($type)->stateless()->user();
  77. if ($info) {
  78. $user = Auth::user();
  79. if ($user) {
  80. return $this->bind($type, $user, $info);
  81. }
  82. return redirect()->route('profile')->withErrors(trans('auth.oauth.bind_failed'));
  83. }
  84. return redirect()->route('login')->withErrors(trans('auth.oauth.login_failed'));
  85. }
  86. public function logining($type)
  87. {
  88. config(["services.{$type}.redirect" => route('oauth.login', ['type' => $type])]);
  89. $info = Socialite::driver($type)->stateless()->user();
  90. if ($info) {
  91. return $this->login($type, $info);
  92. }
  93. return redirect()->route('login')->withErrors(trans('auth.oauth.login_failed'));
  94. }
  95. public function register($type)
  96. {
  97. config(["services.{$type}.redirect" => route('oauth.register', ['type' => $type])]);
  98. $info = Socialite::driver($type)->stateless()->user();
  99. // 排除重复用户注册
  100. if ($info) {
  101. $user = User::whereUsername($info->getEmail())->first();
  102. if (! $user) {
  103. $user = UserOauth::whereIdentifier($info->getId())->first();
  104. if (! $user) {
  105. $user = Helpers::addUser($info->getEmail(), Str::random(), MB * ((int) sysConfig('default_traffic')), null, $info->getNickname());
  106. if ($user) {
  107. $user->userAuths()->create([
  108. 'type' => $type,
  109. 'identifier' => $info->getId(),
  110. 'credential' => $info->token,
  111. ]);
  112. Auth::login($user);
  113. return redirect()->route('login');
  114. }
  115. return redirect()->route('register')->withErrors(trans('auth.oauth.register_failed'));
  116. }
  117. }
  118. return redirect()->route('login')->withErrors(trans('auth.oauth.registered'));
  119. }
  120. return redirect()->route('login')->withErrors(trans('auth.oauth.login_failed'));
  121. }
  122. }