-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added signup endpoint for web access
- Loading branch information
Showing
4 changed files
with
106 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/go-playground/validator/v10" | ||
"github.com/pass-wall/passwall-server/internal/app" | ||
"github.com/pass-wall/passwall-server/internal/storage" | ||
"github.com/pass-wall/passwall-server/model" | ||
) | ||
|
||
const ( | ||
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 { | ||
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.UpdateUser(s, createdUser, userDTO, false) | ||
if err != nil { | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
// 6. Migrate user specific tables in user's schema | ||
err = s.Users().Migrate(updatedUser.Schema) | ||
if err != nil { | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
response := model.Response{ | ||
Code: http.StatusOK, | ||
Status: Success, | ||
Message: SignupSuccess, | ||
} | ||
RespondWithJSON(w, http.StatusOK, response) | ||
} | ||
} |
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
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