add working signup + login
This commit is contained in:
parent
bc85b7c340
commit
d8cb8c8fa8
27 changed files with 600 additions and 39 deletions
52
internal/database/sql/token.go
Normal file
52
internal/database/sql/token.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package sql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"emperror.dev/errors"
|
||||
"git.sleepycat.moe/sam/mercury/internal/database"
|
||||
"github.com/keegancsmith/sqlf"
|
||||
"github.com/oklog/ulid/v2"
|
||||
)
|
||||
|
||||
// TokenStore is the interface to tokens in the database.
|
||||
type TokenStore struct {
|
||||
q Querier
|
||||
}
|
||||
|
||||
// NewTokenStore creates a new TokenStore instance.
|
||||
func NewTokenStore(q Querier) *TokenStore {
|
||||
return &TokenStore{q: q}
|
||||
}
|
||||
|
||||
func (s *TokenStore) Get(ctx context.Context, id ulid.ULID) (database.Token, error) {
|
||||
q := sqlf.Sprintf("SELECT * FROM tokens WHERE id = %s", id)
|
||||
|
||||
t, err := Get[database.Token](ctx, s.q, q)
|
||||
return t, errors.Wrap(err, "executing query")
|
||||
}
|
||||
|
||||
func (s *TokenStore) GetApplication(ctx context.Context, id ulid.ULID) (database.Application, error) {
|
||||
q := sqlf.Sprintf("SELECT * FROM applications WHERE id = %s", id)
|
||||
|
||||
app, err := Get[database.Application](ctx, s.q, q)
|
||||
return app, errors.Wrap(err, "executing query")
|
||||
}
|
||||
|
||||
func (s *TokenStore) Create(ctx context.Context, userID, appID ulid.ULID, scopes []string, expires time.Time) (database.Token, error) {
|
||||
q := sqlf.Sprintf(`INSERT INTO tokens
|
||||
(id, user_id, app_id, scopes, expires)
|
||||
values (%s, %s, %s, %v, %v)
|
||||
RETURNING *`, makeULID(), userID, appID, scopes, expires)
|
||||
|
||||
t, err := Get[database.Token](ctx, s.q, q)
|
||||
return t, errors.Wrap(err, "executing query")
|
||||
}
|
||||
|
||||
func (s *TokenStore) CreateApplication(ctx context.Context, name string) (database.Application, error) {
|
||||
q := sqlf.Sprintf("INSERT INTO applications (id, name) VALUES (%s, %s) RETURNING *", makeULID(), name)
|
||||
|
||||
app, err := Get[database.Application](ctx, s.q, q)
|
||||
return app, errors.Wrap(err, "executing query")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue