diff options
| author | Thibaut Horel <thibaut.horel@gmail.com> | 2018-12-27 10:13:29 -0500 |
|---|---|---|
| committer | Thibaut Horel <thibaut.horel@gmail.com> | 2018-12-27 10:13:29 -0500 |
| commit | 0adcee59a57178dc7706a43a15aa5b90fe1cb1ee (patch) | |
| tree | 565a730e9952c431452f0f83de88f7ba2608242b /store.go | |
| parent | b953a2c81fa8a66a58a9c0ec185da48ce853e8d9 (diff) | |
| download | id-0adcee59a57178dc7706a43a15aa5b90fe1cb1ee.tar.gz | |
WIP: add login template
Diffstat (limited to 'store.go')
| -rw-r--r-- | store.go | 35 |
1 files changed, 24 insertions, 11 deletions
@@ -15,32 +15,45 @@ type Session struct { type User struct { Id int64 UserName string + Password string } type Store interface { GetSession(id string) (*Session, bool) - GetUser(id int64) (*User, bool) + GetUser(name string) (*User, bool) } type PgStore struct { *sql.DB - cache map[string]*Session + sessionCache map[string]*Session } -func NewPgStore() *PgSessionStore { +func NewPgStore() *PgStore { db, err := sql.Open("postgres", "postgres://auth_master:pass@localhost/authdb") if err != nil { log.Panic(err) } - return &PgSessionStore{db, make(map[string]*Session)} + return &PgStore{db, make(map[string]*Session)} } -func (store *PgSessionStore) Get(id string) (*Session, bool) { - s, ok := store.cache[id] - if !ok { - row := store.QueryRow("SELECT id, user_id FROM session WHERE id = ?", id) - var s Session - row.Scan(&s. +func (store *PgStore) GetSession(id string) (*Session, bool) { + s, ok := store.sessionCache[id] + if ok { + return s, true } - return s, ok + s = new(Session) + row := store.QueryRow("SELECT id, user_id FROM session WHERE id = $1", id) + if err := row.Scan(s.Id, s.UserId); err != nil { + return nil, false + } + return s, true +} + +func (store *PgStore) GetUser(name string) (*User, bool) { + u := new(User) + row := store.QueryRow("SELECT id, user_name, password FROM user WHERE user_name = $1", name) + if err := row.Scan(u.Id, u.UserName, u.Password); err != nil { + return nil, false + } + return u, true } |
