First pass of authentication
This commit is contained in:
parent
c943864edc
commit
01c6e9de87
15 changed files with 311 additions and 42 deletions
36
services/auth/service.go
Normal file
36
services/auth/service.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"emperror.dev/errors"
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/providers/db"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *db.Provider
|
||||
}
|
||||
|
||||
func New(db *db.Provider) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Login(ctx context.Context, username, password string) (models.User, error) {
|
||||
user, err := s.db.SelectUserByUsername(ctx, username)
|
||||
if err != nil {
|
||||
return models.User{}, err
|
||||
}
|
||||
|
||||
if !user.CheckPassword(password) {
|
||||
return models.User{}, errors.New("invalid password")
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetUser(ctx context.Context, userID int64) (models.User, error) {
|
||||
return s.db.SelectUserByID(ctx, userID)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue