サインアップとログイン

サインアップとログイン処理を行うために書いたコード。

sub sign_up :Local {
    my ( $self, $c ) = @_;
    if ($c->request->method ne 'POST') {
        return;
    }

    my $params = $c->req->params;
    if($c->model("db::users")->search({username => $params->{username}}) == 0){
        my $digest = sha1_hex($params->{password});
        $c->model("db::users")->create({id => 0, username => $params->{username}, password => $digest});
        $c->res->redirect("/register/completed");
    }
}


sub login :Local {
    my ( $self, $c ) = @_;
    if ($c->request->method ne 'POST') {
        return;
    }
    my $params = $c->req->params;
    my $digest = sha1_hex($params->{password});
    warn "$params->{username}: $params->{password}";
    if($c->authenticate({username => $params->{username}, password => $params->{password}})){
        warn "bbbb";
        $c->response->redirect("http://united.jp/");
    } else {
        $c->stash(error_msg => 'User name or password is incorrect.');
        warn "aaaaaa";
    }
}