Skip to content

Commit 74b6d1d

Browse files
committed
Added bcrypt auth
1 parent 0e6b21c commit 74b6d1d

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

main.go

+33-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/gorilla/mux"
66
"github.com/gorilla/sessions"
77
"github.com/go-redis/redis"
8+
"golang.org/x/crypto/bcrypt"
89
"html/template"
910
)
1011

@@ -22,14 +23,21 @@ func main() {
2223
r.HandleFunc("/", indexPostHandler).Methods("POST")
2324
r.HandleFunc("/login", loginGetHandler).Methods("GET")
2425
r.HandleFunc("/login", loginPostHandler).Methods("POST")
25-
r.HandleFunc("/test", testGetHandler).Methods("GET")
26+
r.HandleFunc("/register", registerGetHandler).Methods("GET")
27+
r.HandleFunc("/register", registerPostHandler).Methods("POST")
2628
fs := http.FileServer(http.Dir("./static/"))
2729
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
2830
http.Handle("/", r)
2931
http.ListenAndServe(":8080", nil)
3032
}
3133

3234
func indexGetHandler(w http.ResponseWriter, r *http.Request) {
35+
session, _ := store.Get(r, "session")
36+
_, ok := session.Values["username"]
37+
if !ok {
38+
http.Redirect(w, r, "/login", 302)
39+
return
40+
}
3341
comments, err := client.LRange("comments", 0, 10).Result()
3442
if err != nil {
3543
return
@@ -51,20 +59,34 @@ func loginGetHandler(w http.ResponseWriter, r *http.Request) {
5159
func loginPostHandler(w http.ResponseWriter, r *http.Request) {
5260
r.ParseForm()
5361
username := r.PostForm.Get("username")
62+
password := r.PostForm.Get("password")
63+
hash, err := client.Get("user:" + username).Bytes()
64+
if err != nil {
65+
return
66+
}
67+
err = bcrypt.CompareHashAndPassword(hash, []byte(password))
68+
if err != nil {
69+
return
70+
}
5471
session, _ := store.Get(r, "session")
5572
session.Values["username"] = username
5673
session.Save(r, w)
74+
http.Redirect(w, r, "/", 302)
5775
}
5876

59-
func testGetHandler(w http.ResponseWriter, r *http.Request) {
60-
session, _ := store.Get(r, "session")
61-
untyped, ok := session.Values["username"]
62-
if !ok {
63-
return
64-
}
65-
username, ok := untyped.(string)
66-
if !ok {
77+
func registerGetHandler(w http.ResponseWriter, r *http.Request) {
78+
templates.ExecuteTemplate(w, "register.html", nil)
79+
}
80+
81+
func registerPostHandler(w http.ResponseWriter, r *http.Request) {
82+
r.ParseForm()
83+
username := r.PostForm.Get("username")
84+
password := r.PostForm.Get("password")
85+
cost := bcrypt.DefaultCost
86+
hash, err := bcrypt.GenerateFromPassword([]byte(password), cost)
87+
if err != nil {
6788
return
6889
}
69-
w.Write([]byte(username))
70-
}
90+
client.Set("user:" + username, hash, 0)
91+
http.Redirect(w, r, "/login", 302)
92+
}

templates/login.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
</head>
55
<body>
66
<form method="POST">
7-
Username: <input name="username">
7+
<div>Username: <input name="username"></div>
8+
<div>Password: <input name="password"></div>
89
<div>
910
<button type="submit">Login</button>
1011
</div>

templates/register.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<title>Register</title>
4+
</head>
5+
<body>
6+
<form method="POST">
7+
<div>Username: <input name="username"></div>
8+
<div>Password: <input name="password"></div>
9+
<div>
10+
<button type="submit">Register</button>
11+
</div>
12+
</form>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)