Add cookie

This commit is contained in:
Lucas Schumacher 2024-08-08 15:44:19 -04:00
parent 951c081680
commit faf9139d79
2 changed files with 20 additions and 0 deletions

View File

@ -58,9 +58,27 @@ func (u Users) PostSignin(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Something went wrong.", http.StatusInternalServerError)
return
}
// Bad cookie
cookie := http.Cookie{
Name: "bad",
Value: user.Email,
Path: "/",
}
http.SetCookie(w, &cookie)
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 {
u := Users{}
u.Templates.Signup = signup

View File

@ -50,6 +50,8 @@ func main() {
r.Get("/signin", usersCtrlr.GetSignin)
r.Post("/signin", usersCtrlr.PostSignin)
r.Get("/user", usersCtrlr.CurrentUser)
r.NotFound(notFoundHandler)
fmt.Println("Starting the server on :3000...")
http.ListenAndServe(":3000", r)