我正在努力学习Cro(和Perl6同时);)
我的学习应用程序基于Cro的文档.我添加了一些可行的身份验证,但用户会话立即被遗忘.
您可以在https://gitlab.com/ecocode/beaverapp查看代码
转到登录pasge并使用“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; } }
我认为问题是由于中间件会话管理不起作用.我该怎么纠正这个?或者问题可能是由于其他原因造成的?