Skip to content

Commit 0a02a1c

Browse files
committed
add cookie after session ceated
1 parent e0000d6 commit 0a02a1c

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

go.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@ go 1.17
44

55
require (
66
github.com/caarlos0/env/v6 v6.7.1
7-
github.com/dgrijalva/jwt-go v3.2.0+incompatible
87
github.com/friendsofgo/errors v0.9.2
98
github.com/google/uuid v1.3.0
9+
github.com/gorilla/securecookie v1.1.1
1010
github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12
1111
github.com/labstack/echo/v4 v4.5.0
1212
github.com/lib/pq v1.10.3
1313
github.com/spf13/viper v1.8.1
14-
github.com/stretchr/testify v1.7.0
1514
github.com/volatiletech/randomize v0.0.1
1615
github.com/volatiletech/sqlboiler/v4 v4.6.0
1716
github.com/volatiletech/strmangle v0.0.1
1817
go.uber.org/zap v1.19.1
1918
)
2019

2120
require (
22-
github.com/davecgh/go-spew v1.1.1 // indirect
2321
github.com/fsnotify/fsnotify v1.4.9 // indirect
2422
github.com/gofrs/uuid v3.2.0+incompatible // indirect
2523
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
@@ -33,7 +31,6 @@ require (
3331
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
3432
github.com/pelletier/go-toml v1.9.3 // indirect
3533
github.com/pkg/errors v0.9.1 // indirect
36-
github.com/pmezard/go-difflib v1.0.0 // indirect
3734
github.com/spf13/afero v1.6.0 // indirect
3835
github.com/spf13/cast v1.3.1 // indirect
3936
github.com/spf13/jwalterweatherman v1.1.0 // indirect
@@ -53,5 +50,4 @@ require (
5350
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
5451
gopkg.in/ini.v1 v1.62.0 // indirect
5552
gopkg.in/yaml.v2 v2.4.0 // indirect
56-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
5753
)

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
8282
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
8383
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8484
github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
85-
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
8685
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
8786
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
8887
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -183,6 +182,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
183182
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
184183
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
185184
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
185+
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
186+
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
186187
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
187188
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
188189
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=

internal/handlers/session.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package handlers
22

33
import (
44
"context"
5-
"errors"
65
"net/http"
6+
"sandbox-go-api-sqlboiler-rest-auth/internal/scookie"
77
"sandbox-go-api-sqlboiler-rest-auth/models"
88
"time"
99

@@ -40,20 +40,34 @@ func (h *Handlers) CreateSession(c echo.Context) error {
4040
return echo.NewHTTPError(http.StatusBadRequest)
4141
}
4242

43-
var session models.Session
43+
var s models.Session
4444
var uid, _ = uuid.NewUUID()
45-
session.ID = uid.String()
46-
session.UserID = u.ID
47-
session.ExpiresAt = time.Now().Add(time.Hour * 24 * 30)
48-
err = session.Insert(ctx, h.db, boil.Infer())
45+
s.ID = uid.String()
46+
s.UserID = u.ID
47+
s.ExpiresAt = time.Now().Add(time.Hour * 24 * 30)
48+
err = s.Insert(ctx, h.db, boil.Infer())
4949
if err != nil {
5050
return echo.NewHTTPError(http.StatusInternalServerError)
5151
}
5252

53+
sc := scookie.NewSecureCookie()
54+
encoded, err := sc.Encode("session", s.ID)
55+
if err != nil {
56+
return echo.NewHTTPError(http.StatusInternalServerError)
57+
}
58+
cookie := &http.Cookie{
59+
Name: "session",
60+
Value: encoded,
61+
Path: "/",
62+
Expires: time.Now().Add(time.Hour * 24 * 30),
63+
Secure: true,
64+
HttpOnly: true,
65+
SameSite: 2,
66+
}
67+
c.SetCookie(cookie)
5368
return c.NoContent(http.StatusNoContent)
5469
}
5570

5671
func (h *Handlers) DeleteSession(c echo.Context) error {
57-
//ctx := context.Background()
58-
return errors.New("not implemented")
72+
return c.NoContent(http.StatusNoContent)
5973
}

internal/scookie/scookie.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package scookie
2+
3+
import (
4+
"github.com/gorilla/securecookie"
5+
)
6+
7+
func NewSecureCookie() *securecookie.SecureCookie {
8+
var hashKey = []byte("jkb2kJU4C6ad11DOFElCYMhtF8kvw75n")
9+
return securecookie.New(hashKey, nil)
10+
}

0 commit comments

Comments
 (0)