Compare commits

..

5 Commits

Author SHA1 Message Date
Elizabeth c24e34ae85
Merge branch 'main' into dont-be-authoritative
continuous-integration/drone/pr Build is passing Details
2024-04-04 15:17:56 -06:00
Elizabeth Hunt 569d2788eb
make a unique session id
continuous-integration/drone/push Build is passing Details
2024-04-03 09:57:50 -06:00
simponic 09215bc616 Merge pull request 'Make user login atomic' (#4) from user-login-upsert into main
continuous-integration/drone/push Build is passing Details
Reviewed-on: #4
2024-04-02 16:41:21 -04:00
Ezri Brimhall 161dcbad20
removed commented code
continuous-integration/drone/pr Build is failing Details
2024-04-02 14:37:13 -06:00
Ezri Brimhall 76fb42a61e
User login should now be atomic 2024-04-02 14:36:49 -06:00
1 changed files with 5 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import (
"log"
"time"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/utils"
_ "github.com/mattn/go-sqlite3"
)
@ -50,12 +51,7 @@ func GetUser(dbConn *sql.DB, id string) (*User, error) {
func FindOrSaveUser(dbConn *sql.DB, user *User) (*User, error) {
log.Println("finding or saving user", user.ID)
_, err := dbConn.Exec(`INSERT OR IGNORE INTO users (id, mail, username, display_name) VALUES (?, ?, ?, ?);`, user.ID, user.Mail, user.Username, user.DisplayName)
if err != nil {
return nil, err
}
_, err = dbConn.Exec(`UPDATE users SET mail = ?, username = ?, display_name = ? WHERE id = ?;`, user.Mail, user.Username, user.DisplayName, user.ID)
_, err := dbConn.Exec(`INSERT INTO users (id, mail, username, display_name) VALUES (?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET mail = excluded.mail, username = excluded.username, display_name = excluded.display_name;`, user.ID, user.Mail, user.Username, user.DisplayName)
if err != nil {
return nil, err
}
@ -67,8 +63,9 @@ func MakeUserSessionFor(dbConn *sql.DB, user *User) (*UserSession, error) {
log.Println("making session for user", user.ID)
expireAt := time.Now().Add(time.Hour * 12)
sessionId := utils.RandomId()
_, err := dbConn.Exec(`INSERT OR REPLACE INTO user_sessions (id, user_id, expire_at) VALUES (?, ?, ?);`, user.ID, user.ID, time.Now().Add(ExpiryDuration))
_, err := dbConn.Exec(`INSERT OR REPLACE INTO user_sessions (id, user_id, expire_at) VALUES (?, ?, ?);`, sessionId, user.ID, time.Now().Add(ExpiryDuration))
if err != nil {
log.Println(err)
@ -76,7 +73,7 @@ func MakeUserSessionFor(dbConn *sql.DB, user *User) (*UserSession, error) {
}
return &UserSession{
ID: user.ID,
ID: sessionId,
UserID: user.ID,
ExpireAt: expireAt,
}, nil