Merge pull request 'ayushman' (#15) from ayushman into main

Reviewed-on: #15
This commit is contained in:
cereci5049 2024-08-07 23:58:03 +02:00
commit 454f631f1f
5 changed files with 62 additions and 23 deletions

Binary file not shown.

View file

@ -24,18 +24,27 @@ proc createPost*(conn: Turso, user: User)=
conn.execute(fmt"""INSERT INTO users (first_name, last_name, email, phone, password, access_level) VALUES ("{user.firstName}", "{user.lastName}", "{user.email}", "{user.phone}", "{user.password}", {user.accessLevel});""")
proc getUser*(conn: Turso, email: string, password: string): (bool, User)=
echo "h1"
var
data = conn.getData(fmt"""SELECT * FROM users WHERE email = "{email}" and password = "{password}";""")
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())
firstName: "",
lastName: "",
email: "",
phone: "",
password: "",
accessLevel: 0
)
if user.firstName.len == 0 or user.lastName.len == 0 or user.email.len == 0 or user.password.len == 0:
result = (false, user)
if data["rows"].len > 0:
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:
result = (true, user)
result = (false, user)

View file

@ -6,7 +6,7 @@ import
strformat,
./lib/[mics, htmx],
./db/users,
./model/model,
./model/[model, rand],
./controller/signup
"/^path" -> before[post, get]:
@ -43,19 +43,41 @@ import
"/login" -> get:
compileTemplateFile(getScriptDir() / "view" / "login.nimja")
"/htmx/login" -> post:
var em =ctx.urlForm["email"]
var ps=ctx.urlForm["password"]
var
form = ctx.urlForm
em =form["email"]
ps=form["password"]
val: Validity
validity = initTable[string, Validity]()
if em.len==0 or ps.len==0:
ctx.send(sendLogin(em, ps))
for a, b in form:
if form[a] == "":
val.name = ""
val.message = "Field is Required"
val.mark = "is-invalid"
validity[a] = val
else:
val.name = b
val.message = ""
val.mark = ""
validity[a] = val
ctx.send(sendLogin(validity, ""))
let conn=newTurso()
var temp=getUser(conn, em, ps)
if temp[0]:
if temp[0] == true:
ctx &= initCookie("user", $(%* temp[1]))
ctx.response.headers.add("HX-Redirect", "/")
else:
echo "user not found"
for a, b in form:
val.name = b
val.message = ""
val.mark = ""
validity[a] = val
ctx.send(sendLogin(validity, "Invalid Email or Password"))
"/:path" -> get:
compileTemplateFile(getScriptDir() / "view" / "404.nimja")

View file

@ -1,6 +1,9 @@
import strformat
import
strformat,
tables,
../model/rand
proc sendLogin*(em, pw: string): string =
proc sendLogin*(val: Table[string, Validity], message: string): string =
result = fmt"""
<form id="input_form" class="" novalidate
hx-trigger="submit"
@ -8,17 +11,17 @@ proc sendLogin*(em, pw: string): string =
hx-target="#input_form"
>
<label class="text-danger"></label>
<label class="text-danger">{message}</label>
<div class="form-group mt-5">
<label for="email">Email:</label>
<label class="text-danger">Email is Required</label>
<input type="email" name="email" id="email" class="form-control" required autocomplete="off" value="{em}">
<label class="text-danger">{val["email"].message}</label>
<input type="email" name="email" id="email" class="form-control {val["email"].mark}" required autocomplete="off" value="{val["email"].name}">
</div>
<div class="form-group">
<label for="password">Password:</label>
<label class="text-danger">Password is Required</label>
<input type="password" name="password" id="password" class="form-control" required autocomplete="off" value="{pw}">
<label class="text-danger">{val["password"].message}</label>
<input type="password" name="password" id="password" class="form-control {val["password"].mark}" required autocomplete="off" value="{val["password"].name}">
</div>

5
src/model/rand.nim Normal file
View file

@ -0,0 +1,5 @@
type
Validity* = object
name*: string
message*: string
mark*: string