本文整理汇总了Golang中github.com/robmeades/utm/service/models.User.Authenticate方法的典型用法代码示例。如果您正苦于以下问题:Golang User.Authenticate方法的具体用法?Golang User.Authenticate怎么用?Golang User.Authenticate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/robmeades/utm/service/models.User
的用法示例。
在下文中一共展示了User.Authenticate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Login
/// Login method for the authentication struct that takes
// an HTTP request and returns an HTTP response.
// A session is created if successful.
func (a *Auth) Login(request http.ResponseWriter, response *http.Request) {
fmt.Fprintf(request, "Welcome to the login page!")
// decoder := json.NewDecoder(response.Body)
// credentials := new(Credentials)
// err := decoder.Decode(&credentials)
// if err != nil {
// panic(err)
// }
// db := utilities.GetDB(response)
// user := new(models.User)
// err = user.Authenticate(db, credentials.Email, credentials.Password)
// if err == nil {
// session := sessions.GetSession(response)
// session.Set("user_id", user.ID.Hex())
// session.Set("user_company", user.Company)
// session.Set("user_email", user.Email)
// request.WriteHeader(202)
// } else {
// request.WriteHeader(404)
// }
email := response.FormValue("email")
password := response.FormValue("password")
db := utilities.GetDB(response)
user := new(models.User)
err := user.Authenticate(db, email, password)
if err == nil {
//session := sessions.GetSession(request)
// session.Set("user_id", user.ID.Hex())
// session.Set("user_company", user.Company)
// session.Set("user_email", user.Email)
//response.WriteHeader(202)
http.Redirect(request, response, "http://localhost:3000", 202)
http.Redirect(request, response, "/", 202)
fmt.Fprintf(request, "error is nil!")
//http.Redirect(response, request, "/", 202)
} else {
//fmt.Fprintf(request, "There is Error!")
//http.Redirect(request, response, "http://localhost:3000", 202)
// http.Redirect(request, response, "http://localhost:3000/lastestState", 202)
// request.WriteHeader(404)
}
}
示例2: loginHandler
func loginHandler(response http.ResponseWriter, request *http.Request) {
email := request.FormValue("email")
password := request.FormValue("password")
session := sessions.GetSession(request)
db := utilities.GetDB(request)
user := new(models.User)
err := user.Authenticate(db, email, password)
fmt.FPrintf("Calling login User session \n%s", spew.Sdump(user_id))
if err == nil {
session.Set("user_id", user.ID.Hex())
session.Set("user_company", user.Company)
session.Set("user_email", user.Email)
http.Redirect(response, request, "/display", 302)
} else {
http.Redirect(response, request, "/", 302)
}
}