使用myth-auth Codeigniter 4登录后重定向到上一页



我在使用mythth -auth进行日志记录的过程中遇到了奇怪的bug。所以我有一个单页用户,可以不登录访问,和一个管理页面,要求用户登录,如果他们想要访问它。但是在登录到管理页面后,它会重定向到前一个页面。所以我再次登录,它可以进入管理页面。奇怪的是,当我退出时,同样的事情又开始了。

我开始认为App/Config/Route页面或AuthController可能有问题。

public function login()
{
// No need to show a login form if the user
// is already logged in.
if ($this->auth->check()) {
$redirectURL = session('redirect_url') ?? base_url('admin');
unset($_SESSION['redirect_url']);
return redirect()->to($redirectURL);
}
// Set a return URL if none is specified
$_SESSION['redirect_url'] = session('redirect_url') ?? previous_url() ?? base_url('admin');
return view($this->config->views['login'], ['config' => $this->config]);
}
/**
* Attempts to verify the user's credentials
* through a POST request.
*/
public function attemptLogin()
{
$rules = [
'login' => 'required',
'password' => 'required',
];
if ($this->config->validFields == ['email']) {
$rules['login'] .= '|valid_email';
}
if (!$this->validate($rules)) {
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
}
$login = $this->request->getPost('login');
$password = $this->request->getPost('password');
$remember = (bool)$this->request->getPost('remember');
// Determine credential type
$type = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
// Try to log them in...
if (!$this->auth->attempt([$type => $login, 'password' => $password], $remember)) {
return redirect()->back()->withInput()->with('error', $this->auth->error() ?? lang('Auth.badAttempt'));
}
// Is the user being forced to reset their password?
if ($this->auth->user()->force_pass_reset === true) {
// return redirect()->to(route_to('reset-password') .'?token='. $this->auth->user()->reset_hash)->withCookies();
return redirect()->to(route_to('reset-password') . '?token=' . $this->auth->user()->reset_hash);
}
$redirectURL = session('redirect_url') ?? base_url('admin');
unset($_SESSION['redirect_url']);
// return redirect()->to($redirectURL)->withCookies()->with('message', lang('Auth.loginSuccess'));
return redirect()->to($redirectURL)->with('message', lang('Auth.loginSuccess'));
}

这里是路由页

// ROUTE USER
$routes->get('/', 'Page::index');
/* ROUTE PER-PAGE */
$routes->get('/admin', 'Page::admin');

我找到了bug,在用户视图中我写了下面的代码

<p>Login halaman Admin <a href="/login">Login</a></p>

但是如果我用url访问管理页面,它会重定向到index.php/login。所以我把<a href="/login">变成了<a href"index.php/login">。这是url问题

相关内容

  • 没有找到相关文章

最新更新