Skip to content

Commit 4501f4d

Browse files
author
zainhoda
committed
Stub log in and log out handling
1 parent 6d1edb2 commit 4501f4d

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

main.go

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package main
22

33
import (
4-
"fmt"
54
"html/template"
65
"log"
76
"net/http"
7+
"time"
88

99
"github.com/julienschmidt/httprouter"
1010
)
@@ -15,34 +15,51 @@ func LoginPage(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
1515

1616
baseT.ExecuteTemplate(w, "base", map[string]string{
1717
"PageName": "login",
18+
"User": getUser(w, req),
1819
})
1920
}
2021

22+
func LoginHandler(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
23+
username := p.ByName("email")
24+
password := p.ByName("password")
25+
26+
if verifyUser(w, req, username, password) {
27+
http.Redirect(w, req, "/admin/", http.StatusFound)
28+
}
29+
}
30+
31+
func LogoutHandler(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
32+
http.Redirect(w, req, "/", http.StatusFound)
33+
}
34+
2135
func MainPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
2236
baseT := template.Must(template.New("base").Parse(base))
2337
baseT = template.Must(baseT.Parse(mainPage))
2438

2539
baseT.ExecuteTemplate(w, "base", map[string]string{
2640
"PageName": "main",
41+
"User": getUser(w, r),
2742
})
2843
}
2944

30-
func SignupPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
31-
username := ps.ByName("username")
32-
password := ps.ByName("password")
33-
45+
func SignupPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
3446
baseT := template.Must(template.New("base").Parse(base))
3547
baseT = template.Must(baseT.Parse(signup))
3648

3749
baseT.ExecuteTemplate(w, "base", map[string]string{
3850
"PageName": "signup",
51+
"User": getUser(w, r),
3952
})
53+
}
54+
55+
func SignupHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
56+
username := ps.ByName("email")
57+
password := ps.ByName("password")
4058

41-
// create new user
42-
if len(username)+len(password) < 11 {
43-
fmt.Fprintf(w, "New user"+username+" with pass:"+password+"has signed up")
59+
if addUser(username, password) {
60+
http.Redirect(w, r, "/admin/", http.StatusFound)
4461
} else {
45-
fmt.Fprintf(w, "Invalid arguments!")
62+
http.Redirect(w, r, "/signup/", http.StatusFound)
4663
}
4764
}
4865

@@ -59,14 +76,45 @@ func AdminPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
5976

6077
baseT.ExecuteTemplate(w, "base", map[string]string{
6178
"PageName": "admin",
79+
"User": getUser(w, r),
6280
})
6381
}
6482

83+
func verifyUser(w http.ResponseWriter, r *http.Request, username string, password string) bool {
84+
cookie := http.Cookie{Name: "goblog", Value: "blah", Expires: time.Now().Add(time.Hour * 24 * 7 * 52), HttpOnly: true, MaxAge: 50000, Path: "/"}
85+
http.SetCookie(w, &cookie)
86+
return true
87+
}
88+
89+
func addUser(username string, password string) bool {
90+
// Insert into database
91+
92+
return true
93+
}
94+
95+
func getUser(w http.ResponseWriter, r *http.Request) string {
96+
cookie, err := r.Cookie("goblog")
97+
98+
if err == nil {
99+
if cookie.Value != "" {
100+
return cookieToUsername(cookie.Value)
101+
}
102+
}
103+
return ""
104+
}
105+
106+
func cookieToUsername(cookie string) string {
107+
108+
}
109+
65110
func main() {
66111
router := httprouter.New()
67112
router.GET("/", MainPage)
68-
router.GET("/login/", LoginPage)
113+
//router.GET("/login/", LoginPage)
114+
router.POST("/login/", LoginHandler)
69115
router.GET("/signup/", SignupPage)
116+
router.POST("/signup/", SignupHandler)
70117
router.GET("/admin/", AdminPage)
118+
router.GET("/logout/", LogoutHandler)
71119
log.Fatal(http.ListenAndServe(":1337", router))
72120
}

pages.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ var base = `
4646
<button type="submit" class="btn btn-default">Submit</button>
4747
</form>
4848
<ul class="nav navbar-nav navbar-right">
49-
<li {{if eq .PageName "signup" }}class="active"{{end}}><a href="/signup">Sign Up</a></li>
50-
<li class="dropdown">
49+
<li {{if not .User}}class="hidden"{{end}}> <a href="/admin">{{if .User}}{{.User}}{{end}}</a> </li>
50+
<li {{if not .User}}class="hidden"{{end}}> <a href="/logout">Log Out</a> </li>
51+
<li {{if .User}}class="hidden"{{end}} {{if eq .PageName "signup" }}class="active"{{end}}><a href="/signup">Sign Up</a></li>
52+
<li {{if .User}}class="hidden"{{end}} class="dropdown">
5153
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Sign in <b class="caret"></b></a>
5254
<ul class="dropdown-menu" style="padding: 15px;min-width: 250px;">
5355
<li>

0 commit comments

Comments
 (0)