mirror of
https://github.com/sigmasternchen/kukkubukku
synced 2025-03-15 07:18:54 +00:00
added first controller; requireLoggedIn with session
This commit is contained in:
parent
fb13065e21
commit
e3dbeaba4b
11 changed files with 104 additions and 2 deletions
11
controller/backend.sh
Normal file
11
controller/backend.sh
Normal 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
11
controller/home.sh
Normal 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
23
controller/users.sh
Normal 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
|
||||
}
|
|
@ -32,6 +32,7 @@ loginUser() {
|
|||
|
||||
password="$(hashPassword "$password" "$salt")"
|
||||
|
||||
test "$password" = "$hash"
|
||||
# return true if password is correct
|
||||
test "$password" != "$hash"
|
||||
return
|
||||
}
|
||||
|
|
13
index.sh
13
index.sh
|
@ -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
|
||||
|
|
2
templates/backend.fragment.templ
Normal file
2
templates/backend.fragment.templ
Normal file
|
@ -0,0 +1,2 @@
|
|||
<h1>Hi</h1>
|
||||
You are {{ print $username }}.
|
1
templates/home.fragment.templ
Normal file
1
templates/home.fragment.templ
Normal file
|
@ -0,0 +1 @@
|
|||
<h1>Welcome</h1>
|
9
templates/layout.html.templ
Normal file
9
templates/layout.html.templ
Normal file
|
@ -0,0 +1,9 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Kukkubukku - {{ print "$title" }}</title>
|
||||
</head>
|
||||
<body>
|
||||
{{ print "$content" }}
|
||||
</body>
|
||||
</html>
|
5
templates/login.fragment.templ
Normal file
5
templates/login.fragment.templ
Normal 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
23
utils/auth.sh
Normal 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
5
utils/headers.sh
Normal file
|
@ -0,0 +1,5 @@
|
|||
#!/bin/bash
|
||||
|
||||
htmlContent() {
|
||||
header "Content-Type" "text/html"
|
||||
}
|
Loading…
Reference in a new issue