Browse Source

Fix bugs in OAuth binding logic

兔姬桑 4 years ago
parent
commit
5c7b7d256a

+ 9 - 5
app/Http/Controllers/OAuth/BaseController.php

@@ -44,11 +44,15 @@ class BaseController extends Controller
 
     private function bind(string $type, $user, $info)
     {
-        $user->userAuths()->create([
-            'type'       => $type,
-            'identifier' => $info->getId(),
-            'credential' => $info->token,
-        ]);
+        $auth = $user->userAuths()->whereType($type)->first();
+        $data = ['type' => $type, 'identifier' => $info->getId(), 'credential' => $info->token];
+        if ($auth) {
+            $user->userAuths()->whereType($type)->update($data);
+
+            return redirect()->route('profile')->with('successMsg', '重新绑定成功');
+        }
+
+        $user->userAuths()->create($data);
 
         return redirect()->route('profile')->with('successMsg', '绑定成功');
     }

+ 3 - 2
database/migrations/2021_06_16_115448_oauth.php

@@ -16,12 +16,13 @@ class Oauth extends Migration
     {
         Schema::create('user_oauth', function (Blueprint $table) {
             $table->increments('id');
-            $table->unsignedInteger('user_id')->unique()->comment('用户ID');
+            $table->unsignedInteger('user_id')->comment('用户ID');
             $table->string('type', 10)->comment('登录类型');
             $table->string('identifier', 128)->unique()->comment('手机号/邮箱/第三方的唯一标识');
-            $table->string('credential', 128)->comment('密码/Token凭证');
+            $table->string('credential', 128)->nullable()->comment('密码/Token凭证');
             $table->dateTime('created_at')->comment('创建时间');
             $table->dateTime('updated_at')->comment('最后更新时间');
+            $table->unique(['user_id', 'type']);
             $table->foreign('user_id')->references('id')->on('user')->cascadeOnDelete();
         });
 

+ 5 - 3
resources/views/auth/register.blade.php

@@ -105,9 +105,11 @@
                 <span> 快速注册 </span>
             </div>
             @foreach (json_decode(sysConfig('oauth_path')) as $item)
-                <a class="btn btn-icon btn-pure" href="{{route('oauth.route', ['type' => $item, 'action' => 'register'])}}">
-                    <i class="fab {{config('common.oauth.icon')[$item]}} fa-lg" aria-hidden="true"></i>
-                </a>
+                @if ($item !== 'telegram')
+                    <a class="btn btn-icon btn-pure" href="{{route('oauth.route', ['type' => $item, 'action' => 'register'])}}">
+                        <i class="fab {{config('common.oauth.icon')[$item]}} fa-lg" aria-hidden="true"></i>
+                    </a>
+                @endif
             @endforeach
         </div>
     @endif

+ 11 - 10
resources/views/user/profile.blade.php

@@ -27,7 +27,7 @@
                             <x-avatar :user="Auth::getUser()"/>
                         </a>
                         <h4 class="user-name">{{Auth::getUser()->nickname}}</h4>
-                        <p class="user-job"> <i class="fab fa-weixin fa-lg mr-10" aria-hidden="true"></i> {{trans('common.payment.wechat')}}:
+                        <p class="user-job"><i class="fab fa-weixin fa-lg mr-10" aria-hidden="true"></i> {{trans('common.payment.wechat')}}:
                             @if(Auth::getUser()->wechat) {{Auth::getUser()->wechat}} @else {{trans('common.none')}} @endif</p>
                         <p class="user-location"><i class="fab fa-qq fa-lg mr-10" aria-hidden="true"></i> QQ:
                             @if(Auth::getUser()->qq) {{Auth::getUser()->qq}} @else {{trans('common.none')}} @endif</p>
@@ -38,17 +38,18 @@
                         </div>
                         <div class="user-socials list-group-gap list-group-full">
                             @foreach (json_decode(sysConfig('oauth_path')) as $item)
-                                @if (in_array($item, $auth))
-                                    <a class="list-group-item justify-content-center" href="{{route('oauth.route', ['type' => $item, 'action' => 'binding'])}}">
-                                        <i class="fab {{config('common.oauth.icon')[$item]}} fa-lg mr-10" aria-hidden="true"></i> {{config('common.oauth.labels')[$item]}} :
+                                <a class="list-group-item justify-content-center"
+                                   @if($item !== 'telegram') href="{{route('oauth.route', ['type' => $item, 'action' => 'binding'])}}" @endif>
+                                    <i class="fab {{config('common.oauth.icon')[$item]}} fa-lg mr-10" aria-hidden="true"></i> {{config('common.oauth.labels')[$item]}} :
+                                    @if(in_array($item, $auth))
                                         <span class="red-600">重新绑定</span>
-                                    </a>
-                                @else
-                                    <a class="list-group-item justify-content-center" href="{{route('oauth.route', ['type' => $item, 'action' => 'binding'])}}">
-                                        <i class="fab {{config('common.oauth.icon')[$item]}} fa-lg mr-10" aria-hidden="true"></i> {{config('common.oauth.labels')[$item]}} :
+                                    @else
                                         <span class="grey-500">未绑定</span>
-                                    </a>
-                                @endif
+                                    @endif
+                                    @if($item === 'telegram')
+                                        {!! Socialite::driver('telegram')->getButton() !!}
+                                    @endif
+                                </a>
                             @endforeach
                         </div>
                     @endif

+ 1 - 1
routes/web.php

@@ -16,7 +16,7 @@ Route::middleware(['isForbidden', 'affiliate', 'isMaintenance'])->group(function
     Route::get('lang/{locale}', 'AuthController@switchLang')->name('lang'); // 语言切换
     Route::get('login', 'AuthController@showLoginForm')->middleware('isSecurity')->name('login'); // 登录页面
 
-    Route::namespace('OAuth')->prefix('oauth/')->name('oauth.')->group(function () { // 用户第三方登录默认登录/转跳方式
+    Route::namespace('OAuth')->prefix('oauth')->name('oauth.')->group(function () { // 用户第三方登录默认登录/转跳方式
         Route::get('{type}/login', 'BaseController@logining')->name('login');
         Route::get('{type}/bind', 'BaseController@binding')->name('bind');
         Route::get('{type}/register', 'BaseController@register')->name('register');