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