Ver Fonte

完善jwt用户认证

haozi há 5 anos atrás
pai
commit
5ef756f26c

+ 9 - 2
app/Http/Controllers/api/procurement/wechat/AuthController.php

@@ -2,6 +2,7 @@
 
 namespace App\Http\Controllers\api\procurement\wechat;
 
+use App\UserDetail;
 use Illuminate\Support\Facades\Auth;
 use App\Http\Controllers\Controller;
 
@@ -31,10 +32,16 @@ class AuthController extends Controller
      */
     public function login()
     {
-        $credentials = request(['phone', 'password']);
+        $phone=request('phone');
+        $user_id=UserDetail::query()->where('mobile_phone',$phone)->value('user_id');
+        $credentials = [
+            'id'=>$user_id,
+            'password'=>request('password')
+        ];
+
 
         if (! $token = auth('api')->attempt($credentials)) {
-            return response()->json(['error' => '很抱歉,您的用户名和密码不匹配'], 401);
+            return response()->json(['error' => '很抱歉,您的手机号和密码不匹配'], 401);
         }
 
         return $this->respondWithToken($token);

+ 1 - 0
app/Http/Kernel.php

@@ -74,6 +74,7 @@ class Kernel extends HttpKernel
         'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
         'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
         'auth.api' => \App\Http\Middleware\ApiAuth::class,
+        'procurement.auth.api' => \App\Http\Middleware\ProcurementApiAuth::class,
     ];
 
     /**

+ 56 - 0
app/Http/Middleware/ProcurementApiAuth.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use Closure;
+use Tymon\JWTAuth\Exceptions\JWTException;
+use Tymon\JWTAuth\Exceptions\TokenExpiredException;
+use Tymon\JWTAuth\Exceptions\TokenInvalidException;
+use Tymon\JWTAuth\Facades\JWTAuth;
+
+
+class ProcurementApiAuth
+{
+    /**
+     * Handle an incoming request.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \Closure  $next
+     * @return mixed
+     */
+    public function handle($request, Closure $next)
+    {
+        try {
+            if (! $user = JWTAuth::parseToken()->authenticate()) {
+                return response()->json([
+                    'code' => 1004,
+                    'msg' => '用户不存在'
+
+                ], 404);
+            }
+            return $next($request);
+
+        } catch (TokenExpiredException $e) {
+
+            return response()->json([
+                'code' => 1003,
+                'msg' => 'token 过期' ,
+            ]);
+
+        } catch (TokenInvalidException $e) {
+
+            return response()->json([
+                'code' => 1002,
+                'msg' => 'token 无效',
+            ]);
+
+        }catch (JWTException $e) {
+
+            return response()->json([
+                'code' => 1001,
+                'msg' => '缺少token' ,
+            ]);
+
+        }
+    }
+}

+ 0 - 11
app/User.php

@@ -26,10 +26,6 @@ class User extends Authenticatable implements JWTSubject
         'name', 'email', 'password'
     ];
 
-    protected $appends=[
-      'phone'
-    ];
-
     /**
      * The attributes that should be hidden for arrays.
      *
@@ -85,9 +81,6 @@ class User extends Authenticatable implements JWTSubject
     function roles(){
         return $this->belongsToMany('App\Role','user_role','id_user','id_role');
     }
-    function userDetail(){
-        return $this->hasOne('App\UserDetail','user_id','id');
-    }
 
     function logistics(){
         return $this->belongsToMany('App\Logistic','logistic_user','user_id','logistic_id');
@@ -139,10 +132,6 @@ class User extends Authenticatable implements JWTSubject
         }
         return $workgroupIds;
     }
-    public function getPhoneAttribute()
-    {
-        return $this['phone']? $this['userDetail']['mobile_phone']:null;
-    }
 
     //jwt
     public function getJWTIdentifier()

+ 4 - 3
routes/api.php

@@ -13,11 +13,12 @@ use Illuminate\Support\Facades\Route;
 | is assigned the "api" middleware group. Enjoy building your API!
 |
 */
-Route::group(['prefix' => 'auth'], function ($router) {
-    Route::post('login', 'api\procurement\wechat\AuthController@login');
+Route::group(['prefix' => 'procurement','middleware'=>'procurement.auth.api'], function ($router) {
     Route::post('logout', 'api\procurement\wechat\AuthController@logout');
     Route::post('refresh', 'api\procurement\wechat\AuthController@refresh');
     Route::post('me', 'api\procurement\wechat\AuthController@me');
-
+});
+Route::group(['prefix' => 'procurement'], function ($router) {
+    Route::post('login', 'api\procurement\wechat\AuthController@login');
 });