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

View File

@ -9,7 +9,7 @@
<nav> <nav>
<div style="overflow: hidden;"> <div style="overflow: hidden;">
<p style="float: left;">Logged in as: {{.Username}}</p> <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> </div>
</nav> </nav>
{{end}} {{end}}

View File

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