Skip to content

Commit

Permalink
Add user IPs
Browse files Browse the repository at this point in the history
  • Loading branch information
Piszmog committed May 9, 2024
1 parent 56a3bee commit 2bfd96d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions db/migrations/20240509034801_user_ips.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE users
DROP COLUMN initial_ip_address;

ALTER TABLE sessions
DROP COLUMN ip_address;
11 changes: 11 additions & 0 deletions db/migrations/20240509034801_user_ips.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ALTER TABLE users
ADD COLUMN initial_ip_address TEXT NOT NULL DEFAULT '';

UPDATE users
SET initial_ip_address = '';

ALTER TABLE sessions
ADD COLUMN ip_address TEXT NOT NULL DEFAULT '';

UPDATE sessions
SET ip_address = '';
2 changes: 1 addition & 1 deletion db/queries/session.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- name: InsertSession :exec
INSERT INTO sessions (expires_at, user_agent, token, user_id) VALUES (?, ?, ?, ?);
INSERT INTO sessions (expires_at, user_agent, token, ip_address, user_id) VALUES (?, ?, ?, ?, ?);

-- name: GetSessionByToken :one
SELECT created_at, expires_at, token, user_id FROM sessions WHERE token = ?;
Expand Down
4 changes: 2 additions & 2 deletions db/queries/user.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- name: InsertUser :one
INSERT INTO users (email, password)
VALUES (?, ?)
INSERT INTO users (email, password, initial_ip_address)
VALUES (?, ?, ?)
RETURNING id;

-- name: GetUserByEmail :one
Expand Down
5 changes: 3 additions & 2 deletions server/handler/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (h *Handler) Authenticate(w http.ResponseWriter, r *http.Request) {
cookieValue = cookie.Value
}

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

func (h *Handler) newSession(ctx context.Context, userID int64, userAgent string, currentToken string) (string, time.Time, error) {
func (h *Handler) newSession(ctx context.Context, userID int64, userAgent string, currentToken string, ipAddress string) (string, time.Time, error) {
if currentToken != "" {
if err := h.Database.Queries().DeleteSessionByToken(ctx, currentToken); err != nil {
return "", time.Time{}, err
Expand All @@ -102,6 +102,7 @@ func (h *Handler) newSession(ctx context.Context, userID int64, userAgent string
Token: token,
ExpiresAt: time.Now().Add(24 * time.Hour),
UserAgent: userAgent,
IpAddress: ipAddress,
}
if err := h.Database.Queries().InsertSession(ctx, session); err != nil {
return "", time.Time{}, err
Expand Down
5 changes: 3 additions & 2 deletions server/handler/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ func (h *Handler) Register(w http.ResponseWriter, r *http.Request) {
return
}
user := queries.InsertUserParams{
Email: email,
Password: string(hashedPassword),
Email: email,
Password: string(hashedPassword),
InitialIpAddress: r.RemoteAddr,
}
userID, err := h.Database.Queries().InsertUser(r.Context(), user)
if err != nil {
Expand Down

0 comments on commit 2bfd96d

Please sign in to comment.