示例#1
0
func (a *Auth) Login(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Welcome to the login page!")
	// decoder := json.NewDecoder(r.Body)
	// credentials := new(Credentials)
	// err := decoder.Decode(&credentials)
	// if err != nil {
	// 	panic(err)
	// }

	// db := utilities.GetDB(r)
	// user := new(models.User)
	// err = user.Authenticate(db, credentials.Email, credentials.Password)
	// if err == nil {
	// 	session := sessions.GetSession(r)
	// 	session.Set("user_id", user.ID.Hex())
	// 	session.Set("user_company", user.Company)
	// 	session.Set("user_email", user.Email)
	// 	w.WriteHeader(202)

	// } else {
	// 	w.WriteHeader(404)
	// }

	email := r.FormValue("email")
	password := r.FormValue("password")

	db := utilities.GetDB(r)
	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(w, r, "http://localhost:3000", 202)
		http.Redirect(w, r, "/", 202)
		fmt.Fprintf(w, "error is nil!")
		//http.Redirect(response, request, "/", 202)

	} else {
		//fmt.Fprintf(w, "There is Error!")
		//http.Redirect(w, r, "http://localhost:3000", 202)
		// http.Redirect(w, r, "http://localhost:3000/lastestState", 202)
		// w.WriteHeader(404)
	}
}
示例#2
0
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)
	if err == nil {
		session.Set("user_id", user.ID.Hex())
		session.Set("user_company", user.Company)
		session.Set("user_email", user.Email)
		//fmt.Fprintf(response, "User %s success!", session.Get("user_email"))
		http.Redirect(response, request, "/", 302)

	}

}