-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
factorisations of some part of the code
- Loading branch information
1 parent
488037f
commit 52eb968
Showing
7 changed files
with
118 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package authHandler | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"lets-go/libs/bcrypt" | ||
loglib "lets-go/libs/logLib" | ||
"lets-go/libs/token" | ||
userModel "lets-go/models/user" | ||
localTypes "lets-go/types" | ||
"reflect" | ||
) | ||
|
||
type Model interface { | ||
Create() error | ||
Delete() error | ||
CheckDuplicate() (bool, error) | ||
} | ||
|
||
func createModel(model Model) error { | ||
duplicate, err := model.CheckDuplicate() | ||
modelName := reflect.ValueOf(model).Type().String() | ||
var formated string | ||
if err != nil { | ||
formated = fmt.Sprintf("an error occured while checking for duplicates of %s", modelName) | ||
loglib.LogError(formated, err) | ||
return err | ||
} | ||
|
||
if duplicate { | ||
formated = fmt.Sprintf("the %s already exists", modelName) | ||
err = errors.New(formated) | ||
loglib.LogError(formated, err) | ||
return err | ||
} | ||
|
||
if err := model.Create(); err != nil { | ||
formated = fmt.Sprintf("an error occured while creating %s", modelName) | ||
loglib.LogError(formated, err) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateUser(dataObj *localTypes.LoginRequestData) (*userModel.User, error) { | ||
user, err := userModel.GetByEmail(dataObj.Email) | ||
if err != nil { | ||
loglib.LogError("the e-mail doesn't exist", err) | ||
return nil, err | ||
} | ||
|
||
if !bcrypt.CheckPasswordHash(dataObj.Password, user.Password) { | ||
loglib.LogError("wrong password", nil) | ||
return nil, errors.New("wrong password") | ||
} | ||
|
||
return user, nil | ||
} | ||
|
||
type Tokens struct { | ||
accessToken string | ||
refreshToken string | ||
} | ||
|
||
func generateTokens(userID string, listRoles []string) (*Tokens, error) { | ||
accessToken, err := token.CreateAccessToken(userID, listRoles) | ||
|
||
if err != nil { | ||
loglib.LogError("error while creating access token", err) | ||
return nil, err | ||
} | ||
|
||
refreshToken, err := token.CreateRefreshToken(userID, listRoles) | ||
if err != nil { | ||
loglib.LogError("error while creating refresh token", err) | ||
return nil, err | ||
} | ||
|
||
return &Tokens{accessToken, refreshToken}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package commonerrors | ||
|
||
import ( | ||
localconstants "lets-go/libs/localConstants" | ||
loglib "lets-go/libs/logLib" | ||
"net/http" | ||
) | ||
|
||
func EncodingError(responseWriter http.ResponseWriter, err error) { | ||
loglib.LogError("error while encoding response", err) | ||
http.Error(responseWriter, localconstants.SERVER_ERROR, http.StatusInternalServerError) | ||
} | ||
|
||
func DencodingError(responseWriter http.ResponseWriter, err error) { | ||
http.Error(responseWriter, localconstants.INVALID_JSON_FORMAT, http.StatusBadRequest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package localconstants | ||
|
||
const ( | ||
SERVER_ERROR = "server error" | ||
UNAUTHORIZED = "unauthorized" | ||
SERVER_ERROR = "server error" | ||
UNAUTHORIZED = "unauthorized" | ||
INVALID_JSON_FORMAT = "invalid Json format" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package user_model | ||
package userModel | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// models/user/user_test.go | ||
package user_model | ||
package userModel | ||
|
||
import ( | ||
"os" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters