|
|
|
@ -177,27 +177,47 @@ func LogIn(c *gin.Context) {
|
|
|
|
|
} |
|
|
|
|
if err := c.ShouldBindJSON(&body); err != nil { |
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{ |
|
|
|
|
"error": "Failed to read body", |
|
|
|
|
"code": http.StatusBadRequest, |
|
|
|
|
"data": nil, |
|
|
|
|
"message": "Failed to read body", |
|
|
|
|
"status": false, |
|
|
|
|
}) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
var user models.Post |
|
|
|
|
initializers.DB.First(&user, "email = ?", body.Email) |
|
|
|
|
if user.ID == 0 { |
|
|
|
|
result := initializers.DB.First(&user, "email = ?", body.Email) |
|
|
|
|
if result.Error != nil { |
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{ |
|
|
|
|
"error": "Invalid email or password", |
|
|
|
|
"code": http.StatusBadRequest, |
|
|
|
|
"data": nil, |
|
|
|
|
"message": "Invalid email or password", |
|
|
|
|
"status": false, |
|
|
|
|
}) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(body.Password)) |
|
|
|
|
if err != nil { |
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{ |
|
|
|
|
"error": "Invalid email or password", |
|
|
|
|
"code": http.StatusBadRequest, |
|
|
|
|
"data": nil, |
|
|
|
|
"message": "Invalid email or password", |
|
|
|
|
"status": false, |
|
|
|
|
}) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
user.LastLoginAt = time.Now() |
|
|
|
|
initializers.DB.Save(&user) |
|
|
|
|
result = initializers.DB.Save(&user) |
|
|
|
|
if result.Error != nil { |
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{ |
|
|
|
|
"code": http.StatusInternalServerError, |
|
|
|
|
"data": nil, |
|
|
|
|
"message": "Failed to update user data", |
|
|
|
|
"status": false, |
|
|
|
|
}) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updateLoginRecord(c, user.ID, time.Now()) |
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ |
|
|
|
|
"sub": user.ID, |
|
|
|
@ -206,16 +226,38 @@ func LogIn(c *gin.Context) {
|
|
|
|
|
tokenString, err := token.SignedString([]byte(os.Getenv("SECRET"))) |
|
|
|
|
if err != nil { |
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{ |
|
|
|
|
"error": "Failed to create token", |
|
|
|
|
"code": http.StatusBadRequest, |
|
|
|
|
"data": nil, |
|
|
|
|
"message": "Failed to create token", |
|
|
|
|
"status": false, |
|
|
|
|
}) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
c.SetSameSite(http.SameSiteLaxMode) |
|
|
|
|
c.SetCookie("Authorization", tokenString, 3600*24*30, "", "", false, true) |
|
|
|
|
|
|
|
|
|
// Menghitung total record setelah operasi save
|
|
|
|
|
var totalRecord int64 |
|
|
|
|
result = initializers.DB.Model(models.Post{}).Count(&totalRecord) |
|
|
|
|
if result.Error != nil { |
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{ |
|
|
|
|
"code": http.StatusInternalServerError, |
|
|
|
|
"data": nil, |
|
|
|
|
"message": "Failed to fetch total record", |
|
|
|
|
"status": false, |
|
|
|
|
}) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{ |
|
|
|
|
"token": tokenString, |
|
|
|
|
"code": http.StatusOK, |
|
|
|
|
"data": user, |
|
|
|
|
"message": "OK", |
|
|
|
|
"status": true, |
|
|
|
|
"totalRecord": totalRecord, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func LogOut(c *gin.Context) { |
|
|
|
|
c.SetCookie("Authorization", "", -1, "/", "", false, true) |
|
|
|
|
c.JSON(http.StatusOK, gin.H{ |
|
|
|
|