more logging
This commit is contained in:
parent
deb4b0509b
commit
334550cef9
@ -3,15 +3,19 @@ package common
|
|||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"party.at/party/internal/data"
|
"party.at/party/internal/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (app *Application) LoginUser(email, password string) (*data.Token, error) {
|
func (app *Application) LoginUser(email, password string) (*data.Token, error) {
|
||||||
|
app.Logger.PrintInfo("login attempt", map[string]string{"email": email})
|
||||||
|
|
||||||
user, err := app.Models.Users.GetByEmail(email)
|
user, err := app.Models.Users.GetByEmail(email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, data.ErrRecordNotFound) {
|
if errors.Is(err, data.ErrRecordNotFound) {
|
||||||
|
app.Logger.PrintInfo("login failed: user not found", map[string]string{"email": email})
|
||||||
return nil, data.ErrInvalidCredentials
|
return nil, data.ErrInvalidCredentials
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -19,9 +23,15 @@ func (app *Application) LoginUser(email, password string) (*data.Token, error) {
|
|||||||
|
|
||||||
identities, err := app.Models.UserIdentities.GetByUser(user.ID)
|
identities, err := app.Models.UserIdentities.GetByUser(user.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, data.ErrRecordNotFound) {
|
||||||
|
app.Logger.PrintInfo("login failed: no identities", map[string]string{"email": email, "user_id": fmt.Sprint(user.ID)})
|
||||||
|
return nil, data.ErrInvalidCredentials
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.Logger.PrintInfo("login: found identities", map[string]string{"email": email, "count": fmt.Sprint(len(identities))})
|
||||||
|
|
||||||
var authenticatedIdentity *data.UserIdentity
|
var authenticatedIdentity *data.UserIdentity
|
||||||
for _, identity := range identities {
|
for _, identity := range identities {
|
||||||
match, err := identity.Password.Matches(password)
|
match, err := identity.Password.Matches(password)
|
||||||
@ -35,9 +45,11 @@ func (app *Application) LoginUser(email, password string) (*data.Token, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if authenticatedIdentity == nil {
|
if authenticatedIdentity == nil {
|
||||||
|
app.Logger.PrintInfo("login failed: wrong password", map[string]string{"email": email})
|
||||||
return nil, data.ErrInvalidCredentials
|
return nil, data.ErrInvalidCredentials
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.Logger.PrintInfo("login success", map[string]string{"email": email, "user_id": fmt.Sprint(user.ID)})
|
||||||
return app.Models.Tokens.New(user.ID, authenticatedIdentity.ID, 24*time.Hour, data.ScopeAuthentication)
|
return app.Models.Tokens.New(user.ID, authenticatedIdentity.ID, 24*time.Hour, data.ScopeAuthentication)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user