Add Cookie on login

This commit is contained in:
Jan Bader 2016-06-14 23:08:40 +02:00
parent 576084e1de
commit 48eea7fb37

View File

@ -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,
})