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

20
main.go
View File

@ -24,13 +24,11 @@ func signupPage(res http.ResponseWriter, req *http.Request) {
data := struct {
Title string
Username string
ShowLogin bool
ShowLogout bool
LoggedIn bool
}{
"Register",
"Unregistered",
false,
false,
}
tpl.ExecuteTemplate(res, "register", data)
}
@ -73,12 +71,10 @@ func loginPage(res http.ResponseWriter, req *http.Request) {
data := struct {
Title string
Username string
ShowLogin bool
ShowLogout bool
LoggedIn bool
}{
"Login",
"Unregistered",
true,
false,
}
tpl.ExecuteTemplate(res, "login", data)
@ -118,13 +114,11 @@ func tokenPage(res http.ResponseWriter, req *http.Request) {
data := struct {
Title string
Username string
ShowLogin bool
ShowLogout bool
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
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 }}