首页 > 编程学习 > laravel5.5 jwt使用

laravel5.5 jwt使用

发布时间:2022/11/24 23:18:04

BaseController

<?phpnamespace App\Http\Controllers\v1;use App\Http\Controllers\Controller;
use Dingo\Api\Routing\Helpers;class BaseController extends Controller
{use Helpers;}

AuthController

<?phpnamespace App\Http\Controllers\V1;use App\User;
use Dingo\Blueprint\Annotation\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Validator;class AuthController extends BaseController
{protected $guard = 'api';//设置使用guard为api选项验证,请查看config/auth.php的guards设置项,重要!public function __construct(){$this->middleware('refresh', ['except' => ['login','register']]);}public function test(){echo "test!!";}public function register(Request $request){$rules = ['name' => ['required'],'email' => ['required'],'password' => ['required', 'min:6', 'max:16'],];$payload = $request->only('name', 'email', 'password');$validator = Validator::make($payload, $rules);// 验证格式if ($validator->fails()) {return $this->response->array(['error' => $validator->errors()]);}// 创建用户$result = User::create(['name' => $payload['name'],'email' => $payload['email'],'password' => bcrypt($payload['password']),]);if ($result) {return $this->response->array(['success' => '创建用户成功']);} else {return $this->response->array(['error' => '创建用户失败']);}}/*** Get a JWT token via given credentials.** @param  \Illuminate\Http\Request  $request** @return \Illuminate\Http\JsonResponse*/public function login(Request $request){$credentials = $request->only('email', 'password');if ($token = $this->guard()->attempt($credentials)) {return $this->respondWithToken($token);}return $this->response->errorUnauthorized('登录失败');}/*** Get the authenticated User** @return \Illuminate\Http\JsonResponse*/public function me(){//return response()->json($this->guard()->user());return $this->response->array($this->guard()->user());}/*** Log the user out (Invalidate the token)** @return \Illuminate\Http\JsonResponse*/public function logout(){$this->guard()->logout();//return response()->json(['message' => 'Successfully logged out']);return $this->response->array(['message' => '退出成功']);}/*** Refresh a token.** @return \Illuminate\Http\JsonResponse*/public function refresh(){return $this->respondWithToken($this->guard()->refresh());}/*** Get the token array structure.** @param  string $token** @return \Illuminate\Http\JsonResponse*/protected function respondWithToken($token){return response()->json(['access_token' => $token,'token_type' => 'bearer','expires_in' => $this->guard()->factory()->getTTL() * 60]);}/*** Get the guard to be used during authentication.** @return \Illuminate\Contracts\Auth\Guard*/public function guard(){return Auth::guard($this->guard);}
}

routes/api.php


// 需要用户认证
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', ['namespace' => 'App\Http\Controllers\V1'], function ($api) {// 授权组$api->group(['prefix' => 'au'], function ($api) {$api->post('register', 'AuthController@register');$api->post('login', 'AuthController@login');$api->post('logout', 'AuthController@logout');$api->post('refresh', 'AuthController@refresh');$api->post('me', 'AuthController@me');$api->get('test', 'AuthController@test');});
});

 


本文链接:https://www.ngui.cc/el/2111861.html
Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000