go-mongo/services/auth/auth.go
2025-05-31 18:38:18 +03:00

37 lines
715 B
Go

package authService
import (
"context"
"fmt"
"github.com/farahty/go-mongo/app"
"github.com/farahty/go-mongo/models"
"go.mongodb.org/mongo-driver/bson"
)
func Login(ctx context.Context, loginInput *models.LoginInput) (*models.LoginResponse, error) {
// todo : fix the security threats here
filter := bson.D{
{
Key: "$or",
Value: bson.A{
bson.D{{Key: "phone", Value: loginInput.Identity}},
bson.D{{Key: "email", Value: loginInput.Identity}},
},
},
}
user, err := app.FindOne[models.User](ctx, "users", filter)
if err != nil {
return nil, err
}
if !user.CheckPassword(loginInput.Password) {
return nil, fmt.Errorf("incorrect password")
}
return successLogin(ctx, user)
}