diff --git a/src/main.v b/src/main.v index cd17153..323ba40 100644 --- a/src/main.v +++ b/src/main.v @@ -1,14 +1,19 @@ module main import veb -import databases - -pub struct Context { - veb.Context -} +import db.sqlite +import veb.auth pub struct App { veb.StaticHandler +pub mut: + db sqlite.DB + auth auth.Auth[sqlite.DB] +} + +pub struct Context { + veb.Context + user User } pub fn (app &App) index(mut ctx Context) veb.Result { @@ -16,16 +21,17 @@ pub fn (app &App) index(mut ctx Context) veb.Result { } fn main() { - mut db := databases.create_db_connection() or { panic(err) } + mut app := &App{ + db: sqlite.connect('app.db')! + } - sql db { + sql app.db { create table User create table Plane create table Ticket } or { panic('error on create table: ${err}') } - db.close() or { panic(err) } - mut app := &App{} + app.auth = auth.new(app.db) app.handle_static('static', false)! diff --git a/src/src b/src/src deleted file mode 100755 index b5c8231..0000000 Binary files a/src/src and /dev/null differ diff --git a/src/templates/signup.html b/src/templates/signup.html index 5a0fe18..3a02d3d 100644 --- a/src/templates/signup.html +++ b/src/templates/signup.html @@ -133,6 +133,7 @@ display: none; } +
@@ -142,7 +143,12 @@
-
+
diff --git a/src/templates/signup_form.html b/src/templates/signup_form.html new file mode 100644 index 0000000..9156368 --- /dev/null +++ b/src/templates/signup_form.html @@ -0,0 +1,132 @@ + +
+
+ + +
+ 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 2118223..89e7497 100644 --- a/src/user_controller.v +++ b/src/user_controller.v @@ -1,8 +1,6 @@ module main import veb -import encoding.base64 -import json @['/controller/user/create'; post] pub fn (mut app App) controller_create_user(mut ctx Context) veb.Result { @@ -15,9 +13,9 @@ pub fn (mut app App) controller_create_user(mut ctx Context) veb.Result { // Create a map of field names and their values fields := { 'first_name': first_name - 'last_name': last_name - 'email': email - 'password': password + 'last_name': last_name + 'email': email + 'password': password // Gender can be optional, so not including it in validation } @@ -31,7 +29,7 @@ pub fn (mut app App) controller_create_user(mut ctx Context) veb.Result { // If any fields are empty, return error message if empty_fields.len > 0 { - return ctx.text('The following fields cannot be empty: ${empty_fields.join(", ")}') + return ctx.text('The following fields cannot be empty: ${empty_fields.join(', ')}') } app.service_add_user(first_name, last_name, email, password, gender) or { diff --git a/src/user_entities.v b/src/user_entities.v index e16b737..20c4dfc 100644 --- a/src/user_entities.v +++ b/src/user_entities.v @@ -10,4 +10,5 @@ mut: password string @[sql_type: 'TEXT'] gender string @[sql_type: 'TEXT'] tickets []Ticket @[fkey: 'user_id'] + salt string } diff --git a/src/user_service.v b/src/user_service.v index f5094b8..e41504b 100644 --- a/src/user_service.v +++ b/src/user_service.v @@ -1,30 +1,21 @@ module main -import crypto.bcrypt -import databases +import veb.auth fn (app &App) service_add_user(first_name string, last_name string, email string, password string, gender string) ! { - mut db := databases.create_db_connection()! - - defer { - db.close() or { panic(err) } - } - - hashed_password := bcrypt.generate_from_password(password.bytes(), bcrypt.min_cost) or { - eprintln(err) - return err - } + salt := auth.generate_salt() user_model := User{ first_name: first_name last_name: last_name email: email - password: hashed_password + password: auth.hash_password_with_salt(password, salt) gender: gender + salt: salt } mut insert_error := '' - sql db { + sql app.db { insert user_model into User } or { insert_error = err.msg() } if insert_error != '' { diff --git a/src/user_view.v b/src/user_view.v new file mode 100644 index 0000000..d2496bb --- /dev/null +++ b/src/user_view.v @@ -0,0 +1,7 @@ +module main + +import veb + +pub fn (app &App) signup(mut ctx Context) veb.Result { + return $veb.html() +}