Update access token in db and add loggedIn middleware

This commit is contained in:
Viktor Patchev
2022-01-06 18:43:42 +01:00
parent 98edef469e
commit b86e4fa54b
6 changed files with 19 additions and 1 deletions

View File

@@ -22,6 +22,8 @@ type User struct {
func (u *User) GenerateAccessToken(c echo.Context) error {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"id": u.ID,
"email": u.Email,
})
t, err := token.SignedString([]byte("secret"))

4
go.mod
View File

@@ -7,6 +7,7 @@ require (
github.com/go-playground/validator/v10 v10.10.0
github.com/joho/godotenv v1.4.0
github.com/labstack/echo/v4 v4.6.1
github.com/labstack/gommon v0.3.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
gorm.io/driver/postgres v1.2.3
@@ -18,6 +19,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
@@ -28,7 +30,6 @@ require (
github.com/jackc/pgx/v4 v4.14.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.3 // indirect
github.com/labstack/gommon v0.3.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
@@ -40,5 +41,6 @@ require (
golang.org/x/net v0.0.0-20210913180222-943fd674d43e // indirect
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

2
go.sum
View File

@@ -25,6 +25,7 @@ github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXS
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
@@ -216,6 +217,7 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 h1:Hir2P/De0WpUhtrKGGjvSb2YxUgyZ7EFOSLIcSSpiwE=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=

View File

@@ -2,6 +2,7 @@ package middlewares
import (
"fmt"
"github.com/labstack/echo/v4/middleware"
"net/http"
"os"
@@ -13,6 +14,10 @@ var (
jwtKey = os.Getenv("JWT_KEY")
)
var IsLoggedIn = middleware.JWTWithConfig(middleware.JWTConfig{
SigningKey: []byte("secret"),
})
// Authoriszed : Check Auth
func Authoriszed(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {

View File

@@ -29,6 +29,7 @@ func main() {
e.Validator = &lib.CustomValidator{Validator: validator.New()}
e.Use(middlewares.ContextDB(db))
//e.Use(middlewares.IsLoggedIn)
routes.Routes(e.Group(""))

View File

@@ -55,6 +55,12 @@ func (AuthRouter) Auth(c echo.Context) error {
return err
}
if err := db.Model(&user).Where("id = ?", user.ID).Update("access_token", user.AccessToken).Error; err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{
"message": "server error, try again",
})
}
//var cookie http.Cookie
//
//cookie.Name = "token"