we know if a user is logger in so use it

This commit is contained in:
stryan 2020-09-22 14:00:43 -04:00
parent 0250dd0066
commit c3d1d097d8
3 changed files with 21 additions and 24 deletions

38
main.go
View File

@ -22,15 +22,13 @@ func signupPage(res http.ResponseWriter, req *http.Request) {
http.Redirect(res, req, "/", 302)
} else {
data := struct {
Title string
Username string
ShowLogin bool
ShowLogout bool
Title string
Username string
LoggedIn bool
}{
"Register",
"Unregistered",
false,
false,
}
tpl.ExecuteTemplate(res, "register", data)
}
@ -71,14 +69,12 @@ func loginPage(res http.ResponseWriter, req *http.Request) {
http.Redirect(res, req, "/", 302)
} else {
data := struct {
Title string
Username string
ShowLogin bool
ShowLogout bool
Title string
Username string
LoggedIn bool
}{
"Login",
"Unregistered",
true,
false,
}
tpl.ExecuteTemplate(res, "login", data)
@ -116,15 +112,13 @@ func tokenPage(res http.ResponseWriter, req *http.Request) {
tpl.ExecuteTemplate(res, "error", nil)
}
data := struct {
Title string
Username string
ShowLogin bool
ShowLogout bool
Token string
Title string
Username string
LoggedIn bool
Token string
}{
"Token Generation",
u,
false,
true,
token,
}
@ -133,20 +127,20 @@ func tokenPage(res http.ResponseWriter, req *http.Request) {
func homePage(res http.ResponseWriter, req *http.Request) {
u := getUserName(req)
active := false
uname := "Unregistered"
if u != "" {
uname = u
active = true
}
data := struct {
Title string
Username string
ShowLogin bool
ShowLogout bool
Title string
Username string
LoggedIn bool
}{
"Index",
uname,
true,
true,
active,
}
tpl.ExecuteTemplate(res, "index", data)

View File

@ -9,7 +9,7 @@
<nav>
<div style="overflow: hidden;">
<p style="float: left;">Logged in as: {{.Username}}</p>
<p style="float: right;"><a href="/">Home</a> {{if .ShowLogin}}<a href="/login">Login</a> {{end}}{{if .ShowLogout}}<a href="/logout">Logout</a>{{end}}</p>
<p style="float: right;"><a href="/">Home</a> {{if .LoggedIn}}<a href="/logout">Logout</a> {{else}}<a href="/login">Login</a>{{end}}</p>
</div>
</nav>
{{end}}

View File

@ -1,6 +1,9 @@
{{ define "index" }}
{{ template "header" .}}
<p><a href="/register">Register</a></p>
{{if .LoggedIn }}
<p><a href="/token">Get Token</a></p>
{{else}}
<p><a href="/register">Register</a></p>
{{end}}
{{template "footer" .}}
{{ end }}