From 8ad698bf16b18e0dc4e33ea648b79d072a8e02d1 Mon Sep 17 00:00:00 2001 From: shani Date: Sun, 19 May 2024 16:14:01 +0700 Subject: [PATCH 1/2] fix stuff --- db/migrations/20240501171902_add_users_table.up.sql | 2 +- src/handler/auth_handler.go | 5 +++-- src/handler/nurse_handler.go | 4 +--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/db/migrations/20240501171902_add_users_table.up.sql b/db/migrations/20240501171902_add_users_table.up.sql index dd76b56..e6a225a 100644 --- a/db/migrations/20240501171902_add_users_table.up.sql +++ b/db/migrations/20240501171902_add_users_table.up.sql @@ -1,6 +1,6 @@ CREATE TABLE users ( user_id VARCHAR(255) PRIMARY KEY, - nip NUMERIC(13) UNIQUE NOT NULL, + nip NUMERIC(15) UNIQUE NOT NULL, name VARCHAR(255) NOT NULL, identity_card_scan_img VARCHAR(2083), password VARCHAR(255) NOT NULL, diff --git a/src/handler/auth_handler.go b/src/handler/auth_handler.go index 9c8ec6b..bc6f6ca 100644 --- a/src/handler/auth_handler.go +++ b/src/handler/auth_handler.go @@ -161,7 +161,7 @@ func (h *AuthHandler) LoginNurse(c *gin.Context) { func ValidateRegisterRequest(nip int64, name, password string) error { // Validate email format if !isValidNip(nip) { - return errors.New("nip must be in valid email format") + return errors.New("nip invalid") } // Validate name length @@ -210,7 +210,8 @@ func ValidateLoginNurseRequest(nip int64, password string) error { // TODO fix to the correct nip validation func isValidNip(nip int64) bool { // Regular expression pattern for email format - nipRegex := `^615[12][2-9][0-9]{3}(0[1-9]|1[0-2])[0-9]{3}$` + nipRegex := `^615[12](200[0-9]|201[0-9]|202[0-4])(0[1-9]|1[0-2])\d{3,5}$` + match, _ := regexp.MatchString(nipRegex, strconv.FormatInt(nip, 10)) return match } diff --git a/src/handler/nurse_handler.go b/src/handler/nurse_handler.go index c2380ad..0938c64 100644 --- a/src/handler/nurse_handler.go +++ b/src/handler/nurse_handler.go @@ -9,7 +9,6 @@ import ( "net/http" "regexp" "strconv" - "time" "github.com/gin-gonic/gin" ) @@ -230,8 +229,7 @@ func ValidateRegisterNurseRequest(nip int64, name string) error { } func isValidNipNurse(nip int64) bool { - currentYear := time.Now().Year() - nipRegex := fmt.Sprintf(`^303[12](200[0-%d]|20[01][0-9]|202[0-%d])(0[1-9]|1[0-2])[0-9]{3}$`, currentYear%10, currentYear%10) + nipRegex := fmt.Sprintf(`^303[12](200[0-9]|201[0-9]|202[0-4])(0[1-9]|1[0-2])\d{3,5}$`) // Convert the nip int64 to a string nipStr := strconv.FormatInt(nip, 10) // Match the string with the regex From 1f333485b6888d664c253c7d1002f51ae9898ff9 Mon Sep 17 00:00:00 2001 From: shani Date: Sun, 19 May 2024 16:31:17 +0700 Subject: [PATCH 2/2] fix stuff --- src/handler/auth_handler.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/handler/auth_handler.go b/src/handler/auth_handler.go index bc6f6ca..553cd71 100644 --- a/src/handler/auth_handler.go +++ b/src/handler/auth_handler.go @@ -30,6 +30,12 @@ func (h *AuthHandler) Register(c *gin.Context) { } // Validate request payload + if(request.Nip == 0) { + log.Println("Register bad request > invalid NIP") + c.JSON(400, gin.H{"status": "bad request", "message": "invalid NIP"}) + return + } + err = ValidateRegisterRequest(request.Nip, request.Name, request.Password) if err != nil { log.Println("Register bad request ", err) @@ -72,6 +78,12 @@ func (h *AuthHandler) Login(c *gin.Context) { c.JSON(400, gin.H{"status": "bad request", "message": err}) return } + + if(request.Nip == 0) { + log.Println("Register bad request > invalid NIP") + c.JSON(400, gin.H{"status": "bad request", "message": "invalid NIP"}) + return + } nStr := strconv.FormatInt(request.Nip, 10) if !strings.HasPrefix(nStr, "615") { @@ -105,7 +117,8 @@ func (h *AuthHandler) Login(c *gin.Context) { "data": gin.H{ "nip": userData.Nip, "name": userData.Name, - "accessToken": token, + "accessToken": token, + "userId": userData.Id, }, }) } @@ -158,7 +171,7 @@ func (h *AuthHandler) LoginNurse(c *gin.Context) { } // ValidateRegisterRequest validates the register user request payload -func ValidateRegisterRequest(nip int64, name, password string) error { +func ValidateRegisterRequest(nip int64, name string, password string) error { // Validate email format if !isValidNip(nip) { return errors.New("nip invalid")