Enha: use of sql sessions
This commit is contained in:
@ -19,7 +19,7 @@ func (p *RepoProvider) SessionByToken(ctx context.Context, token string) (*model
|
||||
db := getDB(ctx, p.DB)
|
||||
session := &models.Session{}
|
||||
// The lifetime in the DB is in seconds, but in the model it is in minutes.
|
||||
err := sqlx.GetContext(ctx, db, session, `SELECT id, updated_at, lifetime / 60 as lifetime, cookie_token, username FROM sessions WHERE cookie_token = ?`, token)
|
||||
err := sqlx.GetContext(ctx, db, session, `SELECT id, updated_at, lifetime / 60 as lifetime, token_key, username FROM sessions WHERE token_key = ? LIMIT 1;`, token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -29,21 +29,21 @@ func (p *RepoProvider) SessionByToken(ctx context.Context, token string) (*model
|
||||
func (p *RepoProvider) SessionCreate(ctx context.Context, session *models.Session) error {
|
||||
db := getDB(ctx, p.DB)
|
||||
// The lifetime in the model is in minutes, but in the DB it is in seconds.
|
||||
_, err := db.ExecContext(ctx, `INSERT INTO sessions (updated_at, lifetime, cookie_token, username) VALUES (?, ?, ?, ?)`,
|
||||
time.Now(), session.Lifetime*60, session.CookieToken, session.Username)
|
||||
_, err := db.ExecContext(ctx, `INSERT INTO sessions (updated_at, lifetime, token_key, username) VALUES (?, ?, ?, ?) ON CONFLICT (token_key) DO UPDATE SET updated_at=CURRENT_TIMESTAMP, lifetime=excluded.lifetime;`,
|
||||
time.Now(), session.Lifetime*60, session.TokenKey, session.Username)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *RepoProvider) SessionUpdate(ctx context.Context, session *models.Session) error {
|
||||
db := getDB(ctx, p.DB)
|
||||
// The lifetime in the model is in minutes, but in the DB it is in seconds.
|
||||
_, err := db.ExecContext(ctx, `UPDATE sessions SET updated_at = ?, lifetime = ? WHERE cookie_token = ?`,
|
||||
time.Now(), session.Lifetime*60, session.CookieToken)
|
||||
_, err := db.ExecContext(ctx, `UPDATE sessions SET updated_at = ?, lifetime = ? WHERE token_key = ?`,
|
||||
time.Now(), session.Lifetime*60, session.TokenKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *RepoProvider) SessionDelete(ctx context.Context, token string) error {
|
||||
db := getDB(ctx, p.DB)
|
||||
_, err := db.ExecContext(ctx, `DELETE FROM sessions WHERE cookie_token = ?`, token)
|
||||
_, err := db.ExecContext(ctx, `DELETE FROM sessions WHERE token_key = ?`, token)
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user