Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline Authentication Functions in Service #38

Closed
Feyzanrs opened this issue May 20, 2024 · 0 comments
Closed

Streamline Authentication Functions in Service #38

Feyzanrs opened this issue May 20, 2024 · 0 comments
Assignees
Labels
Be Be Team config Updates or changes to system configurations medium-priority Important issues to be addressed soon refactor Improvements or restructuring of existing functionality

Comments

@Feyzanrs
Copy link
Member

Feyzanrs commented May 20, 2024

Files to Update:

internal/application/service/authentication.go

Directive:

  • Replace the entire content of authentication.go with the following code:
package application

import (
	"context"

	me "github.com/octoposprime/op-be-auth/internal/domain/model/entity"
	mo "github.com/octoposprime/op-be-auth/internal/domain/model/object"
	smodel "github.com/octoposprime/op-be-shared/pkg/model"
	pb_logging "github.com/octoposprime/op-be-shared/pkg/proto/pb/logging"
	tjwt "github.com/octoposprime/op-be-shared/tool/jwt"
)

// Login generates an authentication token if the given login request values are valid.
func (a *Service) Login(ctx context.Context, loginRequest mo.LoginRequest) (mo.Token, error) {
	if err := loginRequest.Validate(); err != nil {
		userId, _ := ctx.Value(smodel.QueryKeyUid).(string)
		go a.Log(context.Background(), me.NewLogData().GenerateLogData(pb_logging.LogType_LogTypeERROR, "Login", userId, err.Error()))
		return *mo.NewEmptyToken(), err
	}
	user, err := a.CheckUserPassword(ctx, &loginRequest)
	if err != nil {
		userId, _ := ctx.Value(smodel.QueryKeyUid).(string)
		go a.Log(context.Background(), me.NewLogData().GenerateLogData(pb_logging.LogType_LogTypeERROR, "Login", userId, err.Error()))
		return *mo.NewEmptyToken(), err
	}
	if err := a.CheckIsAuthenticable(user); err != nil {
		userId, _ := ctx.Value(smodel.QueryKeyUid).(string)
		go a.Log(context.Background(), me.NewLogData().GenerateLogData(pb_logging.LogType_LogTypeERROR, "Login", userId, err.Error()))
		return *mo.NewEmptyToken(), err
	}
	claims := tjwt.NewClaims(user.Id.String(), user)
	accessToken, refreshToken, err := claims.GenerateJWT()
	if err != nil {
		userId, _ := ctx.Value(smodel.QueryKeyUid).(string)
		go a.Log(context.Background(), me.NewLogData().GenerateLogData(pb_logging.LogType_LogTypeERROR, "Login", userId, err.Error()))
		return *mo.NewEmptyToken(), err
	}
	userId, _ := ctx.Value(smodel.QueryKeyUid).(string)
	go a.Log(context.Background(), me.NewLogData().GenerateLogData(pb_logging.LogType_LogTypeINFO, "Login", userId, "login succeeded"))
	return *mo.NewToken(accessToken, refreshToken), nil

}

// Refresh regenerate an authentication token.
func (a *Service) Refresh(ctx context.Context, token mo.Token) (mo.Token, error) {
	return *mo.NewEmptyToken(), nil
}

// Logout clears some footprints for the user.
func (a *Service) Logout(ctx context.Context, token mo.Token) error {
	if err := token.Validate(); err != nil {
		userId, _ := ctx.Value(smodel.QueryKeyUid).(string)
		go a.Log(context.Background(), me.NewLogData().GenerateLogData(pb_logging.LogType_LogTypeERROR, "Logout", userId, err.Error()))
		return err
	}
	userId, _ := ctx.Value(smodel.QueryKeyUid).(string)
	go a.Log(context.Background(), me.NewLogData().GenerateLogData(pb_logging.LogType_LogTypeERROR, "Logout", userId, "logout succeeded"))
	return nil
}

The changes will be merged into the first/hsm/1/create-first branch first. Therefore, set the base branch of the pull request as first/hsm/1/create-first.

@Feyzanrs Feyzanrs added config Updates or changes to system configurations medium-priority Important issues to be addressed soon refactor Improvements or restructuring of existing functionality labels May 20, 2024
@octopos-prime octopos-prime added the Be Be Team label May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Be Be Team config Updates or changes to system configurations medium-priority Important issues to be addressed soon refactor Improvements or restructuring of existing functionality
Projects
None yet
Development

No branches or pull requests

3 participants