From 773b7764441c55ad3a67ea2dcd8d31d8fc0c1d2f Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Mon, 6 Jan 2025 21:08:00 +0100 Subject: [PATCH] Added last login information --- internal/webserver/controller/auth/signin.go | 7 +++ .../webserver/embedded/translations/es.yml | 2 + .../webserver/embedded/translations/fr.yml | 2 + .../webserver/embedded/views/user/index.html | 53 ++++++++++++------- internal/webserver/model/user.go | 1 + 5 files changed, 47 insertions(+), 18 deletions(-) diff --git a/internal/webserver/controller/auth/signin.go b/internal/webserver/controller/auth/signin.go index e881bb9..438792b 100644 --- a/internal/webserver/controller/auth/signin.go +++ b/internal/webserver/controller/auth/signin.go @@ -1,6 +1,7 @@ package auth import ( + "log" "strings" "time" @@ -37,6 +38,12 @@ func (a *Controller) SignIn(c *fiber.Ctx) error { }, "layout") } + user.LastLogin = time.Now() + if err := a.repository.Update(user); err != nil { + log.Printf("error updating user last login time: %v\n", err) + return fiber.ErrInternalServerError + } + // Send back JWT as a cookie. expiration := time.Now().Add(a.config.SessionTimeout) signedToken, err := GenerateToken(c, user, expiration, a.config.Secret) diff --git a/internal/webserver/embedded/translations/es.yml b/internal/webserver/embedded/translations/es.yml index 1a15858..a1627f5 100644 --- a/internal/webserver/embedded/translations/es.yml +++ b/internal/webserver/embedded/translations/es.yml @@ -138,3 +138,5 @@ "Titles by %s": "Títulos de %s" "Send to %s": "Enviar a %s" "Home": "Inicio" +"Last login": "Último Acceso" +"Never": "Nunca" diff --git a/internal/webserver/embedded/translations/fr.yml b/internal/webserver/embedded/translations/fr.yml index 5e59128..5e595a5 100644 --- a/internal/webserver/embedded/translations/fr.yml +++ b/internal/webserver/embedded/translations/fr.yml @@ -138,3 +138,5 @@ "Titles by %s": "Titres par %s" "Send to %s": "Envoyer à %s" "Home": "Accueil" +"Last login": "Dernière connexion" +"Never": "Jamais" diff --git a/internal/webserver/embedded/views/user/index.html b/internal/webserver/embedded/views/user/index.html index 0b2d602..72cb74c 100644 --- a/internal/webserver/embedded/views/user/index.html +++ b/internal/webserver/embedded/views/user/index.html @@ -14,27 +14,44 @@

{{t $lang "Users"}}

-
+ + + + + + + + {{range $i, $user := .Users}} -
-
- {{$user.Name}} - ({{$user.Email}}) +
+ + + + {{end}} - + +
{{t $lang "Name"}}{{t $lang "Last login"}}
+ {{$user.Name}} + ({{$user.Email}}) - {{if eq $user.Role 2}} - Admin - {{end}} - - {{ if not (and (eq $admins 1) (eq $user.Role 2)) }} - - - - - - {{end}} - + {{if eq $user.Role 2}} + Admin + {{end}} + + {{if $user.LastLogin.IsZero }} + {{t $lang "Never"}} + {{else}} + {{$user.LastLogin.Format "2006-01-02 15:04"}} + {{end}} + + {{ if not (and (eq $admins 1) (eq $user.Role 2)) }} + + + + + + {{end}} +
{{ $length := len .Paginator.Pages }} {{ if gt $length 1 }} {{template "partials/pagination" .}} diff --git a/internal/webserver/model/user.go b/internal/webserver/model/user.go index 7692c4d..5c2c784 100644 --- a/internal/webserver/model/user.go +++ b/internal/webserver/model/user.go @@ -29,6 +29,7 @@ type User struct { RecoveryUUID string RecoveryValidUntil time.Time Highlights []Highlight `gorm:"constraint:OnDelete:CASCADE"` + LastLogin time.Time } // Validate checks all user's fields to ensure they are in the required format