From 2ea4a0957fb715b45ce9894ba759b675acb408db Mon Sep 17 00:00:00 2001 From: fitel17112 Date: Wed, 12 Mar 2025 22:10:59 +0530 Subject: [PATCH] 0011 --- src/templates/footer.html | 24 +++++++ src/templates/header.html | 35 ++++++++++ src/templates/login.html | 42 +++++++----- src/templates/profile.html | 85 ++++++++++++++++++++++++ src/templates/signup.html | 41 ++++++++---- src/templates/user.html | 131 +++++++++++++++++++++++++++++++++++++ src/user_controller.v | 4 +- src/user_service.v | 34 ++++++++-- src/user_view.v | 27 ++++++++ 9 files changed, 386 insertions(+), 37 deletions(-) create mode 100644 src/templates/footer.html create mode 100644 src/templates/header.html create mode 100644 src/templates/profile.html create mode 100644 src/templates/user.html diff --git a/src/templates/footer.html b/src/templates/footer.html new file mode 100644 index 0000000..565225b --- /dev/null +++ b/src/templates/footer.html @@ -0,0 +1,24 @@ + + + @js '/static/js/bootstrap.js' + + \ No newline at end of file diff --git a/src/templates/header.html b/src/templates/header.html new file mode 100644 index 0000000..4a408b3 --- /dev/null +++ b/src/templates/header.html @@ -0,0 +1,35 @@ + + + + + + Air Bookings + @css '/static/css/bootstrap.css' + + + + +
\ No newline at end of file diff --git a/src/templates/login.html b/src/templates/login.html index e286ad3..5e27631 100644 --- a/src/templates/login.html +++ b/src/templates/login.html @@ -101,7 +101,7 @@ margin-top: 5px; display: none; } - + /* Field error styling */ .field-error { color: #e74c3c; @@ -109,37 +109,37 @@ font-weight: normal; margin-left: 5px; } - + /* Style for input fields with errors */ .input-error { border: 1px solid #e74c3c !important; background-color: #fff8f8; } - + /* Spinner styling */ .spinner { text-align: center; padding: 10px; color: #666; } - + .htmx-request .htmx-indicator { display: block !important; } - + /* Alert styling */ .alert { padding: 15px; margin-bottom: 20px; border-radius: 4px; } - + .alert-danger { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; } - + .alert-success { background-color: #d4edda; color: #155724; @@ -177,7 +177,10 @@
- +
- +
- +
- -
-

Don't have an account? Sign up now

+ +
+

+ Don't have an account? + Sign up now +

@js '/static/js/bootstrap.js' - \ No newline at end of file + diff --git a/src/templates/profile.html b/src/templates/profile.html new file mode 100644 index 0000000..2e757ba --- /dev/null +++ b/src/templates/profile.html @@ -0,0 +1,85 @@ +@include 'header.html' + +
+
+
+
+
+

My Profile

+
+
+
+

@{user.first_name} @{user.last_name}

+

@{user.email}

+

@{user.gender}

+
+ +
+
+
Update Profile
+
+
+
+ + +
+ + +
+
+ +
+ + +
+
+ +
+ + +
+
+ +
+ +
+
+
+
+ +
+
+
Account Actions
+
+
+
+ Logout +
+
+
+
+
+
+
+
+ +
+ + + +@include 'footer.html' \ No newline at end of file diff --git a/src/templates/signup.html b/src/templates/signup.html index cb67740..ab05db4 100644 --- a/src/templates/signup.html +++ b/src/templates/signup.html @@ -133,7 +133,7 @@ margin-top: 5px; display: none; } - + /* Field error styling */ .field-error { color: #e74c3c; @@ -141,37 +141,37 @@ font-weight: normal; margin-left: 5px; } - + /* Style for input fields with errors */ .input-error { border: 1px solid #e74c3c !important; background-color: #fff8f8; } - + /* Spinner styling */ .spinner { text-align: center; padding: 10px; color: #666; } - + .htmx-request .htmx-indicator { display: block !important; } - + /* Alert styling */ .alert { padding: 15px; margin-bottom: 20px; border-radius: 4px; } - + .alert-danger { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; } - + .alert-success { background-color: #d4edda; color: #155724; @@ -210,7 +210,12 @@
- +
- +
- +
- +
-->
- +
+
+
+
+
+

User Profile

+
+
+
+

@{user.first_name} @{user.last_name}

+

@{user.email}

+

@{user.gender}

+
+ +
+
+
Update Profile
+
+
+
+ + +
+ + +
+
+ +
+ + +
+
+ +
+ + +
+
+ +
+ +
+
+
+
+ +
+
+
Account Actions
+
+
+
+ Logout +
+
+
+
+
+
+
+
+ +
+ + + +@include 'footer.html' diff --git a/src/user_controller.v b/src/user_controller.v index 80a85d8..83f5671 100644 --- a/src/user_controller.v +++ b/src/user_controller.v @@ -52,7 +52,7 @@ pub fn (mut app App) controller_create_user(mut ctx Context, first_name string, // Generate and insert the token using user ID token := app.auth.add_token(x.id) or { '' } // Authenticate the user by adding the token to the cookies - ctx.set_cookie(name: 'token', value: token) + ctx.set_cookie(name: 'token', value: token, path: '/') } // Return success message with HTML @@ -110,7 +110,7 @@ pub fn (mut app App) controller_get_user(mut ctx Context, email string, password token := app.auth.add_token(user.id) or { '' } // Authenticate the user by adding the token to the cookies - ctx.set_cookie(name: 'token', value: token) + ctx.set_cookie(name: 'token', value: token, path: '/') // Return success message with HTML and redirect success_html := '
diff --git a/src/user_service.v b/src/user_service.v index 2a72506..6d0208d 100644 --- a/src/user_service.v +++ b/src/user_service.v @@ -41,35 +41,55 @@ fn (app &App) service_update_user(id ?string, first_name string, last_name strin return error('User ID is required') } + // Unwrap the id value + user_id := id or { return error('Invalid ID') } + // Get current user data current_user := sql app.db { - select from User where id == id limit 1 + select from User where id == user_id limit 1 }! if current_user.len == 0 { return error('User not found') } - mut updates := []string{} - // Check which fields have changed if first_name != current_user[0].first_name { sql app.db { - update User set first_name = first_name where id == id + update User set first_name = first_name where id == user_id }! } if last_name != current_user[0].last_name { sql app.db { - update User set last_name = last_name where id == id + update User set last_name = last_name where id == user_id }! } - if password != '' { // Only update password if a new one is provided + if !auth.compare_password_with_hash(password, current_user[0].salt, current_user[0].password) { salt := auth.generate_salt() hashed_password := auth.hash_password_with_salt(password, salt) sql app.db { - update User set password = hashed_password, salt = salt where id == id + update User set password = hashed_password, salt = salt where id == user_id }! } return } + +fn (app &App) service_get_user(id int) !User { + if id == 0 { + return error('User ID is required') + } + + user_id := id + + // Get user data + user := sql app.db { + select from User where id == user_id limit 1 + }! + + if user.len == 0 { + return error('User not found') + } + + return user[0] +} diff --git a/src/user_view.v b/src/user_view.v index 63b95aa..4cd251f 100644 --- a/src/user_view.v +++ b/src/user_view.v @@ -9,3 +9,30 @@ pub fn (app &App) signup(mut ctx Context) veb.Result { pub fn (app &App) login(mut ctx Context) veb.Result { return $veb.html() } + +pub fn (app &App) user(mut ctx Context) veb.Result { + user_id := ctx.get_cookie('token') or { '' } + token := app.auth.find_token(user_id) or { return ctx.redirect('/login') } + + if token.user_id == 0 { + // Redirect to login if not logged in + return ctx.redirect('/login') + } + + user := app.service_get_user(token.user_id) or { return ctx.redirect('/login') } + + return $veb.html() +} + +pub fn (app &App) profile(mut ctx Context) veb.Result { + user_id := ctx.get_cookie('token') or { '' } + token := app.auth.find_token(user_id) or { return ctx.redirect('/login') } + + if token.user_id == 0 { + return ctx.redirect('/login') + } + + user := app.service_get_user(token.user_id) or { return ctx.redirect('/login') } + + return $veb.html() +}