Skip to content

Commit

Permalink
feat(login): add error message for misused login methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaraa committed Jun 6, 2024
1 parent 598c180 commit 9e41c8f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
9 changes: 8 additions & 1 deletion app/handlers/apis/email_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ func (e *emailLoginApi) HandleEmailLogin(w http.ResponseWriter, r *http.Request)
}

verificationToken, err := e.service.Login(reqBody)
if err != nil {
if err != nil && errors.Is(err, login.ErrDifferentLoginMethod) {
log.Errorf("[EMAIL LOGIN API]: Failed to login user: %+v, error: %s\n", reqBody, err.Error())
// w.WriteHeader(http.StatusInternalServerError)
status.
BugsBunnyError("This account uses Google Auth to login!").
Render(context.Background(), w)
return
} else if err != nil {
log.Errorf("[EMAIL LOGIN API]: Failed to login user: %+v, error: %s\n", reqBody, err.Error())
// w.WriteHeader(http.StatusInternalServerError)
status.
Expand Down
10 changes: 9 additions & 1 deletion app/handlers/apis/google_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"dankmuzikk/log"
"dankmuzikk/services/login"
"dankmuzikk/views/components/status"
"errors"
"net/http"
"time"
)
Expand Down Expand Up @@ -39,7 +40,14 @@ func (g *googleLoginApi) HandleGoogleOAuthLoginCallback(w http.ResponseWriter, r
}

sessionToken, err := g.service.Login(state, code)
if err != nil {
if err != nil && errors.Is(err, login.ErrDifferentLoginMethod) {
log.Errorf("[EMAIL LOGIN API]: Failed to login, error: %s\n", err.Error())
// w.WriteHeader(http.StatusInternalServerError)
status.
BugsBunnyError("This account uses Email to login!").
Render(context.Background(), w)
return
} else if err != nil {
// w.WriteHeader(http.StatusUnauthorized)
status.
GenericError("Account doesn't exist").
Expand Down
5 changes: 4 additions & 1 deletion app/services/login/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ func NewEmailLoginService(
}

func (e *EmailLoginService) Login(user entities.LoginRequest) (string, error) {
account, err := e.accountRepo.GetByConds("email = ? AND is_o_auth = 0", user.Email)
account, err := e.accountRepo.GetByConds("email = ?", user.Email)
if err != nil {
return "", errors.Join(ErrAccountNotFound, err)
}
if account[0].IsOAuth {
return "", ErrDifferentLoginMethod
}

profile, err := e.profileRepo.GetByConds("account_id = ?", account[0].Id)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions app/services/login/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ var (
ErrAccountExists = errors.New("an account with the associated email already exists")
ErrExpiredVerificationCode = errors.New("expired verification code")
ErrInvalidVerificationCode = errors.New("invalid verification code")
ErrDifferentLoginMethod = errors.New("account uses a different login/signup method")
)
5 changes: 4 additions & 1 deletion app/services/login/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ func (g *GoogleLoginService) Login(state, code string) (string, error) {
return "", err
}

account, err := g.accountRepo.GetByConds("email = ? AND is_o_auth = 1", googleUser.Email)
account, err := g.accountRepo.GetByConds("email = ?", googleUser.Email)
if errors.Is(err, db.ErrRecordNotFound) || len(account) == 0 {
return g.Signup(googleUser)
}
if !account[0].IsOAuth {
return "", ErrDifferentLoginMethod
}
if err != nil {
return "", err
}
Expand Down

0 comments on commit 9e41c8f

Please sign in to comment.