Skip to content

Commit

Permalink
init login request
Browse files Browse the repository at this point in the history
  • Loading branch information
loan-mgt committed Jun 13, 2024
1 parent 0e4f2ef commit bdf3a67
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
31 changes: 22 additions & 9 deletions internal/controllers/indexController.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,38 @@ import (
"log"
"net/http"

templatedata "rcp/elite/internal/types/template-data"
"rcp/elite/internal/utils"
)

func IndexController(w http.ResponseWriter, r *http.Request) {
// Check if the 'stk' cookie is set

data := templatedata.IndexData{
Main: "logged",
}
cookie, err := r.Cookie("stk")
if err != nil {
err = utils.Templates.ExecuteTemplate(w, "index", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
data.Main = "home"

defaultCookie := http.Cookie{
Name: "stk",
Value: "default_value_here",
MaxAge: 60 * 60 * 24 * 60, // 2 months in seconds (60 seconds * 60 minutes * 24 hours * 60 days)
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteStrictMode,
}
}
http.SetCookie(w, &defaultCookie)
log.Println("Cookie 'stk' set with default value")

// If the cookie is found, you can access its value with cookie.Value
log.Println("Cookie 'stk' found with value:", cookie.Value)
} else {
// If the cookie is found, you can access its value with cookie.Value
log.Println("Cookie 'stk' found with value:", cookie.Value)
}

err = utils.Templates.ExecuteTemplate(w, "logged", nil)
err = utils.Templates.ExecuteTemplate(w, "index", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

}
5 changes: 5 additions & 0 deletions internal/types/template-data/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package templatedata

type IndexData struct {
Main string
}
12 changes: 5 additions & 7 deletions template/home.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
<img class="h-full w-full" src="/assets/images/logo_transparent.svg" alt="logo" />
</div>

<form id="username-form" class="flex flex-col gap-2" hx-ws="send:submit" hx-vals='{"type": "game-search"}'>
<label class="font-semibold h-fit" for="username">Username:</label>
<input class="py-1 px-2 rounded-lg border-primary border-2" id="username" name="username" type="text"
maxlength="15" placeholder="Your best username" />
<form id="username-form" class="flex flex-col gap-2" action="/login" hx-get="/login" hx-target="#main" hx-ws="submit:get username-form">
<label class="font-semibold h-fit" for="username">Username:</label>
<input class="py-1 px-2 rounded-lg border-primary border-2" id="username" name="username" type="text" maxlength="15" placeholder="Your best username" />

<button class="py-1 px-2 bg-primary rounded-lg font-semibold mt-2 hover-bg-secondary transition-all"
type="submit">Play</button>
</form>
<button class="py-1 px-2 bg-primary rounded-lg font-semibold mt-2 hover-bg-secondary transition-all" type="submit">Play</button>
</form>
</div>
{{ end }}
6 changes: 5 additions & 1 deletion template/index.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

<body class="flex flex-col gap-5 items-center w-full h-full bg-primary-light" hx-ws="connect:/ws">

{{ template "home" . }}
{{ if eq .Main "home" }}
{{ template "home" . }}
{{ else if eq .Main "logged" }}
{{ template "logged" . }}
{{ end }}

</body>

Expand Down
36 changes: 14 additions & 22 deletions template/logged.html.tmpl
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
{{ define "logged" }}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RCP Game</title>
<link rel="icon" href="/assets/images/logo_transparent.svg">

<link rel="stylesheet" href="/styles/output.css">

</head>

<script src="https://unpkg.com/[email protected]"></script>

<body class="bg-primary-light flex h-full w-full items-center flex-col gap-5" hx-ws="connect:/ws">

{{ template "home" . }}

</body>

</html>
<div id="main" class="flex h-full w-full items-center flex-col gap-5">
<div class="h-36 w-auto max-h-1/4">
<img class="h-full w-full" src="/assets/images/logo_transparent.svg" alt="logo" />
</div>

<form id="username-form" class="flex flex-col gap-2" hx-ws="send:submit" hx-vals='{"type": "game-search"}'>
<label class="font-semibold h-fit" for="username">Username:</label>
<input readonly class="py-1 px-2 rounded-lg border-primary border-2" id="username" name="username" type="text"
maxlength="15" placeholder="Your best username" />

<button class="py-1 px-2 bg-primary rounded-lg font-semibold mt-2 hover-bg-secondary transition-all"
type="submit">Play</button>
</form>
</div>
{{ end }}

0 comments on commit bdf3a67

Please sign in to comment.