CHANGE: user login style

This commit is contained in:
2025-02-21 20:44:35 +01:00
parent f6e77bda66
commit 3be2a47c56
8 changed files with 122 additions and 30 deletions

View File

@@ -43,14 +43,19 @@ func parseTimestamp(r *http.Request , get_key string, fallback string) (time.Tim
// Returns bookings from DB with similar card uid -> checks for card uid in http query params
func getBookings(w http.ResponseWriter, r *http.Request) {
// if(!Session.Exists(r.Context(), "user")){
// log.Println("No user in session storage!")
// http.Error(w, "Not logged in!", http.StatusForbidden)
// return
// }
var user models.User
var err error
if(helper.GetEnv("GO_ENV", "production") == "debug"){
user, err = (*models.User).GetByPersonalNummer(nil, 123)
}else{
if(!Session.Exists(r.Context(), "user")){
log.Println("No user in session storage!")
http.Error(w, "Not logged in!", http.StatusForbidden)
return
}
// user, err := (*models.User).GetByPersonalNummer(nil, Session.GetInt(r.Context(), "user"))
user, err := (*models.User).GetByPersonalNummer(nil, 123)
user, err = (*models.User).GetByPersonalNummer(nil, Session.GetInt(r.Context(), "user"))
}
if(err != nil){
log.Println("No user found with the given personal number!")
http.Error(w, "No user found", http.StatusNotFound)

View File

@@ -21,18 +21,18 @@ func CreateSessionManager(lifetime time.Duration) *scs.SessionManager {
func LoginHandler(w http.ResponseWriter, r *http.Request){
switch r.Method{
case http.MethodGet: showForm(w, r)
case http.MethodGet: showForm(w, r, false)
break
case http.MethodPost: loginUser(w, r)
break
default:
showForm(w, r)
showForm(w, r, false)
break
}
}
func showForm(w http.ResponseWriter, r *http.Request){
templates.LoginForm().Render(r.Context(), w)
func showForm(w http.ResponseWriter, r *http.Request, failed bool){
templates.LoginForm(failed).Render(r.Context(), w)
}
func loginUser(w http.ResponseWriter, r *http.Request){
@@ -66,7 +66,9 @@ func loginUser(w http.ResponseWriter, r *http.Request){
log.Printf("New succesfull user login from %s %s!\n", user.Vorname, user.Name)
Session.Put(r.Context(), "user", user.PersonalNummer)
http.Redirect(w, r, "/time", http.StatusSeeOther) //with this browser always uses GET
}else{
showForm(w, r, true)
}
showForm(w, r)
showForm(w, r, false)
}

View File

@@ -8,4 +8,6 @@ require github.com/lib/pq v1.10.9
require github.com/a-h/templ v0.3.833
require github.com/alexedwards/scs/v2 v2.8.0 // indirect
require github.com/alexedwards/scs/v2 v2.8.0
require github.com/joho/godotenv v1.5.1

View File

@@ -4,5 +4,7 @@ github.com/alexedwards/scs/v2 v2.8.0 h1:h31yUYoycPuL0zt14c0gd+oqxfRwIj6SOjHdKRZx
github.com/alexedwards/scs/v2 v2.8.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=

View File

@@ -10,6 +10,7 @@ import (
"net/http"
"time"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
@@ -17,6 +18,12 @@ import (
func main() {
var err error
err = godotenv.Load(".env")
if err != nil {
log.Println("No .env file found in directory!")
}
models.DB, err = OpenDatabase()
if err != nil {
log.Fatal(err)

View File

@@ -600,9 +600,21 @@
.h-4 {
height: calc(var(--spacing) * 4);
}
.h-\[90vh\] {
height: 90vh;
}
.h-\[100vh\] {
height: 100vh;
}
.h-full {
height: 100%;
}
.w-1 {
width: calc(var(--spacing) * 1);
}
.w-1\/2 {
width: calc(1/2 * 100%);
}
.w-2 {
width: calc(var(--spacing) * 2);
}
@@ -702,18 +714,24 @@
.border-neutral-200 {
border-color: var(--color-neutral-200);
}
.border-neutral-300 {
border-color: var(--color-neutral-300);
}
.border-neutral-400 {
border-color: var(--color-neutral-400);
}
.border-neutral-900 {
border-color: var(--color-neutral-900);
}
.border-red-600 {
border-color: var(--color-red-600);
}
.bg-accent {
background-color: var(--color-accent);
}
.bg-neutral-100 {
background-color: var(--color-neutral-100);
}
.bg-neutral-200 {
background-color: var(--color-neutral-200);
}
.bg-neutral-300 {
background-color: var(--color-neutral-300);
}
@@ -726,6 +744,9 @@
.bg-purple-600 {
background-color: var(--color-purple-600);
}
.bg-red-200 {
background-color: var(--color-red-200);
}
.bg-red-600 {
background-color: var(--color-red-600);
}
@@ -745,6 +766,10 @@
font-size: var(--text-sm);
line-height: var(--tw-leading, var(--text-sm--line-height));
}
.text-xs {
font-size: var(--text-xs);
line-height: var(--tw-leading, var(--text-xs--line-height));
}
.font-bold {
--tw-font-weight: var(--font-weight-bold);
font-weight: var(--font-weight-bold);
@@ -817,6 +842,13 @@
}
}
}
.hover\:border-neutral-500 {
&:hover {
@media (hover: hover) {
border-color: var(--color-neutral-500);
}
}
}
.hover\:bg-neutral-200 {
&:hover {
@media (hover: hover) {
@@ -824,13 +856,6 @@
}
}
}
.hover\:bg-neutral-300 {
&:hover {
@media (hover: hover) {
background-color: var(--color-neutral-300);
}
}
}
.hover\:bg-neutral-700 {
&:hover {
@media (hover: hover) {

View File

@@ -1,10 +1,22 @@
package templates
templ LoginForm(){
templ LoginForm(failed bool){
{{
failedClass := ""
if(failed){
failedClass = "border border-red-600"
}
}}
@Style()
<form method="POST">
<input name="personal_nummer" type="text" placeholder="personal_nummer"/>
<input name="password" type="passwort" placeholder="password"/>
<button type="submit">Send</button>
<div class="w-full h-[100vh] flex flex-col justify-center items-center">
<form method="POST" class={"w-1/2 flex flex-col gap-4 p-2", failedClass}>
<input name="personal_nummer" placeholder="Personalnummer" type="text" class="w-full placeholder:text-neutral-400 text-neutral-700 text-sm border border-neutral-300 rounded-md px-3 py-2 transition duration-300 ease focus:outline-none hover:border-neutral-500"/>
<input name="password" placeholder="Passwort" type="password" class="w-full placeholder:text-neutral-400 text-neutral-700 text-sm border border-neutral-300 rounded-md px-3 py-2 transition duration-300 ease focus:outline-none hover:border-neutral-500"/>
if failed {
<p class="text-red-600 text-sm" >Login fehlgeschlagen, bitte erneut versuchen!</p>
}
<button type="submit" class="cursor-pointer rounded-md text-neutral-800 p-2 md:px-4 border text-center text-sm hover:text-white transition-colors border-neutral-300 focus:bg-neutral-700 active:bg-neutral-700 hover:bg-neutral-700 disabled:pointer-events-none disabled:opacity-50">Login</button>
</form>
</div>
}

View File

@@ -8,7 +8,7 @@ package templates
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func LoginForm() templ.Component {
func LoginForm(failed bool) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
@@ -29,11 +29,48 @@ func LoginForm() templ.Component {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
failedClass := ""
if failed {
failedClass = "border border-red-600"
}
templ_7745c5c3_Err = Style().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<form method=\"POST\"><input name=\"personal_nummer\" type=\"text\" placeholder=\"personal_nummer\"> <input name=\"password\" type=\"passwort\" placeholder=\"password\"> <button type=\"submit\">Send</button></form>")
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div class=\"w-full h-[100vh] flex flex-col justify-center items-center\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 = []any{"w-1/2 flex flex-col gap-4 p-2", failedClass}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var2...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "<form method=\"POST\" class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var2).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/user.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\"><input name=\"personal_nummer\" placeholder=\"Personalnummer\" type=\"text\" class=\"w-full placeholder:text-neutral-400 text-neutral-700 text-sm border border-neutral-300 rounded-md px-3 py-2 transition duration-300 ease focus:outline-none hover:border-neutral-500\"> <input name=\"password\" placeholder=\"Passwort\" type=\"password\" class=\"w-full placeholder:text-neutral-400 text-neutral-700 text-sm border border-neutral-300 rounded-md px-3 py-2 transition duration-300 ease focus:outline-none hover:border-neutral-500\"> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if failed {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "<p class=\"text-red-600 text-sm\">Login fehlgeschlagen, bitte erneut versuchen!</p>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<button type=\"submit\" class=\"cursor-pointer rounded-md text-neutral-800 p-2 md:px-4 border text-center text-sm hover:text-white transition-colors border-neutral-300 focus:bg-neutral-700 active:bg-neutral-700 hover:bg-neutral-700 disabled:pointer-events-none disabled:opacity-50\">Login</button></form></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}