OAuthController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 simple(string $type)
  14. {
  15. $info = Socialite::driver($type)->stateless()->user();
  16. if ($info) {
  17. $user = Auth::user();
  18. if ($user) {
  19. return $this->bind($type, $user, $info);
  20. }
  21. return $this->login($type, $info);
  22. }
  23. return redirect()->route('login')->withErrors(trans('auth.oauth.login_failed'));
  24. }
  25. private function bind(string $type, $user, $info)
  26. {
  27. $auth = $user->userAuths()->whereType($type)->first();
  28. $data = ['type' => $type, 'identifier' => $info->getId(), 'credential' => $info->token];
  29. if ($auth) {
  30. $user->userAuths()->whereType($type)->update($data);
  31. return redirect()->route('profile')->with('successMsg', trans('auth.oauth.rebind_success'));
  32. }
  33. $user->userAuths()->create($data);
  34. return redirect()->route('profile')->with('successMsg', trans('auth.oauth.bind_success'));
  35. }
  36. public function route(Request $request, string $type)
  37. {
  38. $action = $request->input('action');
  39. $key = "services.{$type}.redirect";
  40. if ($action === 'binding') {
  41. config([$key => route('oauth.bind', ['type' => $type])]);
  42. } elseif ($action === 'register') {
  43. config([$key => route('oauth.register', ['type' => $type])]);
  44. } else {
  45. config([$key => route('oauth.login', ['type' => $type])]);
  46. }
  47. return Socialite::driver($type)->redirect();
  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. if (! sysConfig('is_register')) {
  98. return redirect()->route('register')->withErrors(trans('auth.register.error.disable'));
  99. }
  100. if ((int) sysConfig('is_invite_register') === 2) { // 必须使用邀请码
  101. return redirect()->route('register')->withErrors(trans('validation.required', ['attribute' => trans('auth.invite.attribute')]));
  102. }
  103. config(["services.{$type}.redirect" => route('oauth.register', ['type' => $type])]);
  104. $info = Socialite::driver($type)->stateless()->user();
  105. // 排除重复用户注册
  106. if ($info) {
  107. $user = User::whereUsername($info->getEmail())->first();
  108. if (! $user) {
  109. $user = UserOauth::whereIdentifier($info->getId())->first();
  110. if (! $user) {
  111. $user = Helpers::addUser($info->getEmail(), Str::random(), MB * ((int) sysConfig('default_traffic')), null, $info->getNickname());
  112. if ($user) {
  113. $user->userAuths()->create([
  114. 'type' => $type,
  115. 'identifier' => $info->getId(),
  116. 'credential' => $info->token,
  117. ]);
  118. Auth::login($user);
  119. return redirect()->route('login');
  120. }
  121. return redirect()->route('register')->withErrors(trans('auth.oauth.register_failed'));
  122. }
  123. }
  124. return redirect()->route('login')->withErrors(trans('auth.oauth.registered'));
  125. }
  126. return redirect()->route('login')->withErrors(trans('auth.oauth.login_failed'));
  127. }
  128. }