Skip to content

Commit

Permalink
completed
Browse files Browse the repository at this point in the history
Signed-off-by: PratikforCoding <kotalpratik@gmail.com>
  • Loading branch information
PratikforCoding committed Sep 21, 2023
1 parent 3be138c commit 4d7656a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,15 @@ func (apiCfg *APIConfig)getBusByName(name string) (bson.M, error) {
return bus, nil
}

func (apiCfg *APIConfig)createUser(email, password string) (bson.M, error) {
func (apiCfg *APIConfig)createUser(username, email, password string) (bson.M, error) {
foundUser, err := apiCfg.getUser(email)
if err != nil {
hash, err := auth.HashedPassword(password)
if err != nil {
return nil, err
}
user := bson.M{
"username": username,
"email": email,
"password": hash,
}
Expand Down
11 changes: 10 additions & 1 deletion controllers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ func (apiCfg *APIConfig)HandlerGetBusByName(w http.ResponseWriter, r *http.Reque

func (apiCfg *APIConfig)HandlerCreateAccount(w http.ResponseWriter, r *http.Request) {
type parameters struct {
Username string `json:"username"`
Email string `json:"email"`
Password string `json:"password"`
ConfirmPassword string `json:"confpassword"`
}

decoder := json.NewDecoder(r.Body)
Expand All @@ -85,13 +87,19 @@ func (apiCfg *APIConfig)HandlerCreateAccount(w http.ResponseWriter, r *http.Requ
return
}

user, err := apiCfg.createUser(params.Email, params.Password)
if params.Password != params.ConfirmPassword {
reply.RespondWtihError(w, http.StatusNotAcceptable, "confirm password correctly")
return
}

user, err := apiCfg.createUser(params.Username, params.Email, params.Password)
if err != nil {
reply.RespondWtihError(w, http.StatusConflict, "User already exists")
return
}
idStr := user["_id"].(primitive.ObjectID).Hex()
retUser := bson.M {
"username": user["username"].(string),
"email": user["email"].(string),
"id": idStr,
}
Expand Down Expand Up @@ -125,6 +133,7 @@ func (apiCfg *APIConfig)HandlerLogin(w http.ResponseWriter, r *http.Request) {
}
idStr := user["_id"].(primitive.ObjectID).Hex()
retUser := bson.M {
"username": user["username"].(string),
"email": user["email"].(string),
"id": idStr,
}
Expand Down
Binary file modified out
Binary file not shown.

0 comments on commit 4d7656a

Please sign in to comment.