-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add delivery person login functionality
Implemented login endpoint for delivery personnel. Added context-based validation and JWT generation for secure authentication. Updated delivery models and routes to support the new login feature.
- Loading branch information
1 parent
ffe7840
commit 5b0e268
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package delivery | ||
|
||
import ( | ||
"Go_Food_Delivery/pkg/database/models/delivery" | ||
"context" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func (s *DeliveryHandler) loginDelivery(c *gin.Context) { | ||
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second) | ||
defer cancel() | ||
var token string | ||
var deliverLoginPerson delivery.DeliveryLoginParams | ||
|
||
if err := c.BindJSON(&deliverLoginPerson); err != nil { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid request"}) | ||
return | ||
} | ||
|
||
verify := s.service.Verify(ctx, deliverLoginPerson.Phone, deliverLoginPerson.OTP) | ||
if !verify { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Either Phone or OTP is incorrect or user is inactive. Please contact administrator."}) | ||
return | ||
} else { | ||
deliveryLoginDetails, err := s.service.ValidateAccountDetails(ctx, deliverLoginPerson.Phone) | ||
if err != nil { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unable to fetch delivery person details. Please contact administrator."}) | ||
return | ||
} | ||
token, err = s.service.GenerateJWT(ctx, deliveryLoginDetails.DeliveryPersonID, deliveryLoginDetails.Name) | ||
if err != nil { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unable to generate login information. Please contact administrator."}) | ||
return | ||
} | ||
|
||
} | ||
|
||
c.JSON(http.StatusCreated, gin.H{"token": token}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package delivery | ||
|
||
import ( | ||
"Go_Food_Delivery/cmd/api/middleware" | ||
"Go_Food_Delivery/pkg/database/models/delivery" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/pquerna/otp/totp" | ||
"log/slog" | ||
"os" | ||
"time" | ||
) | ||
|
||
func (deliverSrv *DeliveryService) ValidateAccountDetails(ctx context.Context, phone string) (*delivery.DeliveryPerson, error) { | ||
var deliveryAccountInfo delivery.DeliveryPerson | ||
err := deliverSrv.db.Select(ctx, &deliveryAccountInfo, "phone", phone) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if deliveryAccountInfo.Status != "AVAILABLE" { | ||
return nil, errors.New("account is inactive or not available") | ||
} | ||
fmt.Printf("%+v", deliveryAccountInfo) | ||
return &deliveryAccountInfo, nil | ||
} | ||
|
||
func (deliverSrv *DeliveryService) ValidateOTP(_ context.Context, secretKey string, otp string) bool { | ||
return totp.Validate(otp, secretKey) | ||
} | ||
|
||
func (deliverSrv *DeliveryService) Verify(ctx context.Context, phone string, otp string) bool { | ||
accDetail, err := deliverSrv.ValidateAccountDetails(ctx, phone) | ||
if err != nil { | ||
slog.Error("Error::validating account details", "err", err) | ||
return false | ||
} | ||
|
||
valid := deliverSrv.ValidateOTP(ctx, accDetail.AuthKey, otp) | ||
return valid | ||
} | ||
|
||
func (deliverSrv *DeliveryService) GenerateJWT(_ context.Context, userId int64, name string) (string, error) { | ||
|
||
claims := middleware.UserClaims{UserID: userId, Name: name, | ||
RegisteredClaims: jwt.RegisteredClaims{ | ||
|
||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * time.Duration(2))), | ||
Issuer: "Go_Food_Delivery", | ||
}} | ||
|
||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
return token.SignedString([]byte(os.Getenv("JWT_SECRET_KEY"))) | ||
} |