|
|
|
@ -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) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|