Skip to content

Commit 7bba798

Browse files
committed
cleaned getUser function and fixed issue with generating new uuid
1 parent e50d84f commit 7bba798

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

030_sessions/03_signup/main.go

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

33
import (
4-
"github.com/satori/go.uuid"
54
"html/template"
65
"net/http"
6+
7+
uuid "github.com/satori/go.uuid"
78
)
89

910
type user struct {
@@ -30,12 +31,12 @@ func main() {
3031
}
3132

3233
func index(w http.ResponseWriter, req *http.Request) {
33-
u := getUser(w, req)
34+
u := getUser(req)
3435
tpl.ExecuteTemplate(w, "index.gohtml", u)
3536
}
3637

3738
func bar(w http.ResponseWriter, req *http.Request) {
38-
u := getUser(w, req)
39+
u := getUser(req)
3940
if !alreadyLoggedIn(req) {
4041
http.Redirect(w, req, "/", http.StatusSeeOther)
4142
return

030_sessions/03_signup/session.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,18 @@ package main
22

33
import (
44
"net/http"
5-
6-
"github.com/satori/go.uuid"
75
)
86

9-
func getUser(w http.ResponseWriter, req *http.Request) user {
7+
func getUser(req *http.Request) user {
8+
var u user
9+
1010
// get cookie
1111
c, err := req.Cookie("session")
1212
if err != nil {
13-
sID, _ := uuid.NewV4()
14-
c = &http.Cookie{
15-
Name: "session",
16-
Value: sID.String(),
17-
}
18-
13+
return u
1914
}
20-
//Next line may not be required, commenting it
21-
// http.SetCookie(w, c)
2215

2316
// if the user exists already, get user
24-
var u user
2517
if un, ok := dbSessions[c.Value]; ok {
2618
u = dbUsers[un]
2719
}

0 commit comments

Comments
 (0)