11package BlogDB::Web::Controller::Root ;
22use Mojo::Base ' Mojolicious::Controller' , -signatures;
3+ use Try::Tiny;
34
45sub get_register ($c ) {
56 $c -> set_template( ' register' );
@@ -9,6 +10,55 @@ sub get_register ($c) {
910sub post_register ($c ) {
1011 $c -> set_template( ' register' );
1112
13+ my $username = $c -> stash-> {form_username } = $c -> param(' username' );
14+ my $email = $c -> stash-> {form_email } = $c -> param(' email' );
15+ my $password = $c -> stash-> {form_password } = $c -> param(' password' );
16+ my $confirm = $c -> stash-> {form_confirm } = $c -> param(' confirm' );
17+
18+ # Error Checking - We have all of the information.
19+ push @{$c -> stash-> {errors }}, " Username is required." unless $username ;
20+ push @{$c -> stash-> {errors }}, " Email address is required." unless $email ;
21+ push @{$c -> stash-> {errors }}, " Password is required." unless $password ;
22+ push @{$c -> stash-> {errors }}, " Confirm password is required." unless $password ;
23+
24+ # Error Checking - Username conforms to expectations:
25+ push @{$c -> stash-> {errors }}, " Usernames must start with a letter and then may contain letters, numbers, underscores and dashes."
26+ unless $username =~ / ^[a-zA-Z][a-zA-Z0-9_-]+$ / ;
27+
28+ return 0 if $c -> stash-> {errors }; # Drop out of processing the registration if there are any errors.
29+
30+ # Error Checking - No user exists with this username or email address, password is valid.
31+ my $is_user_exist = $c -> db-> resultset(' Person' )-> find( { username => $username } );
32+ my $is_email_exist = $c -> db-> resultset(' Person' )-> find( { email => $email } );
33+
34+ push @{$c -> stash-> {errors }}, " Password & Confirmation must match." unless $password eq $confirm ;
35+ push @{$c -> stash-> {errors }}, " Password must be at least 7 chars." unless 7 < length ($password );
36+ push @{$c -> stash-> {errors }}, " This username is already in use." unless not $is_user_exist ;
37+ push @{$c -> stash-> {errors }}, " This email address is already registered." unless not $is_email_exist ;
38+
39+ return 0 if $c -> stash-> {errors }; # Drop out of processing the registration if there are any errors.
40+
41+ # Alright, we are clear to create the account now.
42+
43+ my $person = try {
44+ $c -> db-> storage-> schema-> txn_do( sub {
45+ my $person = $c -> db-> resultset(' Person' )-> create({
46+ email => $email ,
47+ username => $username ,
48+ });
49+ $person -> new_related( ' auth_password' , {} )-> set_password($password );
50+ return $person ;
51+ });
52+ } catch {
53+ push @{$c -> stash-> {errors }}, " The account could not be created: $_ " ;
54+ };
55+
56+ return 0 if $c -> stash-> {errors }; # Drop out of processing the registration if there are any errors.
57+
58+ # We have created a user account for this person
59+ $c -> session-> {uid } = $person -> id;
60+
61+ $c -> redirect_to( $c -> url_for( ' homepage' ) );
1262}
1363
1464sub get_forgot ($c ) {
@@ -31,11 +81,34 @@ sub post_reset ($c) {
3181
3282}
3383
34- sub post_login ($c ) {
84+ sub post_reset ($c ) {
85+ $c -> set_template( ' login' );
86+
87+ }
3588
89+ sub post_login ($c ) {
90+ my $username = $c -> stash-> {form_username } = $c -> param(' username' );
91+ my $password = $c -> stash-> {form_password } = $c -> param(' password' );
92+ my $return = $c -> stash-> {form_return } = $c -> param(' return_url' );
93+
94+ # Find the user -- if they have an @, assume it's an email addresss.
95+ my $person = $c -> db-> resultset(' Person' )-> find( index ($username , ' @' ) == -1
96+ ? { username => $username }
97+ : { email => $username }
98+ );
99+
100+ # Check that we have a user account and the password matches, otherwise redirect without login.
101+ $c -> redirect_to( $return ) unless $person ;
102+ $c -> redirect_to( $return ) unless $person -> auth_password-> check_password( $password );
103+
104+ # The user checks out, log them in.
105+ $c -> session-> {uid } = $person -> id; # Set the user cookie for the login.
106+ $c -> redirect_to( $return ); # Send them to the page they were on with a refresh.
36107}
37108
38109sub post_logout ($c ) {
110+ delete $c -> session-> {uid };
111+ $c -> redirect_to( $c -> url_for( ' homepage' ));
39112
40113}
41114
0 commit comments