From e3cf69ab085cd044d70f451b5dfc4470db9f496a Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Mon, 6 Dec 2021 20:38:55 +0000 Subject: [PATCH] Remember last login --- http/session.go | 2 +- postgres/models.go | 9 ++++---- postgres/queries/users.sql | 6 +++++ postgres/schema/202112021109_initial.sql | 3 ++- postgres/users.sql.go | 29 +++++++++++++++++++++--- 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/http/session.go b/http/session.go index b53b4cd..783f3ca 100644 --- a/http/session.go +++ b/http/session.go @@ -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, }) diff --git a/postgres/models.go b/postgres/models.go index e3cc9a5..007df77 100644 --- a/postgres/models.go +++ b/postgres/models.go @@ -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 { diff --git a/postgres/queries/users.sql b/postgres/queries/users.sql index e79393e..8cf5bca 100644 --- a/postgres/queries/users.sql +++ b/postgres/queries/users.sql @@ -10,4 +10,10 @@ WHERE id = $1; 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 *; \ No newline at end of file diff --git a/postgres/schema/202112021109_initial.sql b/postgres/schema/202112021109_initial.sql index a97e085..baea6ff 100644 --- a/postgres/schema/202112021109_initial.sql +++ b/postgres/schema/202112021109_initial.sql @@ -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 ( diff --git a/postgres/users.sql.go b/postgres/users.sql.go index 75fb84a..bd295b2 100644 --- a/postgres/users.sql.go +++ b/postgres/users.sql.go @@ -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 }