1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-11 17:51:01 +00:00

Use SQL transaction when creating user sessions

This commit is contained in:
Frédéric Guillot 2020-12-22 20:28:34 -08:00
parent 7be9f5989e
commit 60a7362327
4 changed files with 27 additions and 11 deletions

View file

@ -55,21 +55,37 @@ func (s *Storage) UserSessions(userID int64) (model.UserSessions, error) {
return sessions, nil
}
// CreateUserSession creates a new sessions.
func (s *Storage) CreateUserSession(username, userAgent, ip string) (sessionID string, userID int64, err error) {
query := `SELECT id FROM users WHERE username = LOWER($1)`
err = s.db.QueryRow(query, username).Scan(&userID)
// CreateUserSessionFromUsername creates a new user session.
func (s *Storage) CreateUserSessionFromUsername(username, userAgent, ip string) (sessionID string, userID int64, err error) {
token := crypto.GenerateRandomString(64)
tx, err := s.db.Begin()
if err != nil {
return "", 0, fmt.Errorf(`store: unable to start transaction: %v`, err)
}
err = tx.QueryRow(`SELECT id FROM users WHERE username = LOWER($1)`, username).Scan(&userID)
if err != nil {
tx.Rollback()
return "", 0, fmt.Errorf(`store: unable to fetch user ID: %v`, err)
}
token := crypto.GenerateRandomString(64)
query = `INSERT INTO user_sessions (token, user_id, user_agent, ip) VALUES ($1, $2, $3, $4)`
_, err = s.db.Exec(query, token, userID, userAgent, ip)
_, err = tx.Exec(
`INSERT INTO user_sessions (token, user_id, user_agent, ip) VALUES ($1, $2, $3, $4)`,
token,
userID,
userAgent,
ip,
)
if err != nil {
tx.Rollback()
return "", 0, fmt.Errorf(`store: unable to create user session: %v`, err)
}
if err := tx.Commit(); err != nil {
return "", 0, fmt.Errorf(`store: unable to commit transaction: %v`, err)
}
return token, userID, nil
}