Moved logic from Handlers to UI Actions

This commit is contained in:
PB
2025-10-22 16:01:19 +02:00
parent 40a7d841b8
commit e546d16222
7 changed files with 121 additions and 25 deletions

View File

@@ -15,10 +15,11 @@ import (
var (
passSrv *PaswordService
ErrLoginIncorrect = errors.New("login incorrect")
ErrUnableToCacheToken = errors.New("unable to save tokens in cache")
ErrInvalidAccessToken = errors.New("invalid access token")
ErrParsingAccessToken = errors.New("error while parsing access token")
ErrLoginIncorrect = errors.New("login incorrect")
ErrUnableToCacheToken = errors.New("unable to save tokens in cache")
ErrInvalidAccessToken = errors.New("invalid access token")
ErrParsingAccessToken = errors.New("error while parsing access token")
ErrUnableToCacheUserID = errors.New("unable to save User ID in cache")
)
func init() {
@@ -40,6 +41,7 @@ func NewAuthService(userRepo *domain.UserRepository, cache *redis.Client) *AuthS
func (a *AuthService) Login(login, passwd string) (string, error) {
user, err := a.userRepo.FindByUsername(login)
if err != nil {
// TODO place code below in better place...
// if err = database.NoRowsInQuerySet(err); err != nil {
// return "", errors.New("no user found")
// }
@@ -57,16 +59,20 @@ func (a *AuthService) Login(login, passwd string) (string, error) {
return "", ErrUnableToCacheToken
}
// REFACTOR: save uid in cache under user:$ACCES_TOKEN key
// REFACTOR: save uid in cache under "user:$ACCES_TOKEN" key
res := a.cache.Set(context.Background(), "user:"+accessToken, user.ID, accessTokenExpireTime)
if err := res.Err(); err != nil {
fmt.Println("failed to save user:$ACCESS_TOKEN in cache: ", err.Error())
return "", ErrUnableToCacheUserID
}
return accessToken, nil
}
func (a *AuthService) RefreshToken(accessToken string) (string, error) {
// POSSIBLE BIG SECURITY ISSUE- WHEN REFRESH WITH ABANDONED (or EXPIRED)
// ACCESS TOKEN WE GET NEW ACCESS TOKEN
token, claims, err := jwtSrv.ValidateAccessToken(accessToken)
if err != nil || !token.Valid {
return "", ErrInvalidAccessToken