Remember last login
This commit is contained in:
parent
1437fc7b8d
commit
e3cf69ab08
@ -82,9 +82,9 @@ func (h *Handler) loginPost(c *gin.Context) {
|
|||||||
c.AbortWithError(http.StatusUnauthorized, err)
|
c.AbortWithError(http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, _ = h.Service.DB.UpdateLastLogin(context.Background(), user.ID)
|
||||||
maxAge := (int)((expiration * time.Hour).Seconds())
|
maxAge := (int)((expiration * time.Hour).Seconds())
|
||||||
c.SetCookie(authCookie, t, maxAge, "", "", false, true)
|
c.SetCookie(authCookie, t, maxAge, "", "", false, true)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, map[string]string{
|
c.JSON(http.StatusOK, map[string]string{
|
||||||
"token": t,
|
"token": t,
|
||||||
})
|
})
|
||||||
|
@ -50,10 +50,11 @@ type Transaction struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
Email string
|
Email string
|
||||||
Name string
|
Name string
|
||||||
Password string
|
Password string
|
||||||
|
LastLogin sql.NullTime
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserBudget struct {
|
type UserBudget struct {
|
||||||
|
@ -10,4 +10,10 @@ WHERE id = $1;
|
|||||||
INSERT INTO users
|
INSERT INTO users
|
||||||
(email, name, password)
|
(email, name, password)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2, $3)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: UpdateLastLogin :one
|
||||||
|
UPDATE users
|
||||||
|
SET last_login = NOW()
|
||||||
|
WHERE users.id = $1
|
||||||
RETURNING *;
|
RETURNING *;
|
@ -9,7 +9,8 @@ CREATE TABLE users (
|
|||||||
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
|
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
|
||||||
email text NOT NULL,
|
email text NOT NULL,
|
||||||
name 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 (
|
CREATE TABLE user_budgets (
|
||||||
|
@ -13,7 +13,7 @@ const createUser = `-- name: CreateUser :one
|
|||||||
INSERT INTO users
|
INSERT INTO users
|
||||||
(email, name, password)
|
(email, name, password)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2, $3)
|
||||||
RETURNING id, email, name, password
|
RETURNING id, email, name, password, last_login
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateUserParams struct {
|
type CreateUserParams struct {
|
||||||
@ -30,12 +30,13 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
|
|||||||
&i.Email,
|
&i.Email,
|
||||||
&i.Name,
|
&i.Name,
|
||||||
&i.Password,
|
&i.Password,
|
||||||
|
&i.LastLogin,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getUser = `-- name: GetUser :one
|
const getUser = `-- name: GetUser :one
|
||||||
SELECT id, email, name, password FROM users
|
SELECT id, email, name, password, last_login FROM users
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
@ -47,12 +48,13 @@ func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) {
|
|||||||
&i.Email,
|
&i.Email,
|
||||||
&i.Name,
|
&i.Name,
|
||||||
&i.Password,
|
&i.Password,
|
||||||
|
&i.LastLogin,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getUserByUsername = `-- name: GetUserByUsername :one
|
const getUserByUsername = `-- name: GetUserByUsername :one
|
||||||
SELECT id, email, name, password FROM users
|
SELECT id, email, name, password, last_login FROM users
|
||||||
WHERE email = $1
|
WHERE email = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
@ -64,6 +66,27 @@ func (q *Queries) GetUserByUsername(ctx context.Context, email string) (User, er
|
|||||||
&i.Email,
|
&i.Email,
|
||||||
&i.Name,
|
&i.Name,
|
||||||
&i.Password,
|
&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
|
return i, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user