-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,74 @@ var ( | |
InvalidToken = "Token is expired or not valid!" | ||
NoToken = "Token could not found! " | ||
TokenCreateErr = "Token could not be created" | ||
SignupSuccess = "User created successfully" | ||
) | ||
|
||
// Signup ... | ||
func Signup(s storage.Store) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
|
||
userDTO := new(model.UserDTO) | ||
|
||
// 1. Decode request body to userDTO object | ||
decoder := json.NewDecoder(r.Body) | ||
if err := decoder.Decode(&userDTO); err != nil { | ||
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload") | ||
return | ||
} | ||
defer r.Body.Close() | ||
|
||
// 2. Run validator according to model.UserDTO validator tags | ||
validate := validator.New() | ||
validateError := validate.Struct(userDTO) | ||
if validateError != nil { | ||
errs := GetErrors(validateError.(validator.ValidationErrors)) | ||
RespondWithErrors(w, http.StatusBadRequest, InvalidRequestPayload, errs) | ||
return | ||
} | ||
|
||
// 3. Check if user exist in database | ||
_, err := s.Users().FindByEmail(userDTO.Email) | ||
if err == nil { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
yakuter
Author
Collaborator
|
||
errs := []string{"This email is already used!"} | ||
message := "User couldn't created!" | ||
RespondWithErrors(w, http.StatusBadRequest, message, errs) | ||
return | ||
} | ||
|
||
// 4. Create new user | ||
createdUser, err := app.CreateUser(s, userDTO) | ||
if err != nil { | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
// 5. Update user once to generate schema | ||
updatedUser, err := app.GenerateSchema(s, createdUser) | ||
if err != nil { | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
// 6. Create user schema and tables | ||
err = s.Users().CreateSchema(updatedUser.Schema) | ||
if err != nil { | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
// 7. Create user tables in user schema | ||
app.MigrateUserTables(s, updatedUser.Schema) | ||
|
||
response := model.Response{ | ||
Code: http.StatusOK, | ||
Status: Success, | ||
Message: SignupSuccess, | ||
} | ||
RespondWithJSON(w, http.StatusOK, response) | ||
} | ||
} | ||
|
||
// Signin ... | ||
func Signin(s storage.Store) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
|
@@ -43,13 +109,10 @@ func Signin(s storage.Store) http.HandlerFunc { | |
return | ||
} | ||
|
||
// Create hash from master password | ||
loginDTO.MasterPassword = app.NewSHA256([]byte(loginDTO.MasterPassword)) | ||
|
||
// Check if user exist in database and credentials are true | ||
user, err := s.Users().FindByCredentials(loginDTO.Email, loginDTO.MasterPassword) | ||
if err != nil { | ||
RespondWithError(w, http.StatusUnauthorized, InvalidUser) | ||
RespondWithError(w, http.StatusUnauthorized, err.Error()) | ||
return | ||
} | ||
|
||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ import ( | |
func CreateUser(s storage.Store, userDTO *model.UserDTO) (*model.User, error) { | ||
|
||
// Hasing the master password with SHA256 | ||
userDTO.MasterPassword = NewSHA256([]byte(userDTO.MasterPassword)) | ||
userDTO.MasterPassword = NewBcrypt([]byte(userDTO.MasterPassword)) | ||
|
||
// New user's plan is Free and role is Member (not Admin) | ||
userDTO.Plan = "Free" | ||
This comment has been minimized.
Sorry, something went wrong.
mrtrkmn
Contributor
|
||
|
@@ -33,8 +33,8 @@ func CreateUser(s storage.Store, userDTO *model.UserDTO) (*model.User, error) { | |
func UpdateUser(s storage.Store, user *model.User, userDTO *model.UserDTO, isAuthorized bool) (*model.User, error) { | ||
|
||
// TODO: Refactor the contents of updated user with a logical way | ||
if userDTO.MasterPassword != "" && NewSHA256([]byte(userDTO.MasterPassword)) != user.MasterPassword { | ||
userDTO.MasterPassword = NewSHA256([]byte(userDTO.MasterPassword)) | ||
if userDTO.MasterPassword != "" && NewBcrypt([]byte(userDTO.MasterPassword)) != user.MasterPassword { | ||
userDTO.MasterPassword = NewBcrypt([]byte(userDTO.MasterPassword)) | ||
} else { | ||
userDTO.MasterPassword = user.MasterPassword | ||
} | ||
|
This is very strange
if err == nil
?