OAuthController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\User;
  4. use App\Models\UserOauth;
  5. use App\Utils\Helpers;
  6. use App\Utils\IP;
  7. use Auth;
  8. use Illuminate\Http\RedirectResponse;
  9. use Laravel\Socialite\Facades\Socialite;
  10. use Str;
  11. class OAuthController extends Controller
  12. {
  13. public function simple(string $type): RedirectResponse
  14. {
  15. $info = Socialite::driver($type)->stateless()->user();
  16. if ($info) {
  17. $user = Auth::user();
  18. if ($user) {
  19. return $this->binding($type, $user, $info);
  20. }
  21. return $this->logging($type, $info);
  22. }
  23. return redirect()->route('login')->withErrors(trans('auth.oauth.login_failed'));
  24. }
  25. private function binding(string $type, User $user, \Laravel\Socialite\Contracts\User $OauthUser): RedirectResponse
  26. {
  27. $data = ['type' => $type, 'identifier' => $OauthUser->getId(), 'credential' => $OauthUser->token];
  28. if ($user->userAuths()->whereType($type)->updateOrCreate($data)) {
  29. return redirect()->route('profile')->with('successMsg', trans('auth.oauth.bind_success'));
  30. }
  31. return redirect()->route('profile')->withErrors(trans('auth.oauth.bind_failed'));
  32. }
  33. private function logging(string $type, \Laravel\Socialite\Contracts\User $OauthUser): RedirectResponse
  34. {
  35. $user = User::whereUsername($OauthUser->getEmail())->first();
  36. if (! isset($user)) {
  37. $auth = UserOauth::whereType($type)->whereIdentifier($OauthUser->getId())->first();
  38. if (isset($auth)) {
  39. $user = $auth->user;
  40. }
  41. }
  42. if (isset($user)) {
  43. Auth::login($user);
  44. Helpers::userLoginAction($user, IP::getClientIp()); // 用户登录后操作
  45. return redirect()->route('login');
  46. }
  47. return redirect()->route('login')->withErrors(trans('auth.error.not_found_user'));
  48. }
  49. public function login(string $type): RedirectResponse
  50. {
  51. $info = Socialite::driver($type)->stateless()->user();
  52. if ($info) {
  53. return $this->logging($type, $info);
  54. }
  55. return redirect()->route('login')->withErrors(trans('auth.oauth.login_failed'));
  56. }
  57. public function unbind(string $type): RedirectResponse
  58. {
  59. $user = Auth::user();
  60. if ($user && $user->userAuths()->whereType($type)->delete()) {
  61. return redirect()->route('profile')->with('successMsg', trans('auth.oauth.unbind_success'));
  62. }
  63. return redirect()->route('profile')->with('successMsg', trans('auth.oauth.unbind_failed'));
  64. }
  65. public function bind(string $type): RedirectResponse
  66. {
  67. $info = Socialite::driver($type)->stateless()->user();
  68. if ($info) {
  69. $user = Auth::user();
  70. if ($user) {
  71. return $this->binding($type, $user, $info);
  72. }
  73. return redirect()->route('profile')->withErrors(trans('auth.oauth.bind_failed'));
  74. }
  75. return redirect()->route('login')->withErrors(trans('auth.oauth.login_failed'));
  76. }
  77. public function register(string $type): RedirectResponse
  78. {
  79. if (! sysConfig('is_register')) {
  80. return redirect()->route('register')->withErrors(trans('auth.register.error.disable'));
  81. }
  82. if ((int) sysConfig('is_invite_register') === 2) { // 必须使用邀请码
  83. return redirect()->route('register')->withErrors(trans('validation.required', ['attribute' => trans('auth.invite.attribute')]));
  84. }
  85. $OauthUser = Socialite::driver($type)->stateless()->user();
  86. if ($OauthUser) {
  87. if (User::whereUsername($OauthUser->getEmail())->doesntExist() && UserOauth::whereIdentifier($OauthUser->getId())->doesntExist()) { // 排除重复用户注册
  88. $user = Helpers::addUser($OauthUser->getEmail(), Str::random(), MiB * sysConfig('default_traffic'), (int) sysConfig('default_days'), $OauthUser->getNickname());
  89. $user->userAuths()->create([
  90. 'type' => $type,
  91. 'identifier' => $OauthUser->getId(),
  92. 'credential' => $OauthUser->token,
  93. ]);
  94. Auth::login($user);
  95. return redirect()->route('login');
  96. }
  97. return redirect()->route('login')->withErrors(trans('auth.oauth.registered'));
  98. }
  99. return redirect()->route('login')->withErrors(trans('auth.oauth.login_failed'));
  100. }
  101. }