Add Device Token Support for Customer and User Entities
- Introduced a new DeviceToken field in both Customer and User entities to store device tokens for authentication and notifications. - Updated LoginForm structures for both Customer and User to include an optional DeviceToken field, allowing clients to send device tokens during login. - Enhanced the Login methods in the customer and user services to append the device token to the respective entities if it does not already exist, improving user session management and notification capabilities. - Ensured proper validation and structured responses for the new DeviceToken field in the forms, maintaining compliance with best practices for API design.
This commit is contained in:
@@ -38,6 +38,7 @@ type User struct {
|
||||
Position *string `bson:"position"`
|
||||
Phone *string `bson:"phone"`
|
||||
ProfileImage *string `bson:"profile_image"`
|
||||
DeviceToken []string `bson:"device_token"`
|
||||
IsVerified bool `bson:"is_verified"`
|
||||
LastLoginAt *int64 `bson:"last_login_at"`
|
||||
}
|
||||
|
||||
@@ -33,8 +33,9 @@ type UpdateUserForm struct {
|
||||
|
||||
// LoginForm represents the credentials required for user authentication
|
||||
type LoginForm struct {
|
||||
Username string `json:"username" valid:"required" example:"admin"` // Username or email address
|
||||
Password string `json:"password" valid:"required" example:"Admin!1234"` // User password
|
||||
Username string `json:"username" valid:"required" example:"admin"` // Username or email address
|
||||
Password string `json:"password" valid:"required" example:"Admin!1234"` // User password
|
||||
DeviceToken string `json:"device_token" valid:"optional" example:"device_token"` // Device token
|
||||
}
|
||||
|
||||
// RefreshTokenForm represents the refresh token required to generate new access tokens
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
"tm/pkg/authorization"
|
||||
@@ -311,6 +312,11 @@ func (s *userService) Login(ctx context.Context, form *LoginForm) (*AuthResponse
|
||||
return nil, errors.New("invalid credentials")
|
||||
}
|
||||
|
||||
// append device token to user if not exists
|
||||
if !slices.Contains(user.DeviceToken, form.DeviceToken) {
|
||||
user.DeviceToken = append(user.DeviceToken, form.DeviceToken)
|
||||
}
|
||||
|
||||
// Update last login
|
||||
err = s.repository.Login(ctx, user.ID.Hex())
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user