Changed the data structure, will add the things when the time comes

This commit is contained in:
2024-05-20 17:35:01 +01:00
parent 771166f809
commit 2179040833
3 changed files with 44 additions and 23 deletions
+41 -4
View File
@@ -24,10 +24,16 @@ type Claims struct {
jwt.RegisteredClaims jwt.RegisteredClaims
} }
type SessionData struct {
User string
Token string
ExpirationTime int64
}
// Private functions // Private functions
func openDB() *SQLite { func openDB() *Database {
var d SQLite var d Database
err := d.Initialize("../src/data.db") err := d.Initialize("../src/data.db")
if err != nil { if err != nil {
log.Fatal("Failed to get into DB: ", err) log.Fatal("Failed to get into DB: ", err)
@@ -78,8 +84,6 @@ func insertToken(token string, user string, expirationDate int64) error {
); );
` `
log.Println(query)
_, err := d.DB.Exec(query) _, err := d.DB.Exec(query)
if err != nil { if err != nil {
log.Fatal("Failed to insert data: ", err) log.Fatal("Failed to insert data: ", err)
@@ -89,6 +93,35 @@ func insertToken(token string, user string, expirationDate int64) error {
return nil return nil
} }
func getSessionData(token string) (*SessionData, error) {
d := openDB()
query := `
SELECT user, token, expires FROM sessions WHERE token='` + token + `';
`
rows, err := d.DB.Query(query)
if err != nil {
log.Fatal("Failed to get user data: ", err)
return nil, err
}
defer rows.Close()
var p SessionData
for rows.Next() {
if err := rows.Scan(&p.User, &p.Token, &p.ExpirationTime); err != nil {
log.Fatal("Failed to read row.", err)
}
}
// No password found (user doesn't exist?)
if len(p.Token) == 0 {
return nil, nil
}
return &p, nil
}
// Public functions // Public functions
func ValidateCredentials(lf *LoginForm) bool { func ValidateCredentials(lf *LoginForm) bool {
@@ -110,3 +143,7 @@ func StoreSessionToken(token string, user string, expirationDate int64) error {
return nil return nil
} }
func GetSession(token string) (*SessionData, error) {
return getSessionData(token)
}
+2 -18
View File
@@ -15,12 +15,12 @@ type DBAccess interface {
updateq(query string) error updateq(query string) error
} }
type SQLite struct { type Database struct {
DB *sql.DB DB *sql.DB
Path string Path string
} }
func (s *SQLite) Initialize(path string) error { func (s *Database) Initialize(path string) error {
db, err := sql.Open("sqlite3", path) db, err := sql.Open("sqlite3", path)
if err != nil { if err != nil {
return err return err
@@ -31,19 +31,3 @@ func (s *SQLite) Initialize(path string) error {
return nil return nil
} }
func (s *SQLite) Createq(query string) error {
return nil
}
func (s *SQLite) Selectq(query string) error {
return nil
}
func (s *SQLite) Deleteq(query string) error {
return nil
}
func (s *SQLite) Updateq(query string) error {
return nil
}
+1 -1
View File
@@ -9,7 +9,7 @@ CREATE TABLE IF NOT EXISTS users(
CREATE TABLE IF NOT EXISTS sessions( CREATE TABLE IF NOT EXISTS sessions(
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
user TEXT NOT NULL UNIQUE, user TEXT NOT NULL,
token TEXT NOT NULL, token TEXT NOT NULL,
expires INTEGER expires INTEGER
); );