From 48eea7fb377cd6d26674baba7876266ef8c41b36 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Tue, 14 Jun 2016 23:08:40 +0200 Subject: [PATCH] Add Cookie on login --- main.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/main.go b/main.go index d810af8..ef06c34 100644 --- a/main.go +++ b/main.go @@ -43,11 +43,13 @@ func main() { func accessible(c echo.Context) error { return c.String(http.StatusOK, "Accessible") } + func restricted(c echo.Context) error { user := c.Get("user").(*jwt.Token) name := user.Claims["name"].(string) return c.String(http.StatusOK, "Welcome "+name+"!") } + func login(c echo.Context) error { username := c.FormValue("username") password := c.FormValue("password") @@ -66,6 +68,13 @@ func login(c echo.Context) error { if err != nil { return err } + + cookie := new(echo.Cookie) + cookie.SetName("authentication") + cookie.SetValue(t) + cookie.SetExpires(time.Now().Add(expiration * time.Hour)) + c.SetCookie(cookie) + return c.JSON(http.StatusOK, map[string]string{ "token": t, })