Reputation: 645
Using a struct, I'm saving the data in the JSON form to collection like so:
type SignUp struct {
Id int `json:"_id" bson:"_id"`
UserName string `json:"user_name" bson:"user_name"`
EmailId string `json:"email_id" bson:"email_id"`
Password string `json:"password" bson:"password"`
}
type SignUps []SignUp
And retrieved the data from the collection of mongodb using function
func Login(c *gin.Context) {
response := ResponseControllerList{}
conditions := bson.M{}
data, err := GetAllUser(conditions)
fmt.Println(data)
fmt.Println(err)
Matching(data)
return
}
func GetAllUser(blogQuery interface{}) (result SignUps, err error) {
mongoSession := config.ConnectDb()
sessionCopy := mongoSession.Copy()
defer sessionCopy.Close()
getCollection := mongoSession.DB("sign").C("signup")
err = getCollection.Find(blogQuery).Select(bson.M{}).All(&result)
if err != nil {
return result, err
}
return result, nil
}
But the data retrieved without its keys like shown below:-
[{1 puneet [email protected] puneet} {2 Rohit [email protected] puneet}]
Issue is that I want to collect all the existing EmailId
and Password
and match them with userid
and password
entered by the user in fucntion given bellow:-
func Matching(data Signups){
userid="[email protected]"
password="puneet"
//match?
}
How can I match the userid
and password
with the EmailId
and Password
fields?
Upvotes: 0
Views: 1048
Reputation: 5770
This is not answering your question directly, but is very important nonetheless. It seems that you are saving the passwords of your users in plain text in the database. This is very careless and you should add some encryption like bcrypt: https://godoc.org/golang.org/x/crypto/bcrypt
When you have added some encryption, your matching would be quite different than all given answers. This depends on the used encryption package. But please ignore the other answers. They might work, but only for plain text passwords.
Upvotes: 1
Reputation: 31
for _, signup := range data {
if userId == signup.Id && password == signup.Password {
println("Match!")
}
}
Upvotes: 0
Reputation: 75
You can give an if
condition to check if the userid or email is empty or not on your Matching
function like:
....
if data.userid != "" {
data.username = data.userid
}
data.password
....
You can create new struct for the query and use it to create your Matching
function.
Upvotes: 0