Spaces:
Build error
Build error
File size: 1,959 Bytes
2d9d20d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package services
import (
"errors"
"godp.abdanhafidz.com/models"
"godp.abdanhafidz.com/repositories"
)
type AuthenticationService struct {
Service[models.Account, models.AuthenticatedUser]
}
func (s *AuthenticationService) Authenticate() {
accountData := repositories.GetAccountbyEmail(s.Constructor.Email)
if accountData.NoRecord {
s.Exception.DataNotFound = true
s.Exception.Message = "there is no account with given credentials!"
return
}
if VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil {
s.Exception.Unauthorized = true
s.Exception.Message = "incorrect password!"
return
}
token, err_tok := GenerateToken(&accountData.Result)
if err_tok != nil {
s.Error = errors.Join(s.Error, err_tok)
return
}
accountData.Result.Password = "SECRET"
s.Result = models.AuthenticatedUser{
Account: accountData.Result,
Token: token,
}
s.Error = accountData.RowsError
}
func (s *AuthenticationService) Update(oldPassword string, newPassword string) {
if len(newPassword) < 8 {
s.Exception.InvalidPasswordLength = true
s.Exception.Message = "Password must have at least 8 characters!"
return
}
accountData := repositories.GetAccountById(s.Constructor.Id)
if accountData.NoRecord {
s.Exception.DataNotFound = true
s.Exception.Message = "there is no account with given credentials!"
return
}
if VerifyPassword(accountData.Result.Password, oldPassword) != nil {
s.Exception.Unauthorized = true
s.Exception.Message = "incorrect old password!"
return
}
hashed_password, _ := HashPassword(newPassword)
accountData.Result.Password = hashed_password
changePassword := repositories.UpdateAccount(accountData.Result)
changePassword.Result.Password = "SECRET"
s.Result = models.AuthenticatedUser{
Account: changePassword.Result,
}
s.Error = changePassword.RowsError
}
// LoginHandler handles user login
|