我正在尝试学习 Cro (和 Perl6 同时) ;)
我的学习应用基于 Cro 的文档。我添加了一些有效的身份验证,但用户会话立即被遗忘。
您可以在https://gitlab.com/ecocode/beaverapp查看代码, 进入登录页面并使用“user”和“pwd”登录。您被重新路由到 / (表示登录成功),但那里的消息是“当前用户:-”。所以会话丢失了。
Routes.pm6 的相关部分:
class UserSession does Cro::HTTP::Auth {
has $.username is rw;
method logged-in() {
defined $!username;
}
}
my $routes = route {
subset LoggedIn of UserSession where *.logged-in;
get -> UserSession $s {
content 'text/html', "Current user: {$s.logged-in ?? $s.username !! '-'}";
}
get -> LoggedIn $user, 'users-only' {
content 'text/html', "Secret page just for *YOU*, $user.username()";
}
get -> 'login' {
content 'text/html', q:to/HTML/;
<form method="POST" action="/login">
<div>
Username: <input type="text" name="username" />
</div>
<div>
Password: <input type="password" name="password" />
</div>
<input type="submit" value="Log In" />
</form>
HTML
}
post -> UserSession $user, 'login' {
request-body -> (:$username, :$password, *%) {
if valid-user-pass($username, $password) {
$user.username = $username;
redirect '/', :see-other;
}
else {
content 'text/html', "Bad username/password";
}
}
}
sub valid-user-pass($username, $password) {
# Call a database or similar here
return $username eq 'user' && $password eq 'pwd';
}
}
sub routes(Beaverapp $beaverapp) is export {
route {
# Apply middleware, then delegate to the routes.
before Cro::HTTP::Session::InMemory[UserSession].new;
delegate <*> => $routes;
}
}
我认为问题是由于中间件会话管理不起作用。我应该如何纠正这个?或者问题可能是由于其他原因?