/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (!jwt()->check()) { return data()->sendErr(['token' => '未登录或未授权的请求。'], 401); } return $next($request); }
public static function login() { // 验证请求 $validator = Validator::make(request()->all(), ['username' => 'required', 'password' => 'required'], $messages = ['username.required' => '没有填写会员名/手机号/邮箱。', 'password.required' => '没有填写密码。']); if ($validator->fails()) { // 转化错误为数组并追加到data() data()->setErr(collect($validator->errors())->toArray()); return null; } // 认证属性 $loginAttributes = [self::loginUsername(request('username')) => request('username'), 'password' => request('password'), 'status' => 1]; // 成功认证 if (auth()->once($loginAttributes)) { return jwt()->token('uid', auth()->user()->id); } data()->setErr('username', '会员名或密码不正确。'); //错误格式前后一致 return null; }