diff --git a/src/templates/first_name.html b/src/templates/first_name.html deleted file mode 100644 index e69de29..0000000 diff --git a/src/templates/login.html b/src/templates/login.html new file mode 100644 index 0000000..e286ad3 --- /dev/null +++ b/src/templates/login.html @@ -0,0 +1,219 @@ + + + + + + User Login + @css '/static/css/bootstrap.css' + + @js 'https://unpkg.com/htmx.org@2.0.4' + + +
+
+

User Login

+

Sign in to your account to book flights

+
+ +
+
+ +
+ + + + +
+ + +
+ Please enter a valid email address +
+
+ +
+ + +
+ Please enter your password +
+
+ +
+ +
+ +
+

Don't have an account? Sign up now

+
+
+
+
+ + @js '/static/js/bootstrap.js' + \ No newline at end of file diff --git a/src/templates/signup_form.html b/src/templates/signup_form.html deleted file mode 100644 index 195ae87..0000000 --- a/src/templates/signup_form.html +++ /dev/null @@ -1,202 +0,0 @@ -
- -
- - - - -
-
- - -
- Please enter a valid first name -
-
-
- - -
- Please enter a valid last name -
-
-
- -
- - -
- Please enter a valid email address -
-
- -
- - -
- Password must be at least 8 characters long -
-
- -
- -
- -
-
- - -
-
- - -
-
- - -
-
-
-
- - - -
- -
-
- - diff --git a/src/user_controller.v b/src/user_controller.v index 2e9d852..5cd399e 100644 --- a/src/user_controller.v +++ b/src/user_controller.v @@ -24,20 +24,21 @@ pub fn (mut app App) controller_create_user(mut ctx Context, first_name string, // If any fields are empty, return field-specific error messages if empty_fields.len > 0 { mut response := '' response += '
Please fill in all required fields
' - + return ctx.html(response) } @@ -46,13 +47,74 @@ pub fn (mut app App) controller_create_user(mut ctx Context, first_name string, error_html := '
Error: ${err}
' return ctx.html(error_html) } - + // Return success message with HTML success_html := '

User created successfully!

Welcome, ${first_name}! Your account has been created.

Login Now
' - + return ctx.html(success_html) } + +@['/controller/user'; post] +pub fn (app &App) controller_get_user(mut ctx Context, email string, password string) veb.Result { + // Create a map of field names and their values + fields := { + 'email': email + 'password': password + } + + // Check for empty fields + mut empty_fields := []string{} + for field_name, value in fields { + if value == '' { + empty_fields << field_name + } + } + + // If any fields are empty, return field-specific error messages + if empty_fields.len > 0 { + mut response := '' + response += '
Please fill in all required fields
' + + return ctx.html(response) + } + + // Try to find the user + user := app.service_find_user_by_email(email, password) or { + error_html := '
Invalid email or password
' + return ctx.html(error_html) + } + + // Return success message with HTML and redirect + success_html := '
+

Login successful!

+

Welcome back, ${user.first_name}!

+
+ ' + + return ctx.html(success_html) +} + +@['/controller/user/update'; post] +pub fn (app &App) controller_update_user(mut ctx Context, first_name string, last_name string, password string) veb.Result { +} diff --git a/src/user_service.v b/src/user_service.v index e41504b..f2775ea 100644 --- a/src/user_service.v +++ b/src/user_service.v @@ -22,3 +22,16 @@ fn (app &App) service_add_user(first_name string, last_name string, email string return error(insert_error) } } + +fn (app &App) service_find_user_by_email(email string, password string) !User { + mut user := sql app.db { + select from User where email == email + }! + if user[0].id == 0 { + return error('User not found') + } + if !auth.compare_password_with_hash(password, user[0].salt, user[0].password) { + return error('Invalid password') + } + return user[0] +} diff --git a/src/user_view.v b/src/user_view.v index d2496bb..63b95aa 100644 --- a/src/user_view.v +++ b/src/user_view.v @@ -5,3 +5,7 @@ import veb pub fn (app &App) signup(mut ctx Context) veb.Result { return $veb.html() } + +pub fn (app &App) login(mut ctx Context) veb.Result { + return $veb.html() +}