Remember last login

This commit is contained in:
Jan Bader 2021-12-06 20:38:55 +00:00
parent 1437fc7b8d
commit e3cf69ab08
5 changed files with 40 additions and 9 deletions

View File

@ -82,9 +82,9 @@ func (h *Handler) loginPost(c *gin.Context) {
c.AbortWithError(http.StatusUnauthorized, err)
}
_, _ = h.Service.DB.UpdateLastLogin(context.Background(), user.ID)
maxAge := (int)((expiration * time.Hour).Seconds())
c.SetCookie(authCookie, t, maxAge, "", "", false, true)
c.JSON(http.StatusOK, map[string]string{
"token": t,
})

View File

@ -50,10 +50,11 @@ type Transaction struct {
}
type User struct {
ID uuid.UUID
Email string
Name string
Password string
ID uuid.UUID
Email string
Name string
Password string
LastLogin sql.NullTime
}
type UserBudget struct {

View File

@ -11,3 +11,9 @@ INSERT INTO users
(email, name, password)
VALUES ($1, $2, $3)
RETURNING *;
-- name: UpdateLastLogin :one
UPDATE users
SET last_login = NOW()
WHERE users.id = $1
RETURNING *;

View File

@ -9,7 +9,8 @@ CREATE TABLE users (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
email text NOT NULL,
name text NOT NULL,
password text NOT NULL
password text NOT NULL,
last_login timestamp with time zone
);
CREATE TABLE user_budgets (

View File

@ -13,7 +13,7 @@ const createUser = `-- name: CreateUser :one
INSERT INTO users
(email, name, password)
VALUES ($1, $2, $3)
RETURNING id, email, name, password
RETURNING id, email, name, password, last_login
`
type CreateUserParams struct {
@ -30,12 +30,13 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
&i.Email,
&i.Name,
&i.Password,
&i.LastLogin,
)
return i, err
}
const getUser = `-- name: GetUser :one
SELECT id, email, name, password FROM users
SELECT id, email, name, password, last_login FROM users
WHERE id = $1
`
@ -47,12 +48,13 @@ func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) {
&i.Email,
&i.Name,
&i.Password,
&i.LastLogin,
)
return i, err
}
const getUserByUsername = `-- name: GetUserByUsername :one
SELECT id, email, name, password FROM users
SELECT id, email, name, password, last_login FROM users
WHERE email = $1
`
@ -64,6 +66,27 @@ func (q *Queries) GetUserByUsername(ctx context.Context, email string) (User, er
&i.Email,
&i.Name,
&i.Password,
&i.LastLogin,
)
return i, err
}
const updateLastLogin = `-- name: UpdateLastLogin :one
UPDATE users
SET last_login = NOW()
WHERE users.id = $1
RETURNING id, email, name, password, last_login
`
func (q *Queries) UpdateLastLogin(ctx context.Context, id uuid.UUID) (User, error) {
row := q.db.QueryRowContext(ctx, updateLastLogin, id)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.Name,
&i.Password,
&i.LastLogin,
)
return i, err
}