aboutsummaryrefslogtreecommitdiffstats
path: root/store.go
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2018-12-29 19:33:16 -0500
committerThibaut Horel <thibaut.horel@gmail.com>2018-12-29 19:33:16 -0500
commit1ed8283df70c5c2f597821fd318cbda89d2eb7ea (patch)
tree1e22ecc92066308a18b5e8226cebe2b2236acc9d /store.go
parent22e21e02e7d333c9e613456f0c203cd167dba4b1 (diff)
downloadid-1ed8283df70c5c2f597821fd318cbda89d2eb7ea.tar.gz
Add basic user page
Diffstat (limited to 'store.go')
-rw-r--r--store.go26
1 files changed, 18 insertions, 8 deletions
diff --git a/store.go b/store.go
index 9723b66..89698ea 100644
--- a/store.go
+++ b/store.go
@@ -3,13 +3,15 @@ package main
import (
"database/sql"
"log"
+ "time"
_ "github.com/lib/pq"
)
type Session struct {
- Id string
- UserId int64
+ Id string
+ UserId int64
+ Created time.Time
}
type User struct {
@@ -43,8 +45,11 @@ func (store *PgStore) GetSession(id string) (*Session, bool) {
return s, true
}
s = new(Session)
- row := store.QueryRow("SELECT id, user_id FROM sessions WHERE id = $1", id)
- if err := row.Scan(&s.Id, &s.UserId); err != nil {
+ row := store.QueryRow(
+ "SELECT id, user_id, created_at FROM sessions WHERE id = $1",
+ id,
+ )
+ if err := row.Scan(&s.Id, &s.UserId, &s.Created); err != nil {
return nil, false
}
store.sessionCache[s.Id] = s
@@ -52,16 +57,21 @@ func (store *PgStore) GetSession(id string) (*Session, bool) {
}
func (store *PgStore) NewSession(userId int64) *Session {
- var id string
- store.QueryRow("INSERT INTO sessions(user_id) VALUES ($1) RETURNING id", userId).Scan(&id)
- s := &Session{id, userId}
+ s := &Session{UserId: userId}
+ store.QueryRow(
+ "INSERT INTO sessions(user_id) VALUES ($1) RETURNING id, created_at",
+ userId,
+ ).Scan(&s.Id, &s.Created)
store.sessionCache[s.Id] = s
return s
}
func (store *PgStore) GetUser(name string) (*User, bool) {
u := &User{Name: name}
- row := store.QueryRow("SELECT id, password FROM users WHERE name = $1", name)
+ row := store.QueryRow(
+ "SELECT id, password FROM users WHERE name = $1",
+ name,
+ )
if err := row.Scan(&u.Id, &u.Password); err != nil {
return nil, false
}