purge expired sessions and password reset tokens every 24 hours
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"time"
|
||||||
|
|
||||||
_ "modernc.org/sqlite"
|
_ "modernc.org/sqlite"
|
||||||
)
|
)
|
||||||
@@ -27,6 +28,18 @@ func Open(path string) (*sql.DB, error) {
|
|||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PurgeExpired deletes expired sessions and used/expired password reset tokens.
|
||||||
|
func PurgeExpired(database *sql.DB) error {
|
||||||
|
now := time.Now().UTC().Format(time.RFC3339)
|
||||||
|
if _, err := database.Exec(`DELETE FROM sessions WHERE expires_at < ?`, now); err != nil {
|
||||||
|
return fmt.Errorf("purge sessions: %w", err)
|
||||||
|
}
|
||||||
|
if _, err := database.Exec(`DELETE FROM password_resets WHERE expires_at < ? OR used = 1`, now); err != nil {
|
||||||
|
return fmt.Errorf("purge password_resets: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func runSchema(db *sql.DB) error {
|
func runSchema(db *sql.DB) error {
|
||||||
stmts := []string{
|
stmts := []string{
|
||||||
`CREATE TABLE IF NOT EXISTS customers (
|
`CREATE TABLE IF NOT EXISTS customers (
|
||||||
|
|||||||
Reference in New Issue
Block a user