Skip to content

Commit

Permalink
注册,登录等用户名和邮箱忽略大小写
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmykuu committed Feb 21, 2019
1 parent e5f47a4 commit 2ac427c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions apis/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func (a *Signin) Post() interface{} {
user := models.User{}

if strings.Contains(form.Username, "@") {
err = c.Find(bson.M{"email": form.Username}).One(&user)
err = c.Find(bson.M{"email": bson.M{"$regex": form.Username, "$options": "i"}}).One(&user)
} else {
err = c.Find(bson.M{"username": form.Username}).One(&user)
err = c.Find(bson.M{"username": bson.M{"$regex": form.Username, "$options": "i"}}).One(&user)
}

if err != nil {
Expand Down Expand Up @@ -131,12 +131,19 @@ func (a *Signup) Post() interface{} {
}
}

if strings.Contains(form.Username, " ") {
return map[string]interface{}{
"status": 0,
"message": "用户名中不能有空格",
}
}

c := a.DB.C(models.USERS)

user := models.User{}
var user models.User

// 检查用户名
err = c.Find(bson.M{"username": form.Username}).One(&user)
err = c.Find(bson.M{"username": bson.M{"$regex": form.Username, "$options": "i"}}).One(&user)
if err == nil {
return map[string]interface{}{
"status": 0,
Expand All @@ -145,7 +152,7 @@ func (a *Signup) Post() interface{} {
}

// 检查邮箱
err = c.Find(bson.M{"email": form.Email}).One(&user)
err = c.Find(bson.M{"email": bson.M{"$regex": form.Email, "$options": "i"}}).One(&user)

if err == nil {
return map[string]interface{}{
Expand Down Expand Up @@ -274,8 +281,8 @@ func (a *ForgotPassword) Post() interface{} {
var user models.User
c := a.DB.C(models.USERS)
err = c.Find(bson.M{"$or": []bson.M{
bson.M{"username": form.UsernameOrEmail},
bson.M{"email": form.UsernameOrEmail}}}).One(&user)
bson.M{"username": bson.M{"$regex": form.UsernameOrEmail, "$options": "i"}},
bson.M{"email": bson.M{"$regex": form.UsernameOrEmail, "$options": "i"}}}}).One(&user)

if err != nil {
return map[string]interface{}{
Expand Down
2 changes: 1 addition & 1 deletion templates/account/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ <h3 class="title has-text-grey">注册</h3>
this.setState({submiting: true});

post('/api/signup', {
username: username,
username: username.trim(),
email: email,
password: password,
geetest_challenge: result.geetest_challenge,
Expand Down

0 comments on commit 2ac427c

Please sign in to comment.