func registerHandler(response http.ResponseWriter, request *http.Request) { company := request.FormValue("company_name") firstName := request.FormValue("user_firstName") lastName := request.FormValue("user_lastName") email := request.FormValue("email") password := request.FormValue("password") db := utilities.GetDB(request) user := new(models.User) user.NewUser(db, company, firstName, lastName, email, password) //fmt.Fprintf(response, "User %s Created Successfullyss!", firstName) http.Redirect(response, request, "/login", 302) }
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) } }
func (a *Auth) User(w http.ResponseWriter, r *http.Request) { db := utilities.GetDB(r) session := sessions.GetSession(r) user_id := session.Get("user_id") fmt.Println(user_id) if user_id == nil { w.WriteHeader(403) } else { user := new(models.User) user.Get(db, user_id.(string)) fmt.Println(user) outData, _ := json.Marshal(user) w.Write(outData) } }
func (a *Auth) Uuids(w http.ResponseWriter, r *http.Request) { db := utilities.GetDB(r) session := sessions.GetSession(r) user_company := session.Get("user_company") fmt.Println(user_company) if user_company == nil { w.WriteHeader(403) } else { user := new(models.User) uuids := user.GetUserUuids(db, user_company.(string)) fmt.Println(uuids) outData, _ := json.Marshal(uuids) w.Write(outData) } }
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) } }