//Route handlers func RegisterFormProcess(w http.ResponseWriter, req *http.Request) { user := &repository.User{ Password: req.FormValue("password"), Username: req.FormValue("username"), Email: req.FormValue("email"), } bCryptPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(user.Password), 6) if err != nil { http.Error(w, http.StatusText(405), 405) } user.Password = string(bCryptPasswordBytes) user, err = repository.UserRepository().Add(user) if err != nil { http.Error(w, http.StatusText(405), 405) } }
//Route handlers func LoginFormProcess(w http.ResponseWriter, req *http.Request) { formPassword := req.FormValue("password") formUsername := req.FormValue("username") databaseUser, err := repository.UserRepository().FindByUsername(formUsername) if err != nil { http.Error(w, http.StatusText(405), 405) } err = bcrypt.CompareHashAndPassword([]byte(databaseUser.Password), []byte(formPassword)) if err != nil { log.Println("Wrong password") notAuthenticatedRedirect(w, req) } else { token, err := security.CreateUserToken(databaseUser) if err != nil { http.Error(w, http.StatusText(405), 405) } w.Header().Set("X-AUTH", token) authCookie := &http.Cookie{Name: "X-AUTH", Value: token, Path: "/"} http.SetCookie(w, authCookie) http.Redirect(w, req, "/", 302) } }
func Init() { config := LoadConfiguration("application.conf") repository.SetupMongo(config.Host, config.Port) repository.PostRepository() repository.UserRepository() }