refactor: Provide context to user repository
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"git.ostiwe.com/ostiwe-com/status/modules/jwt"
|
"git.ostiwe.com/ostiwe-com/status/modules/jwt"
|
||||||
"git.ostiwe.com/ostiwe-com/status/repository"
|
"git.ostiwe.com/ostiwe-com/status/repository"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
@@ -16,8 +18,8 @@ func New() Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Module) Proceed(login, password string) (*string, error) {
|
func (m *Module) Proceed(ctx context.Context, login, password string) (*string, error) {
|
||||||
lightweightUser, err := m.userRepository.FindByLogin(login)
|
lightweightUser, err := m.userRepository.FindByLogin(ctx, login)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"git.ostiwe.com/ostiwe-com/status/model"
|
"git.ostiwe.com/ostiwe-com/status/model"
|
||||||
"git.ostiwe.com/ostiwe-com/status/modules/db"
|
"git.ostiwe.com/ostiwe-com/status/modules/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User interface {
|
type User interface {
|
||||||
FindByLogin(login string) (*model.User, error)
|
FindByLogin(ctx context.Context, login string) (*model.User, error)
|
||||||
FindByID(ID uint64) (*model.User, error)
|
FindByID(ctx context.Context, ID uint64) (*model.User, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type userRepository struct {
|
type userRepository struct {
|
||||||
@@ -18,14 +20,14 @@ func NewUserRepository() User {
|
|||||||
return &userRepository{repository{db: db.Global}}
|
return &userRepository{repository{db: db.Global}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userRepository) FindByLogin(login string) (*model.User, error) {
|
func (u userRepository) FindByLogin(ctx context.Context, login string) (*model.User, error) {
|
||||||
var user *model.User
|
var user *model.User
|
||||||
|
|
||||||
return user, u.db.Find(&user, "login = ?", login).Error
|
return user, u.db.WithContext(ctx).Find(&user, "login = ?", login).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u userRepository) FindByID(ID uint64) (*model.User, error) {
|
func (u userRepository) FindByID(ctx context.Context, ID uint64) (*model.User, error) {
|
||||||
var user *model.User
|
var user *model.User
|
||||||
|
|
||||||
return user, u.db.Find(&user, "id = ?", ID).Error
|
return user, u.db.WithContext(ctx).Find(&user, "id = ?", ID).Error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func (c *Controller) plainLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
jwtString, err := c.authModule.Proceed(payload.Login, payload.Password)
|
jwtString, err := c.authModule.Proceed(r.Context(), payload.Login, payload.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(w, r)
|
sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(w, r)
|
||||||
if sendErr != nil {
|
if sendErr != nil {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func SetUserFromJWT(next http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
userRepo := repository.NewUserRepository()
|
userRepo := repository.NewUserRepository()
|
||||||
user, err := userRepo.FindByLogin(token.Subject())
|
user, err := userRepo.FindByLogin(ctx, token.Subject())
|
||||||
if err != nil || user == nil {
|
if err != nil || user == nil {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user