sir, I was able to successfully implement hash password , but can’t seems to find a way to verify user email and password in both registration and login
how can i implement http json response? in these code?
these have to spit out useful errors like -“user already exist with same email id, both password field doesn’t match, password is less than 8 chars”
and in login it has to say that user email or password don’t match.
here are the codes
userregistration.go
func User_Collection(user *model.User) (*mongo.InsertOneResult, error) {
collection := clientInstance.Database(AUTH_DB).Collection(USER_COLLECTION)
user.Password = getHash([]byte(user.Password))
return collection.InsertOne(context.TODO(), user)
}
func getHash(password []byte) string {
hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost)
if err != nil {
log.Println(err)
}
return string(hash)
}
login.go
func Find_User(user *model.User) (model.User, error) {
filter := bson.M{"email": user.Email}
result := model.User{}
err := clientInstance.Database(AUTH_DB).Collection(USER_COLLECTION).FindOne(context.Background(), filter).Decode(&result)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return model.User{}, nil
}
return model.User{}, fmt.Errorf("finding user:%w", err)
}
return result, nil
}