Skip to content

Commit 901f3f5

Browse files
committed
Fix login
1 parent 821987e commit 901f3f5

File tree

4 files changed

+41
-32
lines changed

4 files changed

+41
-32
lines changed

GoBlog.exe

-8.05 MB
Binary file not shown.

goblog.db

-32 KB
Binary file not shown.

main.go

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func LoginPage(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
5151
}
5252

5353
func LoginHandler(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
54-
username := p.ByName("email")
55-
password := p.ByName("password")
54+
username := req.FormValue("email")
55+
password := req.FormValue("password")
5656

5757
if verifyUser(w, req, username, password) {
5858
http.Redirect(w, req, "/admin/", http.StatusFound)
@@ -102,12 +102,14 @@ func SignupPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
102102
}
103103

104104
func SignupHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
105-
username := ps.ByName("email")
106-
password := ps.ByName("password")
105+
username := r.FormValue("email")
106+
password := r.FormValue("password")
107107

108108
if addUser(username, password) {
109+
fmt.Println("Success!")
109110
http.Redirect(w, r, "/admin/", http.StatusFound)
110111
} else {
112+
fmt.Println("Failure!")
111113
http.Redirect(w, r, "/signup/", http.StatusFound)
112114
}
113115
}
@@ -143,17 +145,20 @@ func verifyUser(w http.ResponseWriter, r *http.Request, username string, passwor
143145
})
144146
if password == string(correctpass) {
145147
cookie := http.Cookie{Name: "goblog", Value: RandomString(), Expires: time.Now().Add(time.Hour * 24 * 7 * 52), HttpOnly: true, MaxAge: 50000, Path: "/"}
146-
db, err := bolt.Open("goblog.db", 0600, nil)
148+
http.SetCookie(w, &cookie)
149+
147150
if err != nil {
148151
fmt.Println(err)
149152
}
150-
defer db.Close()
153+
151154
db.Update(func(tx *bolt.Tx) error {
152155
b := tx.Bucket([]byte("CookieBucket"))
153-
err := b.Put([]byte(cookie.Value), []byte(username))
156+
err = b.Put([]byte(cookie.Value), []byte(username))
154157
return err
155158
})
156-
http.SetCookie(w, &cookie)
159+
if err != nil {
160+
fmt.Println(err)
161+
}
157162
return true
158163
}
159164
return false
@@ -168,10 +173,10 @@ func addUser(username string, password string) bool {
168173
defer db.Close()
169174
db.View(func(tx *bolt.Tx) error {
170175
b := tx.Bucket([]byte("UsersBucket"))
171-
check = b.Get([]byte(username)) //username
176+
check = b.Get([]byte(username))
172177
return nil
173178
})
174-
if len(check) > 2 {
179+
if check == nil {
175180
db.Update(func(tx *bolt.Tx) error {
176181
b := tx.Bucket([]byte("UsersBucket"))
177182
err := b.Put([]byte(username), []byte(password))
@@ -210,22 +215,26 @@ func RandomString() string {
210215
return string(b)
211216
}
212217

218+
var servervalue []byte
219+
213220
func getUser(w http.ResponseWriter, r *http.Request) string {
214221
cookie, err := r.Cookie("goblog")
215-
servervalue := []byte("")
216-
db, err := bolt.Open("goblog.db", 0600, nil)
217-
defer db.Close()
218-
db.View(func(tx *bolt.Tx) error {
219-
b := tx.Bucket([]byte("CookieBucket"))
220-
servervalue = b.Get([]byte(cookie.Value))
221-
return nil
222-
})
223-
if err != nil {
224-
fmt.Println(err)
225-
}
226-
if err == nil {
227-
if len(servervalue) > 2 {
228-
if cookie.Value != "delete" {
222+
if cookie != nil && err == nil {
223+
if err != nil {
224+
fmt.Println(err)
225+
}
226+
db, err := bolt.Open("goblog.db", 0600, nil)
227+
if err != nil {
228+
fmt.Println(err)
229+
}
230+
defer db.Close()
231+
db.View(func(tx *bolt.Tx) error {
232+
b := tx.Bucket([]byte("CookieBucket"))
233+
servervalue = b.Get([]byte(cookie.Value))
234+
return nil
235+
})
236+
if servervalue != nil {
237+
if len(servervalue) > 2 {
229238
return string(servervalue)
230239
}
231240
}
@@ -243,5 +252,5 @@ func main() {
243252
router.GET("/admin/", AdminPage)
244253
router.GET("/logout/", LogoutHandler)
245254
router.GET("/new/:email/:password", SignupHandler) // <- for testing
246-
log.Fatal(http.ListenAndServe(":1337", router))
255+
log.Fatal(http.ListenAndServe(":1338", router))
247256
}

pages.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ var base = `
5555
<li>
5656
<div class="row">
5757
<div class="col-md-12">
58-
<form class="form" role="form" method="post" action="/login" accept-charset="UTF-8" id="login-nav">
58+
<form class="form" role="form" method="post" action="/login/" accept-charset="UTF-8" id="login-nav">
5959
<div class="form-group">
60-
<label class="sr-only" for="exampleInputEmail2">Email address</label>
61-
<input type="email" class="form-control" id="exampleInputEmail2" placeholder="Email address" required>
60+
<label class="sr-only" for="email">Email address</label>
61+
<input type="email" class="form-control" name="email" id="email" placeholder="Email address" required>
6262
</div>
6363
<div class="form-group">
64-
<label class="sr-only" for="exampleInputPassword2">Password</label>
65-
<input type="password" class="form-control" id="exampleInputPassword2" placeholder="Password" required>
64+
<label class="sr-only" for="password">Password</label>
65+
<input type="password" class="form-control" name="password" id="password" placeholder="Password" required>
6666
</div>
6767
<div class="checkbox">
6868
<label>
@@ -97,9 +97,9 @@ var login = `
9797
</div>
9898
<form class="form-horizontal" action="/login/" method="POST">
9999
<div class="form-group">
100-
<label for="name" class="col-sm-2 control-label">User name</label>
100+
<label for="email" class="col-sm-2 control-label">User name</label>
101101
<div class="col-sm-4">
102-
<input type="text" class="form-control" id="name" name="name" required>
102+
<input type="text" class="form-control" id="email" name="email" required>
103103
</div>
104104
</div>
105105
<div class="form-group">

0 commit comments

Comments
 (0)