找回密码

控制器新增
app/http/controllers/auth_controller.go

代码已被折叠,点此展开

新增路由
routes/web.go

.
.
.
r.HandleFunc("/auth/sendemail", auc.SendEmail).Methods("GET").Name("auth.sendemail")
    r.HandleFunc("/auth/dosendemail", auc.DoSendEmail).Methods("POST").Name("auth.dosendemail")
    r.HandleFunc("/auth/reset/{id:[0-9]+}", auc.ResetPassword).Methods("GET").Name("auth.reset")
    r.HandleFunc("/auth/doreset", auc.DoResetPassword).Methods("POST").Name("auth.doreset")

修改登录表单
resources/views/auth/login.gohtml

<a href="" class="text-sm text-muted float-right"><small>找回密码</small></a>

修改为

<a href="{{ RouteName2URL "auth.sendemail" }}" class="text-sm text-muted float-right"><small>找回密码</small></a>

创建填写邮件和重置密码的页面
resources/views/auth/reset.gohtml

{{define "title"}}
重置密码
{{end}}

{{define "main"}}
<div class="blog-post bg-white p-5 rounded shadow mb-4">
  <h3 class="mb-5 text-center">重置密码</h3>
  <form action="{{ RouteName2URL "auth.doreset" }}" method="post">
    <input type="hidden" name="id" value="{{ .ID }}" required="">
    <div class="form-group row mb-3">
      <label for="password" class="col-md-4 col-form-label text-md-right">密码</label>
      <div class="col-md-6">
        <input id="password" type="password" class="form-control {{if .Errors.password }}is-invalid {{end}}" name="password" value="{{ .Password }}" required="">
        {{ with .Errors.password }}
          {{ template "invalid-feedback" . }}
        {{ end }}
      </div>
    </div>
    <div class="form-group row mb-3">
      <label for="password-confirm" class="col-md-4 col-form-label text-md-right">确认密码</label>
      <div class="col-md-6">
        <input id="password-confirm" type="password" class="form-control {{if .Errors.password_confirm }}is-invalid {{end}}" name="password_confirm" value="{{ .PasswordConfirm }}" required="">
        {{ with .Errors.password_confirm }}
          {{ template "invalid-feedback" . }}
        {{ end }}
      </div>
    </div>
    <div class="form-group row mb-3 mb-0 mt-4">
      <div class="col-md-6 offset-md-4">
        <button type="submit" class="btn btn-primary">
          发送
        </button>
      </div>
    </div>
  </form>
</div>
<div class="mb-3">
  <a href="/" class="text-sm text-muted"><small>返回首页</small></a>
</div>
{{end}}

resources/views/auth/sendemail.gohtml

{{define "title"}}
发送邮件
{{end}}

{{define "main"}}
<div class="blog-post bg-white p-5 rounded shadow mb-4">
  <h3 class="mb-5 text-center">发送邮件</h3>
  <form action="{{ RouteName2URL "auth.dosendemail" }}" method="post">
    <div class="form-group row mb-3">
      <label for="email" class="col-md-4 col-form-label text-md-right">E-mail</label>
      <div class="col-md-6">
        <input id="email" type="email" class="form-control {{if .Error }}is-invalid {{end}}" name="email" value="{{ .Email }}" required="">
        {{ with .Error }}
          <div class="invalid-feedback">
              <p>{{ . }}</p>
          </div>
        {{ end }}
      </div>
    </div>
    <div class="form-group row mb-3 mb-0 mt-4">
      <div class="col-md-6 offset-md-4">
        <button type="submit" class="btn btn-primary">
          发送
        </button>
      </div>
    </div>
  </form>
</div>
<div class="mb-3">
  <a href="/" class="text-sm text-muted"><small>返回首页</small></a>
</div>
{{end}}

创建更新用户方法
app/models/user/crud.go

.
.
.
// Update 更新用户
func (user *User) Update() (rowsAffected int64, err error) {
    result := model.DB.Save(&user)
    if err = result.Error; err != nil {
        logger.LogError(err)
        return 0, err
    }

    return result.RowsAffected, nil
}
本帖已被设为精华帖!
本帖由系统于 1年前 自动加精