Add Cookie on login
This commit is contained in:
parent
576084e1de
commit
48eea7fb37
9
main.go
9
main.go
@ -43,11 +43,13 @@ func main() {
|
|||||||
func accessible(c echo.Context) error {
|
func accessible(c echo.Context) error {
|
||||||
return c.String(http.StatusOK, "Accessible")
|
return c.String(http.StatusOK, "Accessible")
|
||||||
}
|
}
|
||||||
|
|
||||||
func restricted(c echo.Context) error {
|
func restricted(c echo.Context) error {
|
||||||
user := c.Get("user").(*jwt.Token)
|
user := c.Get("user").(*jwt.Token)
|
||||||
name := user.Claims["name"].(string)
|
name := user.Claims["name"].(string)
|
||||||
return c.String(http.StatusOK, "Welcome "+name+"!")
|
return c.String(http.StatusOK, "Welcome "+name+"!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func login(c echo.Context) error {
|
func login(c echo.Context) error {
|
||||||
username := c.FormValue("username")
|
username := c.FormValue("username")
|
||||||
password := c.FormValue("password")
|
password := c.FormValue("password")
|
||||||
@ -66,6 +68,13 @@ func login(c echo.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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{
|
return c.JSON(http.StatusOK, map[string]string{
|
||||||
"token": t,
|
"token": t,
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user