找回密码
控制器新增
app/http/controllers/auth_controller.go
.
.
.
// SendEmail 发送邮件表单
func (*AuthController) SendEmail(w http.ResponseWriter, r *http.Request) {
view.RenderSimple(w, view.D{}, "auth.sendemail")
}
// DoSendEmail 发送邮件
func (*AuthController) DoSendEmail(w http.ResponseWriter, r *http.Request) {
email := r.PostFormValue("email")
// 1. 根据 Email 获取用户
_user, err := user.GetByEmail(email)
// 2. 如果出现错误
var e error
if err != nil {
if err == gorm.ErrRecordNotFound {
e = errors.New("账号不存在")
} else {
e = errors.New("内部错误,请稍后尝试")
}
view.RenderSimple(w, view.D{
"Error": e.Error(),
"Email": email,
}, "auth.sendemail")
} else {
id := _user.ID
// TODO: 发送带id链接邮件到邮箱
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "邮件发送成功,ID: "+strconv.FormatUint(id, 10))
}
}
// ResetPassword 重置密码表单
func (*AuthController) ResetPassword(w http.ResponseWriter, r *http.Request) {
id := route.GetRouteVariable("id", r)
view.RenderSimple(w, view.D{
"ID": id,
}, "auth.reset")
}
// DoResetPassword 重置密码
func (*AuthController) DoResetPassword(w http.ResponseWriter, r *http.Request) {
id := r.PostFormValue("id")
_user, err := user.Get(id)
if err != nil {
if err == gorm.ErrRecordNotFound {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "404 用户未找到")
} else {
logger.LogError(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "500 服务器内部错误")
}
} else {
password := r.PostFormValue("password")
password_confirm := r.PostFormValue("password_confirm")
errs := validateResetPasswordFormData(password, password_confirm)
if len(errs) > 0 {
view.RenderSimple(w, view.D{
"Errors": errs,
"Password": password,
"PasswordConfirm": password_confirm,
}, "auth.reset")
} else {
_user.Password = password
rowsAffected, err := _user.Update()
if err != nil {
// 数据库错误
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "500 服务器内部错误")
return
}
if rowsAffected > 0 {
showURL := route.Name2URL("auth.login")
http.Redirect(w, r, showURL, http.StatusFound)
} else {
fmt.Fprint(w, "您没有做任何更改!")
}
}
}
}
func validateResetPasswordFormData(password string, password_confirm string) map[string][]string {
errs := make(map[string][]string)
if password == "" {
errs["password"] = append(errs["password"], "密码为必填项")
}
if len(password) < 6 {
errs["password"] = append(errs["password"], "密码长度需大于 6")
}
if password != password_confirm {
errs["password_confirm"] = append(errs["password_confirm"], "两次输入密码不匹配!")
}
return errs
}
新增路由
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年前 自动加精