Skip to content

Commit

Permalink
Added last login information
Browse files Browse the repository at this point in the history
  • Loading branch information
svera committed Jan 6, 2025
1 parent 218d3f1 commit 773b776
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
7 changes: 7 additions & 0 deletions internal/webserver/controller/auth/signin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"log"
"strings"
"time"

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions internal/webserver/embedded/translations/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 2 additions & 0 deletions internal/webserver/embedded/translations/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,5 @@
"Titles by %s": "Titres par %s"
"Send to %s": "Envoyer à %s"
"Home": "Accueil"
"Last login": "Dernière connexion"
"Never": "Jamais"
53 changes: 35 additions & 18 deletions internal/webserver/embedded/views/user/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,44 @@ <h2>{{t $lang "Users"}}</h2>
</a>
</div>
</div>
<div class="list-group">
<table class="table table-striped">
<thead>
<tr>
<th>{{t $lang "Name"}}</th>
<th colspan="2">{{t $lang "Last login"}}</th>
</tr>
</thead>
<tbody>
{{range $i, $user := .Users}}
<div class="list-group-item d-flex justify-content-between align-items-start">
<div>
<a href="/users/{{$user.Username}}">{{$user.Name}}</a>
({{$user.Email}})
<tr>
<td>
<a href="/users/{{$user.Username}}">{{$user.Name}}</a>
({{$user.Email}})

{{if eq $user.Role 2}}
<span class="badge rounded-pill text-bg-secondary">Admin</span>
{{end}}
</div>
{{ if not (and (eq $admins 1) (eq $user.Role 2)) }}
<a href="#" data-bs-toggle="modal" data-bs-target="#delete-modal" data-id="{{$user.Username}}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash3-fill" viewBox="0 0 16 16">
<path d="M11 1.5v1h3.5a.5.5 0 0 1 0 1h-.538l-.853 10.66A2 2 0 0 1 11.115 16h-6.23a2 2 0 0 1-1.994-1.84L2.038 3.5H1.5a.5.5 0 0 1 0-1H5v-1A1.5 1.5 0 0 1 6.5 0h3A1.5 1.5 0 0 1 11 1.5Zm-5 0v1h4v-1a.5.5 0 0 0-.5-.5h-3a.5.5 0 0 0-.5.5ZM4.5 5.029l.5 8.5a.5.5 0 1 0 .998-.06l-.5-8.5a.5.5 0 1 0-.998.06Zm6.53-.528a.5.5 0 0 0-.528.47l-.5 8.5a.5.5 0 0 0 .998.058l.5-8.5a.5.5 0 0 0-.47-.528ZM8 4.5a.5.5 0 0 0-.5.5v8.5a.5.5 0 0 0 1 0V5a.5.5 0 0 0-.5-.5Z"/>
</svg>
</a>
{{end}}
</div>
{{if eq $user.Role 2}}
<span class="badge rounded-pill text-bg-secondary">Admin</span>
{{end}}
</td>
<td>
{{if $user.LastLogin.IsZero }}
{{t $lang "Never"}}
{{else}}
{{$user.LastLogin.Format "2006-01-02 15:04"}}
{{end}}
</td>
<td>
{{ if not (and (eq $admins 1) (eq $user.Role 2)) }}
<a href="#" data-bs-toggle="modal" data-bs-target="#delete-modal" data-id="{{$user.Username}}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash3-fill" viewBox="0 0 16 16">
<path d="M11 1.5v1h3.5a.5.5 0 0 1 0 1h-.538l-.853 10.66A2 2 0 0 1 11.115 16h-6.23a2 2 0 0 1-1.994-1.84L2.038 3.5H1.5a.5.5 0 0 1 0-1H5v-1A1.5 1.5 0 0 1 6.5 0h3A1.5 1.5 0 0 1 11 1.5Zm-5 0v1h4v-1a.5.5 0 0 0-.5-.5h-3a.5.5 0 0 0-.5.5ZM4.5 5.029l.5 8.5a.5.5 0 1 0 .998-.06l-.5-8.5a.5.5 0 1 0-.998.06Zm6.53-.528a.5.5 0 0 0-.528.47l-.5 8.5a.5.5 0 0 0 .998.058l.5-8.5a.5.5 0 0 0-.47-.528ZM8 4.5a.5.5 0 0 0-.5.5v8.5a.5.5 0 0 0 1 0V5a.5.5 0 0 0-.5-.5Z"/>
</svg>
</a>
{{end}}
</td>
</tr>
{{end}}
</div>
</tbody>
</table>

{{ $length := len .Paginator.Pages }} {{ if gt $length 1 }}
{{template "partials/pagination" .}}
Expand Down
1 change: 1 addition & 0 deletions internal/webserver/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 773b776

Please sign in to comment.