setup project
This commit is contained in:
58
backend/service/auth.go
Normal file
58
backend/service/auth.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.nmcnew.com/nmcnew/bracketeer/db"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrAuthFailed = errors.New("auth values don't match")
|
||||
)
|
||||
|
||||
type AuthService interface {
|
||||
Login(ctx context.Context, username string, authValue string, authType string) (*db.User, error)
|
||||
}
|
||||
type authService struct {
|
||||
q *db.Queries
|
||||
}
|
||||
|
||||
func NewAuthService(q *db.Queries) AuthService {
|
||||
return &authService{
|
||||
q: q,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *authService) Login(ctx context.Context, username, authValue, authType string) (*db.User, error) {
|
||||
dbValue, err := s.q.GetUserAuthValue(ctx, db.GetUserAuthValueParams{
|
||||
Name: username,
|
||||
Type: db.AuthTypeFromString(authType),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch authType {
|
||||
case "password":
|
||||
err = bcrypt.CompareHashAndPassword([]byte(dbValue.AuthValue), []byte(authValue))
|
||||
if err != nil {
|
||||
return nil, ErrAuthFailed
|
||||
}
|
||||
default:
|
||||
if authValue != dbValue.AuthValue {
|
||||
return nil, ErrAuthFailed
|
||||
}
|
||||
user, err := s.q.GetUserFromId(ctx, dbValue.UserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
return nil, ErrAuthFailed
|
||||
}
|
||||
|
||||
func (s *authService) Register(ctx context.Context, username, email, bio, profileURL, authValue, authType string) (*db.User, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
Reference in New Issue
Block a user