Skip to content

Commit 2bfd96d

Browse files
committed
Add user IPs
1 parent 56a3bee commit 2bfd96d

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE users
2+
DROP COLUMN initial_ip_address;
3+
4+
ALTER TABLE sessions
5+
DROP COLUMN ip_address;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ALTER TABLE users
2+
ADD COLUMN initial_ip_address TEXT NOT NULL DEFAULT '';
3+
4+
UPDATE users
5+
SET initial_ip_address = '';
6+
7+
ALTER TABLE sessions
8+
ADD COLUMN ip_address TEXT NOT NULL DEFAULT '';
9+
10+
UPDATE sessions
11+
SET ip_address = '';

db/queries/session.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- name: InsertSession :exec
2-
INSERT INTO sessions (expires_at, user_agent, token, user_id) VALUES (?, ?, ?, ?);
2+
INSERT INTO sessions (expires_at, user_agent, token, ip_address, user_id) VALUES (?, ?, ?, ?, ?);
33

44
-- name: GetSessionByToken :one
55
SELECT created_at, expires_at, token, user_id FROM sessions WHERE token = ?;

db/queries/user.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- name: InsertUser :one
2-
INSERT INTO users (email, password)
3-
VALUES (?, ?)
2+
INSERT INTO users (email, password, initial_ip_address)
3+
VALUES (?, ?, ?)
44
RETURNING id;
55

66
-- name: GetUserByEmail :one

server/handler/signin.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (h *Handler) Authenticate(w http.ResponseWriter, r *http.Request) {
6767
cookieValue = cookie.Value
6868
}
6969

70-
token, expiresAt, err := h.newSession(r.Context(), user.ID, r.UserAgent(), cookieValue)
70+
token, expiresAt, err := h.newSession(r.Context(), user.ID, r.UserAgent(), cookieValue, r.RemoteAddr)
7171
if err != nil {
7272
h.Logger.Error("failed to create session", "error", err)
7373
h.html(r.Context(), w, http.StatusInternalServerError, components.Alert(types.AlertTypeWarning, "Something went wrong", "Try again later."))
@@ -89,7 +89,7 @@ func (h *Handler) Authenticate(w http.ResponseWriter, r *http.Request) {
8989
w.Header().Set("HX-Redirect", "/")
9090
}
9191

92-
func (h *Handler) newSession(ctx context.Context, userID int64, userAgent string, currentToken string) (string, time.Time, error) {
92+
func (h *Handler) newSession(ctx context.Context, userID int64, userAgent string, currentToken string, ipAddress string) (string, time.Time, error) {
9393
if currentToken != "" {
9494
if err := h.Database.Queries().DeleteSessionByToken(ctx, currentToken); err != nil {
9595
return "", time.Time{}, err
@@ -102,6 +102,7 @@ func (h *Handler) newSession(ctx context.Context, userID int64, userAgent string
102102
Token: token,
103103
ExpiresAt: time.Now().Add(24 * time.Hour),
104104
UserAgent: userAgent,
105+
IpAddress: ipAddress,
105106
}
106107
if err := h.Database.Queries().InsertSession(ctx, session); err != nil {
107108
return "", time.Time{}, err

server/handler/signup.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ func (h *Handler) Register(w http.ResponseWriter, r *http.Request) {
6262
return
6363
}
6464
user := queries.InsertUserParams{
65-
Email: email,
66-
Password: string(hashedPassword),
65+
Email: email,
66+
Password: string(hashedPassword),
67+
InitialIpAddress: r.RemoteAddr,
6768
}
6869
userID, err := h.Database.Queries().InsertUser(r.Context(), user)
6970
if err != nil {

0 commit comments

Comments
 (0)