Compare commits

..

No commits in common. "c24e34ae856204f4299ddda26c5877a1abaf2e73" and "d9d39a01f24922b6de6ad65ceebcb3da501d2790" have entirely different histories.

1 changed files with 8 additions and 5 deletions

View File

@ -5,7 +5,6 @@ import (
"log" "log"
"time" "time"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/utils"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@ -51,7 +50,12 @@ func GetUser(dbConn *sql.DB, id string) (*User, error) {
func FindOrSaveUser(dbConn *sql.DB, user *User) (*User, error) { func FindOrSaveUser(dbConn *sql.DB, user *User) (*User, error) {
log.Println("finding or saving user", user.ID) log.Println("finding or saving user", 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) _, 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)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -63,9 +67,8 @@ func MakeUserSessionFor(dbConn *sql.DB, user *User) (*UserSession, error) {
log.Println("making session for user", user.ID) log.Println("making session for user", user.ID)
expireAt := time.Now().Add(time.Hour * 12) 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 (?, ?, ?);`, sessionId, user.ID, time.Now().Add(ExpiryDuration)) _, err := dbConn.Exec(`INSERT OR REPLACE INTO user_sessions (id, user_id, expire_at) VALUES (?, ?, ?);`, user.ID, user.ID, time.Now().Add(ExpiryDuration))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -73,7 +76,7 @@ func MakeUserSessionFor(dbConn *sql.DB, user *User) (*UserSession, error) {
} }
return &UserSession{ return &UserSession{
ID: sessionId, ID: user.ID,
UserID: user.ID, UserID: user.ID,
ExpireAt: expireAt, ExpireAt: expireAt,
}, nil }, nil