Add cookie
This commit is contained in:
parent
951c081680
commit
faf9139d79
@ -58,9 +58,27 @@ func (u Users) PostSignin(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "Something went wrong.", http.StatusInternalServerError)
|
http.Error(w, "Something went wrong.", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bad cookie
|
||||||
|
cookie := http.Cookie{
|
||||||
|
Name: "bad",
|
||||||
|
Value: user.Email,
|
||||||
|
Path: "/",
|
||||||
|
}
|
||||||
|
http.SetCookie(w, &cookie)
|
||||||
|
|
||||||
fmt.Fprintf(w, "User authenticated: %+v", user)
|
fmt.Fprintf(w, "User authenticated: %+v", user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u Users) CurrentUser(w http.ResponseWriter, r *http.Request) {
|
||||||
|
email, err := r.Cookie("bad")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprint(w, "The bad cookie could not be read.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "Bad cookie: %s\n", email.Value)
|
||||||
|
}
|
||||||
|
|
||||||
func WithTemplates(user_service *models.UserService, signup Template, signin Template) Users {
|
func WithTemplates(user_service *models.UserService, signup Template, signin Template) Users {
|
||||||
u := Users{}
|
u := Users{}
|
||||||
u.Templates.Signup = signup
|
u.Templates.Signup = signup
|
||||||
|
|||||||
2
main.go
2
main.go
@ -50,6 +50,8 @@ func main() {
|
|||||||
r.Get("/signin", usersCtrlr.GetSignin)
|
r.Get("/signin", usersCtrlr.GetSignin)
|
||||||
r.Post("/signin", usersCtrlr.PostSignin)
|
r.Post("/signin", usersCtrlr.PostSignin)
|
||||||
|
|
||||||
|
r.Get("/user", usersCtrlr.CurrentUser)
|
||||||
|
|
||||||
r.NotFound(notFoundHandler)
|
r.NotFound(notFoundHandler)
|
||||||
fmt.Println("Starting the server on :3000...")
|
fmt.Println("Starting the server on :3000...")
|
||||||
http.ListenAndServe(":3000", r)
|
http.ListenAndServe(":3000", r)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user