add method logout
rename auth method to login
This commit is contained in:
1 parent
7ab0e77a97
commit
8c0513d394
4 files changed
+27
-4
No files matched your search
+15
-1
@@ -12,9 +12,23 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gitlab8.alx/msp2.0/msp-lib/utils"
|
||||
"fmt"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"gitlab8.alx/msp2.0/msp-lib/logger"
|
||||
)
|
||||
|
||||
func Auth(authRequest structure.AuthRequest) (*structure.Auth, error) {
|
||||
func Logout(metadata metadata.MD) error {
|
||||
token := metadata.Get(utils.ADMIN_AUTH_HEADER_NAME)
|
||||
|
||||
if len(token) == 0 || token[0] == "" {
|
||||
logger.Errorf("Admin AUTH header: %s, not found, received: %v", utils.ADMIN_AUTH_HEADER_NAME, metadata)
|
||||
st := status.New(codes.InvalidArgument, utils.ServiceError)
|
||||
return st.Err()
|
||||
}
|
||||
model.InvalidateOldToken(token[0])
|
||||
return nil
|
||||
}
|
||||
|
||||
func Login(authRequest structure.AuthRequest) (*structure.Auth, error) {
|
||||
user, err := model.GetUserByEmail(authRequest.Email)
|
||||
if user == nil {
|
||||
return nil, status.New(codes.NotFound, "User not found").Err()
|
||||
|
||||
+5
-2
@@ -4,11 +4,13 @@ import (
|
||||
libStr "gitlab8.alx/msp2.0/msp-lib/structure"
|
||||
"admin-service/structure"
|
||||
"admin-service/controller"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
type Handlers struct {
|
||||
// ===== AUTH =====
|
||||
Auth func(structure.AuthRequest) (*structure.Auth, error) `method:"auth" inner:"false"`
|
||||
Login func(structure.AuthRequest) (*structure.Auth, error) `method:"login" inner:"false"`
|
||||
Logout func(metadata.MD) error `method:"logout" inner:"true"`
|
||||
GetUsers func(structure.UsersRequest) (*structure.UsersResponse, error) `method:"get_users" inner:"true"`
|
||||
CreateUpdateUser func(user libStr.AdminUser) (*libStr.AdminUser, error) `method:"create_update_user" inner:"true"`
|
||||
DeleteUser func(identities structure.IdentitiesRequest) (*structure.DeleteResponse, error) `method:"delete_user" inner:"true"`
|
||||
@@ -16,7 +18,8 @@ type Handlers struct {
|
||||
|
||||
func GetHandlers() *Handlers {
|
||||
return &Handlers{
|
||||
controller.Auth,
|
||||
controller.Login,
|
||||
controller.Logout,
|
||||
controller.GetUsers,
|
||||
controller.CreateUpdateUser,
|
||||
controller.DeleteUser,
|
||||
|
||||
@@ -8,12 +8,18 @@ import (
|
||||
)
|
||||
|
||||
const DELETE_TOKENS = "DELETE FROM " + utils.DB_SCHEME + ".tokens WHERE user_id=?"
|
||||
const DELETE_TOKEN = "DELETE FROM " + utils.DB_SCHEME + ".tokens WHERE token=?"
|
||||
|
||||
func InvalidateOldTokens(userId int64) (int, error) {
|
||||
result, err := database.GetDBManager().Db.Exec(DELETE_TOKENS, userId)
|
||||
return result.RowsAffected(), err
|
||||
}
|
||||
|
||||
func InvalidateOldToken(token string) (int, error) {
|
||||
result, err := database.GetDBManager().Db.Exec(DELETE_TOKEN, token)
|
||||
return result.RowsAffected(), err
|
||||
}
|
||||
|
||||
func CreateNewToken(userId int64, tokenString string, expiredTime *time.Time) (*libStr.AdminToken, error) {
|
||||
token := libStr.AdminToken{UserId: userId, Token: tokenString}
|
||||
if expiredTime != nil {
|
||||
|
||||
Reference in new issue
Block a user