update:优化auth认证
This commit is contained in:
parent
91a1d253c5
commit
4506219fb2
@ -22,17 +22,79 @@ class Index extends CatchController
|
||||
*/
|
||||
public function login(LoginRequest $request, CatchAuth $auth)
|
||||
{
|
||||
$condition = $request->param();
|
||||
|
||||
try {
|
||||
$token = $auth->attempt($condition);
|
||||
|
||||
$user = $auth->user();
|
||||
|
||||
$this->afterLoginSuccess($user);
|
||||
// 登录事件
|
||||
$this->loginEvent($user->username);
|
||||
|
||||
return CatchResponse::success([
|
||||
'token' => $auth->attempt($request->param()),
|
||||
'token' => $token,
|
||||
], '登录成功');
|
||||
} catch (\Exception $exception) {
|
||||
$this->detailWithLoginFailed($exception, $condition);
|
||||
$code = $exception->getCode();
|
||||
return CatchResponse::fail($code == Code::USER_FORBIDDEN ?
|
||||
'该账户已被禁用,请联系管理员' : '登录失败,请检查邮箱和密码', Code::LOGIN_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理登录失败
|
||||
*
|
||||
* @time 2020年10月26日
|
||||
* @param $exception
|
||||
* @param $condition
|
||||
* @return void
|
||||
*/
|
||||
protected function detailWithLoginFailed($exception, $condition)
|
||||
{
|
||||
$message = $exception->getMessage();
|
||||
|
||||
if (strpos($message, '|') !== false) {
|
||||
$username = explode('|', $message)[1];
|
||||
} else {
|
||||
$username = $condition['email'];
|
||||
}
|
||||
|
||||
$this->loginEvent($username, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录成功后
|
||||
*
|
||||
* @time 2020年09月09日
|
||||
* @param $user
|
||||
* @return void
|
||||
*/
|
||||
protected function afterLoginSuccess($user)
|
||||
{
|
||||
$user->last_login_ip = request()->ip();
|
||||
$user->last_login_time = time();
|
||||
$user->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录事件
|
||||
*
|
||||
* @time 2020年09月09日
|
||||
* @param $name
|
||||
* @param bool $success
|
||||
* @return void
|
||||
*/
|
||||
protected function loginEvent($name, $success = true)
|
||||
{
|
||||
$params['login_name'] = $name;
|
||||
$params['success'] = $success ? 1 : 2;
|
||||
event('loginLog', $params);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 登出
|
||||
*
|
||||
|
@ -53,7 +53,6 @@ class CatchAuth
|
||||
{
|
||||
try {
|
||||
$user = $this->authenticate($condition);
|
||||
|
||||
if (!$user) {
|
||||
throw new LoginFailedException();
|
||||
}
|
||||
@ -65,51 +64,13 @@ class CatchAuth
|
||||
throw new LoginFailedException('登录失败|' . $user->username);
|
||||
}
|
||||
|
||||
$token = $this->{$this->getDriver()}($user);
|
||||
$this->afterLoginSuccess($user);
|
||||
// 登录事件
|
||||
$this->loginEvent($user->username);
|
||||
return $token;
|
||||
return $this->{$this->getDriver()}($user);
|
||||
|
||||
} catch (\Exception $exception) {
|
||||
$message = $exception->getMessage();
|
||||
if (strpos($message, '|') !== false) {
|
||||
$username = explode('|', $message)[1];
|
||||
} else {
|
||||
$username = $condition['email'];
|
||||
}
|
||||
$this->loginEvent($username, false);
|
||||
throw new LoginFailedException('登录失败', $exception->getCode());
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录成功后
|
||||
*
|
||||
* @time 2020年09月09日
|
||||
* @param $user
|
||||
* @return void
|
||||
*/
|
||||
protected function afterLoginSuccess($user)
|
||||
{
|
||||
$user->last_login_ip = request()->ip();
|
||||
$user->last_login_time = time();
|
||||
$user->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录事件
|
||||
*
|
||||
* @time 2020年09月09日
|
||||
* @param $name
|
||||
* @param bool $success
|
||||
* @return void
|
||||
*/
|
||||
protected function loginEvent($name, $success = true)
|
||||
{
|
||||
$params['login_name'] = $name;
|
||||
$params['success'] = $success ? 1 : 2;
|
||||
event('loginLog', $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* user
|
||||
|
Loading…
x
Reference in New Issue
Block a user