added first controller; requireLoggedIn with session

This commit is contained in:
overflowerror 2022-01-13 22:32:13 +01:00
parent fb13065e21
commit e3dbeaba4b
11 changed files with 104 additions and 2 deletions

11
controller/backend.sh Normal file
View file

@ -0,0 +1,11 @@
#!/bin/bash
route GET "/backend" backendHome
backendHome() {
requireLoggedIn
endHeaders
title="Backend"
content="$(template "templates/backend.fragment.templ")"
template "templates/layout.html.templ"
}

11
controller/home.sh Normal file
View file

@ -0,0 +1,11 @@
#!/bin/bash
route GET "/" home
home() {
htmlContent
endHeaders
title="Home"
content="$(template "templates/home.fragment.templ")"
template "templates/layout.html.templ"
}

23
controller/users.sh Normal file
View file

@ -0,0 +1,23 @@
#!/bin/bash
route GET "/login" loginForm
loginForm() {
htmlContent
endHeaders
title="Login"
content="$(template "templates/login.fragment.templ")"
template "templates/layout.html.templ"
}
route POST "/login" login
login() {
username="$(formData "username")"
password="$(formData "password")"
if loginUser "$username" "$password"; then
echo "ok"
else
echo "ko"
fi
}

View file

@ -32,6 +32,7 @@ loginUser() {
password="$(hashPassword "$password" "$salt")"
test "$password" = "$hash"
# return true if password is correct
test "$password" != "$hash"
return
}

View file

@ -4,9 +4,20 @@
. shochu/uri.sh
. shochu/router.sh
. shochu/mysql.sh
. shochu/credentials.sh
. shochu/cookies.sh
. shochu/sessions.sh
. shochu/shinden/engine.sh
. utils/auth.sh
. utils/headers.sh
. data/users.sh
. controller/users.sh
. controller/home.sh
. controller/backend.sh
connect "$mysqlHost" "$mysqlUser" "$mysqlPassword" "$mysqlDB"
router

View file

@ -0,0 +1,2 @@
<h1>Hi</h1>
You are {{ print $username }}.

View file

@ -0,0 +1 @@
<h1>Welcome</h1>

View file

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Kukkubukku - {{ print "$title" }}</title>
</head>
<body>
{{ print "$content" }}
</body>
</html>

View file

@ -0,0 +1,5 @@
<form method="POST" action="?">
<input type="text" placeholder="Username" name="username"><br />
<input type="password" placeholder="Password" name="password"><br />
<input type="submit" name="submit"><br />
</from>

23
utils/auth.sh Normal file
View file

@ -0,0 +1,23 @@
#!/bin/bash
_sessionKeyUsername="username"
username=""
isLoggedIn() {
username="$(getSessionValue "$_sessionKeyUsername")"
test ! -z "$username"
return
}
requireLoggedIn() {
startSession
if isLoggedIn; then
echo > /dev/null # empty path
else
redirect "/login"
endHeaders
exit
fi
}

5
utils/headers.sh Normal file
View file

@ -0,0 +1,5 @@
#!/bin/bash
htmlContent() {
header "Content-Type" "text/html"
}