You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.6 KiB
Go
84 lines
1.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"github.com/aziis98/lupus-lite/auth"
|
|
"github.com/aziis98/lupus-lite/database"
|
|
"github.com/aziis98/lupus-lite/events"
|
|
"github.com/aziis98/lupus-lite/model"
|
|
"github.com/aziis98/lupus-lite/util"
|
|
)
|
|
|
|
type Handler interface {
|
|
DebugDatabase() any
|
|
|
|
UserForSession(token string) (model.User, error)
|
|
|
|
Login(username, password string) (string, error)
|
|
Register(username, password, password2 string) error
|
|
|
|
Partite() PartiteHandler
|
|
}
|
|
|
|
type server struct {
|
|
DB database.Database
|
|
Auth auth.AuthService
|
|
|
|
partiteHandler *partiteHandler
|
|
}
|
|
|
|
func NewServer(
|
|
db database.Database,
|
|
authServiceFunc func(database.Database) auth.AuthService) Handler {
|
|
|
|
auth := authServiceFunc(db)
|
|
|
|
s := &server{
|
|
DB: db,
|
|
Auth: auth,
|
|
}
|
|
|
|
s.partiteHandler = &partiteHandler{
|
|
server: s,
|
|
eventBus: events.NewEventBus(),
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
func (s *server) DebugDatabase() any {
|
|
return s.DB
|
|
}
|
|
|
|
func (s *server) UserForSession(token string) (model.User, error) {
|
|
username, err := s.Auth.UserForSession(token)
|
|
if err != nil {
|
|
return model.User{}, err
|
|
}
|
|
|
|
user, err := s.DB.GetUser(username)
|
|
if err != nil {
|
|
return model.User{}, err
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
func (s *server) Login(username, password string) (string, error) {
|
|
return s.Auth.Login(username, password)
|
|
}
|
|
|
|
func (s *server) Register(username, password, password2 string) error {
|
|
if err := util.ValidateUsername(username); err != nil {
|
|
return err
|
|
}
|
|
if err := util.ValidatePasswords(password, password2); err != nil {
|
|
return err
|
|
}
|
|
|
|
return s.Auth.Register(username, password)
|
|
}
|
|
|
|
func (s *server) Partite() PartiteHandler {
|
|
return s.partiteHandler
|
|
}
|