Add users table

This commit is contained in:
ekzyis 2023-09-09 22:52:50 +02:00
parent d876997eeb
commit e6c20935bc
2 changed files with 15 additions and 1 deletions

View File

@ -4,7 +4,11 @@ CREATE TABLE lnauth(
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
session_id VARCHAR(48) NOT NULL DEFAULT encode(gen_random_uuid()::text::bytea, 'base64') session_id VARCHAR(48) NOT NULL DEFAULT encode(gen_random_uuid()::text::bytea, 'base64')
); );
CREATE TABLE users(
pubkey TEXT PRIMARY KEY,
last_seen TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE sessions( CREATE TABLE sessions(
pubkey TEXT NOT NULL, pubkey TEXT NOT NULL REFERENCES users(pubkey),
session_id VARCHAR(48) session_id VARCHAR(48)
); );

View File

@ -69,6 +69,11 @@ func verifyLogin(c echo.Context) error {
c.Logger().Error("bad signature") c.Logger().Error("bad signature")
return c.JSON(http.StatusUnauthorized, map[string]string{"status": "ERROR", "reason": "bad signature"}) return c.JSON(http.StatusUnauthorized, map[string]string{"status": "ERROR", "reason": "bad signature"})
} }
_, err = db.Exec("INSERT INTO users(pubkey) VALUES ($1)", query.Key)
if err != nil {
c.Logger().Error(err)
return c.JSON(http.StatusInternalServerError, map[string]string{"status": "ERROR", "reason": "internal server error"})
}
_, err = db.Exec("INSERT INTO sessions(pubkey, session_id) VALUES($1, $2)", query.Key, sessionId) _, err = db.Exec("INSERT INTO sessions(pubkey, session_id) VALUES($1, $2)", query.Key, sessionId)
if err != nil { if err != nil {
c.Logger().Error(err) c.Logger().Error(err)
@ -112,6 +117,11 @@ func sessionHandler(next echo.HandlerFunc) echo.HandlerFunc {
err = db.QueryRow("SELECT pubkey FROM sessions WHERE session_id = $1", sessionId).Scan(&pubkey) err = db.QueryRow("SELECT pubkey FROM sessions WHERE session_id = $1", sessionId).Scan(&pubkey)
if err == nil { if err == nil {
// session found // session found
_, err = db.Exec("UPDATE users SET last_seen = CURRENT_TIMESTAMP WHERE pubkey = $1", pubkey)
if err != nil {
c.Logger().Error(err)
return c.JSON(http.StatusInternalServerError, map[string]string{"status": "ERROR", "reason": "internal server error"})
}
c.Set("session", Session{pubkey}) c.Set("session", Session{pubkey})
} else if err != sql.ErrNoRows { } else if err != sql.ErrNoRows {
return c.JSON(http.StatusInternalServerError, map[string]string{"status": "ERROR", "reason": "internal server error"}) return c.JSON(http.StatusInternalServerError, map[string]string{"status": "ERROR", "reason": "internal server error"})