From 24f225f68bafc2b78d05182cc82dff3445d69e67 Mon Sep 17 00:00:00 2001 From: optclblast Date: Sun, 27 Oct 2024 12:44:05 -0400 Subject: [PATCH] repo updates --- internal/storage/models/auth.go | 8 ++++++-- internal/storage/postgres/database.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/internal/storage/models/auth.go b/internal/storage/models/auth.go index 38327b3..5d0ae4f 100644 --- a/internal/storage/models/auth.go +++ b/internal/storage/models/auth.go @@ -3,8 +3,12 @@ package models import "time" type Session struct { - ID string - User *User + ID string + SessionToken string + CsrfToken string + User *User + CreatedAt time.Time + ExpiredAt time.Time } type User struct { diff --git a/internal/storage/postgres/database.go b/internal/storage/postgres/database.go index d3bbf03..10f5698 100644 --- a/internal/storage/postgres/database.go +++ b/internal/storage/postgres/database.go @@ -49,6 +49,10 @@ func (d *Database) GetUserByLogin(ctx context.Context, login string) (*models.Us return getUserByLogin(ctx, d.db, login) } +func (d *Database) AddSession(ctx context.Context, ses *models.Session) (uint64, error) { + return addSession(ctx, d.db, ses) +} + func addUser(ctx context.Context, conn dbtx, login string, username string, passwordHash []byte) (uint64, error) { const stmt = `INSERT INTO users (login,username,password) VALUES ($1,$2,$3) RETURNING id` @@ -87,3 +91,15 @@ func getUserByLogin(ctx context.Context, conn dbtx, login string) (*models.User, return u, nil } + +func addSession(ctx context.Context, conn dbtx, session *models.Session) (uint64, error) { + const stmt = `INSERT INTO sessions (session_token, csrf_token, user_id, + created_at, expired_at) VALUES ($1, $2, $3, $4, $5) RETURNING id;` + var id uint64 + row := conn.QueryRow(ctx, stmt, session.SessionToken, session.CsrfToken, session.User.ID, session.CreatedAt, session.ExpiredAt) + if err := row.Scan(&id); err != nil { + return 0, fmt.Errorf("failed to insert new session: %w", err) + } + + return id, nil +}