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 getLatestState(response http.ResponseWriter, request *http.Request) *utilities.Error { // Ensure this is a GET request if (request.Method != "GET") || (request.Method == "") { log.Printf("%s RECEIVED UNSUPPORTED REST REQUEST %s %s\n", logTag, request.Method, request.URL) return utilities.ClientError("UNSUPPORTED METHOD", http.StatusBadRequest) } // Get the latest state; only one response will come back before the requesting channel is closed get := make(chan LatestState) StateTableCmds <- &get state := <-get //Graddually group our units AddUuidToMap(state.LatestDisplayRow) //Recyle map with new state UuidMap = RecycleMap(UuidMap, state) //Copy unit into slice, for encoding UuidSlice = ConvertMapToSlice(UuidMap) sort.Sort(ByUuid(UuidSlice)) //Store our message in MongoDB db := utilities.GetDB(request) UtmMessages := models.UtmMsgs{} if state.LatestDisplayRow != nil { out, err := json.Marshal(state.LatestDisplayRow) if err == nil { UtmMessages.Insert(db, state.LatestDisplayRow.Uuid, string(out)) } } // Send the requested data response.Header().Set("Content-Type", "application/json") response.WriteHeader(http.StatusOK) err := json.NewEncoder(response).Encode(UuidSlice) if err != nil { log.Printf("%s RECEIVED REST REQUEST %s BUT ATTEMPTING TO SERIALISE THE RESULT %s YIELDED ERROR %s\n", logTag, request.URL, spew.Sdump(state), err.Error()) return utilities.ServerError(err) } //log.Printf("%s Received rest request %s and responding with %s\n", logTag, request.URL, spew.Sdump(state)) return nil }
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) } }