This commit is contained in:
cereci5049 2024-08-18 17:10:06 +05:30
parent 8aad9db65f
commit dbe4e2a6d6
6 changed files with 29 additions and 30 deletions

View file

@ -10,13 +10,13 @@ proc htmxLogin*(ctx: Context) =
var
form = ctx.urlForm
em =form["em"]
ph =form["ph"]
ps=form["pw"]
val: Validity
validity = initTable[string, Validity]()
if em.len==0 or ps.len==0:
if ph.len==0 or ps.len==0:
for a, b in form:
if form[a] == "":
val.name = ""
@ -31,7 +31,7 @@ proc htmxLogin*(ctx: Context) =
ctx.send(htmxLogin(validity, ""))
let conn=newTurso()
var temp=getUser(conn, em, ps)
var temp=getUser(conn, ph, ps)
if temp[0] == true:
ctx &= initCookie("user1", $(%* temp[1]))
echo $(%* temp[1])
@ -43,4 +43,4 @@ proc htmxLogin*(ctx: Context) =
val.message = ""
val.mark = ""
validity[a] = val
ctx.send(htmxLogin(validity, "Invalid Email or Password"))
ctx.send(htmxLogin(validity, "Invalid Email or Password"))

View file

@ -17,18 +17,19 @@ proc signup*(ctx: Context): string=
form = ctx.urlForm
firstName = form["fn"]
lastName = form["ln"]
email = form["em"]
phone = form["ph"]
password = form["pw"]
userType = form["ut"]
conn = newTurso()
val: Validity
validity = initTable[string, Validity]()
echo firstName, lastName, email, password
echo firstName, lastName, password, userType, phone
if firstName.len == 0 or lastName.len == 0 or email.len == 0 or phone.len == 0 or password.len == 0:
if firstName.len == 0 or lastName.len == 0 or phone.len == 0 or password.len == 0:
echo "lllo"
for a, b in form:
if form[a] == "":
val.name = ""
@ -45,7 +46,6 @@ proc signup*(ctx: Context): string=
var user = User(
firstName: firstName,
lastName: lastName,
email: email,
phone: phone,
password: password,
accessLevel: 1

View file

@ -12,7 +12,7 @@ proc setupUsers*(conn: Turso) =
id INTEGER PRIMARY KEY AUTOINCREMENT,
firstName VARCHAR(255) NOT NULL,
lastName VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
email VARCHAR(255),
phone VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
createdAt TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,
@ -23,13 +23,14 @@ proc setupUsers*(conn: Turso) =
proc createPost*(conn: Turso, user: User)=
## createPost inserts a new user into the Users table
conn.execute(fmt"""INSERT INTO users (firstName, lastName, email, phone, password, accessLevel) VALUES ("{user.firstName}", "{user.lastName}", "{user.email}", "{user.phone}", "{user.password}", {user.accessLevel});""")
echo "lllo1"
conn.execute(fmt"""INSERT INTO Users (firstName, lastName, phone, password, accessLevel) VALUES ("{user.firstName}", "{user.lastName}", "{user.phone}", "{user.password}", {user.accessLevel});""")
proc getUser*(conn: Turso, email: string, password: string): (bool, User)=
proc getUser*(conn: Turso, phone: string, password: string): (bool, User)=
## getUser retrieves a user from the Users table
echo "h1"
echo phone, password
var
data = conn.getData(fmt"""SELECT * FROM users WHERE email = "{email}" and password = "{password}";""")
data = conn.getData(fmt"""SELECT * FROM Users WHERE phone = "{phone}" and password = "{password}";""")
user = User(
firstName: "",
lastName: "",
@ -40,16 +41,13 @@ proc getUser*(conn: Turso, email: string, password: string): (bool, User)=
)
if data["rows"].len > 0:
echo "h2"
user = User(
firstName: data["rows"][0][1]["value"].getStr(),
lastName: data["rows"][0][2]["value"].getStr(),
email: data["rows"][0][3]["value"].getStr(),
phone: data["rows"][0][4]["value"].getStr(),
password: data["rows"][0][5]["value"].getStr(),
accessLevel: parseInt(data["rows"][0][8]["value"].getStr())
)
result = (true, user)
else:
echo "h3"
result = (false, user)
result = (false, user)

View file

@ -89,15 +89,6 @@ proc htmxSignup*(val: Table[string, Validity], message: string): string =
<input type="text" name="ln" class="form-control {val["ln"].mark}" required autocomplete="off" value="{val["ln"].name}">
</div>
<div class="form-group">
<label for="email">Email:</label>
<label class="text-danger">{val["em"].message}</label>
<input type="email" name="em" class="form-control {val["em"].mark}" required autocomplete="off" value="{val["em"].name}">
</div>
<div class="form-group">
<label for="email">Phone:</label>
<label class="text-danger">{val["ph"].message}</label>

View file

@ -19,11 +19,13 @@
hx-target="this"
hx-swap="outerHTML"
>
<label for="email">Email:</label>
<label for="email">Phone:</label>
<label class="text-danger"></label>
<input type="email" name="em" id="email" class="form-control" required autocomplete="off"
<input type="email" name="ph" class="form-control" required autocomplete="off"
value=""
hx-post="/htmx/email"
hx-post="/htmx/phone"
>
</div>
@ -50,4 +52,4 @@
</div>
</div>
{% endblock %}
{% endblock %}

View file

@ -80,6 +80,14 @@
</div>
<div class="form-group">
<label for="user_type" class="text-black">User Type:</label>
<select id="user_type" class="form-control" name="ut">
<option value="1">User</option>
<option value="2">Seller</option>
</select>
</div>
<hr>
<button id="b4" type="submit" class="btn btn-primary">Submit</button>