fix null pointer exception

This commit is contained in:
Nimer Farahty 2025-06-08 16:52:49 +03:00
parent fe76279706
commit 7949a93dcb
5 changed files with 25 additions and 30 deletions

View File

@ -23,13 +23,6 @@ func LoaderMiddleware(loaders *Loaders, next http.Handler) http.Handler {
// ExpiryMiddleware checks for expired tokens in GraphQL resolvers
func ExpiryMiddleware(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
object := graphql.GetRootFieldContext(ctx)
if object != nil {
println("ExpiryMiddleware: checking token expiry in GraphQL resolver")
} else {
println("ExpiryMiddleware: checking token expiry in HTTP request")
}
if IsTokenExpired(ctx) {
return graphql.ErrorResponse(ctx, "token expired")
}

View File

@ -29,7 +29,7 @@ func mutateHook(b *modelgen.ModelBuild) *modelgen.ModelBuild {
field.Tag = strings.TrimSuffix(field.Tag, `"`) + `,omitempty" bson:"-"`
}
} else {
field.Tag = strings.TrimSuffix(field.Tag, `"`) + `" bson:"` + field.Name + `,omitempty"`
field.Tag = strings.TrimSuffix(field.Tag, `"`) + `" bson:"` + strings.ToLower(field.Name) + `,omitempty"`
}
}
}

View File

@ -1,12 +1,10 @@
schema:
- gql/*.gql
exec:
filename: generated/generated.go
package: generated
model:
filename: models/models_gen.go
package: models
@ -26,11 +24,11 @@ autobind:
# your liking
models:
ID:
model:
model:
- git.farahty.com/nimer/go-mongo/utils.ObjectID
- github.com/99designs/gqlgen/graphql.ID
Int:
model:
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int64
- github.com/99designs/gqlgen/graphql.Int32
- github.com/99designs/gqlgen/graphql.Int32

View File

@ -28,18 +28,18 @@ type Category struct {
Body []*Translated `json:"body,omitempty" bson:"body,omitempty"`
// #bson:ignore
Parent *Category `json:"parent,omitempty,omitempty" bson:"-"`
ParentID *primitive.ObjectID `json:"parentId,omitempty" bson:"parentId,omitempty"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt,omitempty"`
ParentID *primitive.ObjectID `json:"parentId,omitempty" bson:"parentid,omitempty"`
CreatedAt time.Time `json:"createdAt" bson:"createdat,omitempty"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedat,omitempty"`
// #bson:ignore
CreatedBy *User `json:"createdBy,omitempty" bson:"-"`
CreatedByID primitive.ObjectID `json:"createdById" bson:"createdById,omitempty"`
CreatedByID primitive.ObjectID `json:"createdById" bson:"createdbyid,omitempty"`
// #bson:ignore
UpdatedBy *User `json:"updatedBy,omitempty" bson:"-"`
UpdatedByID primitive.ObjectID `json:"updatedById" bson:"updatedById,omitempty"`
UpdatedByID primitive.ObjectID `json:"updatedById" bson:"updatedbyid,omitempty"`
// #bson:ignore
Owner *User `json:"owner,omitempty,omitempty" bson:"-"`
OwnerID primitive.ObjectID `json:"ownerId" bson:"ownerId,omitempty"`
OwnerID primitive.ObjectID `json:"ownerId" bson:"ownerid,omitempty"`
}
func (Category) IsBase() {}
@ -53,7 +53,7 @@ func (this Category) GetOwner() *User { return this.Owner }
type CreateCategoryInput struct {
Title []*TranslatedInput `json:"title" bson:"title,omitempty"`
Body []*TranslatedInput `json:"body,omitempty" bson:"body,omitempty"`
ParentID *primitive.ObjectID `json:"parentId,omitempty" bson:"parentId,omitempty"`
ParentID *primitive.ObjectID `json:"parentId,omitempty" bson:"parentid,omitempty"`
}
type CreateTodoInput struct {
@ -75,8 +75,8 @@ type LoginInput struct {
type LoginResponse struct {
User *User `json:"user" bson:"user,omitempty"`
AccessToken string `json:"accessToken" bson:"accessToken,omitempty"`
RefreshToken string `json:"refreshToken" bson:"refreshToken,omitempty"`
AccessToken string `json:"accessToken" bson:"accesstoken,omitempty"`
RefreshToken string `json:"refreshToken" bson:"refreshtoken,omitempty"`
}
type Mutation struct {
@ -92,17 +92,17 @@ type Todo struct {
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
Title *string `json:"title,omitempty" bson:"title,omitempty"`
Completed *bool `json:"completed,omitempty" bson:"completed,omitempty"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt,omitempty"`
CreatedAt time.Time `json:"createdAt" bson:"createdat,omitempty"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedat,omitempty"`
// #bson:ignore
CreatedBy *User `json:"createdBy,omitempty" bson:"-"`
CreatedByID primitive.ObjectID `json:"createdById" bson:"createdById,omitempty"`
CreatedByID primitive.ObjectID `json:"createdById" bson:"createdbyid,omitempty"`
// #bson:ignore
UpdatedBy *User `json:"updatedBy,omitempty" bson:"-"`
UpdatedByID primitive.ObjectID `json:"updatedById" bson:"updatedById,omitempty"`
UpdatedByID primitive.ObjectID `json:"updatedById" bson:"updatedbyid,omitempty"`
// #bson:ignore
Owner *User `json:"owner,omitempty,omitempty" bson:"-"`
OwnerID primitive.ObjectID `json:"ownerId" bson:"ownerId,omitempty"`
OwnerID primitive.ObjectID `json:"ownerId" bson:"ownerid,omitempty"`
}
func (Todo) IsBase() {}
@ -115,13 +115,13 @@ func (this Todo) GetOwner() *User { return this.Owner }
type Translated struct {
Value string `json:"value" bson:"value,omitempty"`
IsPrimary bool `json:"isPrimary" bson:"isPrimary,omitempty"`
IsPrimary bool `json:"isPrimary" bson:"isprimary,omitempty"`
Language string `json:"language" bson:"language,omitempty"`
}
type TranslatedInput struct {
Value string `json:"value" bson:"value,omitempty"`
IsPrimary bool `json:"isPrimary" bson:"isPrimary,omitempty"`
IsPrimary bool `json:"isPrimary" bson:"isprimary,omitempty"`
Language string `json:"language" bson:"language,omitempty"`
}
@ -132,8 +132,8 @@ type User struct {
Type *string `json:"type,omitempty" bson:"type,omitempty"`
Status *string `json:"status,omitempty" bson:"status,omitempty"`
Verified *bool `json:"verified,omitempty" bson:"verified,omitempty"`
Password *string `json:"-" bson:"Password,omitempty"`
Token *string `json:"-" bson:"Token,omitempty"`
Password *string `json:"-" bson:"password,omitempty"`
Token *string `json:"-" bson:"token,omitempty"`
}
type Role string

View File

@ -11,6 +11,10 @@ func MakeHash(password string) (string, error) {
}
func (u *User) CheckPassword(password string) bool {
if u.Password == nil {
return false
}
err := bcrypt.CompareHashAndPassword([]byte(*u.Password), []byte(password))
return err == nil
}