diff --git a/.env b/.env index 8a36010..8567e45 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ -PORT=8000 +PORT=1000 SECRET=sdc87sdf6gv87df6v8sd7f6v8sdf76v8sdf7v6 DB_URL="host=dumbo.db.elephantsql.com user=yvvpeiee password=QwAhcTtiKL8j9qRyUBIqs9kNbu3O0_FB dbname=yvvpeiee port=5432 sslmode=disable" \ No newline at end of file diff --git a/controllers/usersController.go b/controllers/usersController.go index 389f30c..3accc16 100644 --- a/controllers/usersController.go +++ b/controllers/usersController.go @@ -107,8 +107,6 @@ func LogIn(c *gin.Context) { user.LastLoginAt = time.Now() initializers.DB.Save(&user) - updateLoginRecord(c, user.ID, time.Now()) - token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ "sub": user.ID, "exp": time.Now().Add(time.Hour * 24 * 30).Unix(), @@ -127,8 +125,11 @@ func LogIn(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "token": tokenString, }) + + updateLoginRecord(c, user.ID, time.Now()) } + func LogOut(c *gin.Context) { c.SetCookie("Authorization", "", -1, "/", "", false, true) @@ -145,6 +146,7 @@ func Validate(c *gin.Context) { }) } + func updateLoginRecord(c *gin.Context, userID uint, date time.Time) { // Cari data rekap login berdasarkan tanggal var loginRecord models.LoginRecord @@ -157,16 +159,14 @@ func updateLoginRecord(c *gin.Context, userID uint, date time.Time) { if result.Error == gorm.ErrRecordNotFound { // Jika belum ada data rekap untuk hari ini, buat data baru loginRecord = models.LoginRecord{ - LoginDate: date, - TotalUsers: 1, - UserIDs: fmt.Sprintf("%d", userID), + LoginDate: date, + UserIDs: fmt.Sprintf("%d", userID), } initializers.DB.Create(&loginRecord) } else { // Jika sudah ada data rekap, cek apakah user sudah login pada hari ini if !containsInt(loginRecord.UserIDs, userID) { // Jika user belum login hari ini, tambahkan ID pengguna ke dalam array user_ids - loginRecord.TotalUsers++ loginRecord.UserIDs = fmt.Sprintf("%s,%d", loginRecord.UserIDs, userID) initializers.DB.Save(&loginRecord) } @@ -179,10 +179,13 @@ func updateLoginRecord(c *gin.Context, userID uint, date time.Time) { // Update login record dengan pengguna yang login pada hari ini loginRecord.Users = users - // Update jumlah pengguna (TotalUsers) saat pengguna baru melakukan login - loginRecord.TotalUsers = len(users) - initializers.DB.Save(&loginRecord) + + // Update LastLoginAt pada tabel User jika dalam satu hari user melakukan login kembali + user := models.User{} + initializers.DB.First(&user, userID) + user.LastLoginAt = date + initializers.DB.Save(&user) } diff --git a/go-crud.exe b/go-crud.exe index c39b2ed..436d323 100644 Binary files a/go-crud.exe and b/go-crud.exe differ diff --git a/go-crud.exe~ b/go-crud.exe~ index 31f4b0f..a871486 100644 Binary files a/go-crud.exe~ and b/go-crud.exe~ differ diff --git a/main.go b/main.go index 2647c4c..15129d2 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ func main() { r.GET("/validate", middleware.RequireAuth, controllers.Validate) r.Use(middleware.RequireAuth) + r.POST("/logout", controllers.LogOut) r.POST("/posts", controllers.PostsCreate) r.PUT("/posts/:id", controllers.PostsUpdate) r.GET("/posts", controllers.PostsIndex) diff --git a/middleware/requireAuth.go b/middleware/requireAuth.go index abbdc1b..47be13a 100644 --- a/middleware/requireAuth.go +++ b/middleware/requireAuth.go @@ -52,6 +52,9 @@ func RequireAuth(c *gin.Context) { var user models.User initializers.DB.First(&user, claims["sub"]) + var recordData models.LoginRecord + initializers.DB.First(&recordData, claims["sub"]) + var post models.Post initializers.DB.First(&post, claims["sub"]) diff --git a/models/loginRecord.go b/models/recordData.go similarity index 92% rename from models/loginRecord.go rename to models/recordData.go index 21691df..22eabd8 100644 --- a/models/loginRecord.go +++ b/models/recordData.go @@ -9,7 +9,6 @@ import ( type LoginRecord struct { gorm.Model LoginDate time.Time - TotalUsers int UserIDs string `gorm:"type:text"` Users []User `json:"users" gorm:"-"` }