chore(auth): #331 add tests for sign in
This commit is contained in:
@@ -31,7 +31,7 @@ type Auth interface {
|
||||
SendVerificationMail(userId uuid.UUID, email string)
|
||||
VerifyUserEmail(token string) error
|
||||
|
||||
SignIn(email string, password string) (*types.Session, *types.User, error)
|
||||
SignIn(session *types.Session, email string, password string) (*types.Session, *types.User, error)
|
||||
SignInSession(sessionId string) (*types.Session, *types.User, error)
|
||||
SignInAnonymous() (*types.Session, error)
|
||||
SignOut(sessionId string) error
|
||||
@@ -65,7 +65,7 @@ func NewAuthImpl(db db.Auth, random Random, clock Clock, mail Mail, serverSettin
|
||||
}
|
||||
}
|
||||
|
||||
func (service AuthImpl) SignIn(email string, password string) (*types.Session, *types.User, error) {
|
||||
func (service AuthImpl) SignIn(session *types.Session, email string, password string) (*types.Session, *types.User, error) {
|
||||
user, err := service.db.GetUserByEmail(email)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
@@ -81,7 +81,12 @@ func (service AuthImpl) SignIn(email string, password string) (*types.Session, *
|
||||
return nil, nil, ErrInvalidCredentials
|
||||
}
|
||||
|
||||
session, err := service.createSession(user.Id)
|
||||
err = service.cleanUpSessionWithTokens(session)
|
||||
if err != nil {
|
||||
return nil, nil, types.ErrInternal
|
||||
}
|
||||
|
||||
session, err = service.createSession(user.Id)
|
||||
if err != nil {
|
||||
return nil, nil, types.ErrInternal
|
||||
}
|
||||
@@ -89,6 +94,30 @@ func (service AuthImpl) SignIn(email string, password string) (*types.Session, *
|
||||
return session, user, nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) cleanUpSessionWithTokens(session *types.Session) error {
|
||||
if session == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := service.db.DeleteSession(session.Id)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
tokens, err := service.db.GetTokensBySessionIdAndType(session.Id, types.TokenTypeCsrf)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
for _, token := range tokens {
|
||||
err = service.db.DeleteToken(token.Token)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) SignInSession(sessionId string) (*types.Session, *types.User, error) {
|
||||
if sessionId == "" {
|
||||
return nil, nil, ErrSessionIdInvalid
|
||||
|
||||
Reference in New Issue
Block a user