Как мне получить сообщения об ошибках входа в Ruby on Rails 5.2? - PullRequest
0 голосов
/ 25 января 2019

Я использую Ruby на рельсах 5.2 с Mysql и Puma.Когда я пытаюсь ввести неверную информацию в окне проверки входа в систему, я не получаю никаких ошибок.Вместо этого я получаю кнопки, которые я могу нажимать, но ничего не делать.Если я вернусь на домашнюю страницу, то появится сообщение об ошибке, независимо от того, были ли flash.now или сообщения об ошибках flash.Я знаю, что отключение турбо-ссылок ничего не делает.

Я понятия не имею, почему это происходит, но это связано с 5.0, так как эта проблема не возникает в рельсах 3.2.14.Любой совет?

Код помощника:

module SessionsHelper

   private
      def extendTimelimit(pouch)
         #Extends the time limit for activation
         time_limit = 1.days.from_now.utc
         pouch.remember_token = SecureRandom.urlsafe_base64
         pouch.expiretime = time_limit

         #Emails the user the new activation token
         @pouch = pouch
         @pouch.save
         UserMailer.reset_time(@pouch.user).deliver
         flash[:success] = "Your time limit was extended and you received a new activation token."
         redirect_to activate_path
      end

      def activateAccount(pouch)
         #Activates the user's account
         pouch.activated = true
         @pouch = pouch
         @pouch.save

         #Emails the user's account the new information
         UserMailer.account_info(@pouch.user).deliver
         flash[:success] = "Your account has now been activated!"
         redirect_to login_path
      end

      def emailUser(loginUser)
         #Creates a random new password
         token = SecureRandom.urlsafe_base64
         loginUser.password = token
         loginUser.password_confirmation = token

         #Sends the user an email with their new password
         @user = loginUser
         @user.save
         UserMailer.reset_password(@user).deliver
         flash[:success] = "Your password was succesfully sent"
         redirect_to root_path
      end

      def createLoginCookie(loginUser, pouch)
         #Display the appropriate message to the user
         message = "Welcome back #{loginUser.vname}"
         if(pouch.signed_in_at == nil)
            message = "Greetings #{loginUser.vname} welcome to Duelingpets"
            #UserMailer.welcome(loginUser).deliver
         end
         flash[:success] = message

         #Set the user's sign in time to the current time
         pouch.signed_in_at = currentTime
         pouch.last_visited = nil
         pouch.signed_out_at = nil

         #Create the cookie for the current user
         time_limit = 2.days.from_now.utc
         pouch.expiretime = time_limit
         cookie_lifespan = time_limit + 1.month
         pouch.remember_token = SecureRandom.urlsafe_base64
         cookies[:remember_token] = {:value => pouch.remember_token, :expires => cookie_lifespan}

         #Set the current user to the loginUser
         @pouch = pouch
         @pouch.save
         self.current_user = loginUser
         redirect_to loginUser
      end

      def loginpage(type, pagemode)
         if(type == "loginpost")
            loginUser = User.find_by_login_id(params[:session][:login_id].downcase)
            if(loginUser && loginUser.pouch.activated && loginUser.authenticate(params[:session][:password]))
               if(loginUser.pouch.privilege != "Banned")
                  if(pagemode == "Admin" && loginUser.pouch.privilege == "Admin")
                     createLoginCookie(loginUser, pouch)
                  elsif(pagemode == "Beta" && loginUser.pouch.privilege != "User")
                     createLoginCookie(loginUser, pouch)
                  elsif(pagemode == "User")
                     createLoginCookie(loginUser, pouch)
                  else
                     if(pagemode == "Admin")
                        flash.now[:error] = "Only the admin can login at this time."
                        render "login"
                     elsif(pagemode == "Beta")
                        flash.now[:error] = "Only members of the beta class and higher can login."
                        render "login"
                     end
                  end
               else
                  flash.now[:error] = "You have been banned from this site."
                  render "login"
               end
            else
               flash.now[:error] = "Invalid login name/password combination!"
               render "login"
            end
         end
      end

      def mode(type)
         logoutExpiredUsers
         if(type == "destroy")
            logout_user
            redirect_to root_path
         elsif(type == "login" || type == "loginpost")
            displayGreeter("Login")
            allMode = Maintenancemode.find_by_id(1)
            betaMode = Maintenancemode.find_by_id(3)
            if(allMode.maintenance_on)
               loginpage(type, "Admin")
            elsif(betaMode.maintenance_on)
               loginpage(type, "Beta")
            else
               loginpage(type, "User")
            end
         elsif(type == "recover" || type == "recoverpost")
            displayGreeter("Recover")
            if(!current_user)
               if(type == "recoverypost")
                  vnameUser = User.find_by_vname(params[:session][:vname])
                  loginUser = User.find_by_login_id(params[:session][:login_id].downcase)
                  pouch = Pouch.find_by_id(loginUser.id)
                  if((loginUser && vnameUser) && (loginUser.id == vnameUser.id))
                     if(pouch.activated)
                        allMode = Maintenancemode.find_by_id(1)
                        if(allMode.maintenance_on)
                           if(loginUser.admin)
                              emailUser(loginUser)
                           else
                              flash.now[:error] = "Only the admin can recover at this time."
                              render "recover"
                           end
                        else
                           emailUser(loginUser)
                        end
                     else
                        flash.now[:error] = "Your account has not been activated yet!"
                        render "recover"
                     end
                  else
                     flash.now[:error] = "Invalid login name/vname combination!"
                     render "recover"
                  end
               end
            else
               flash[:error] = "You are already logged in!"
               redirect_to root_path
            end
         elsif(type == "activate" || type == "activatepost")
            displayGreeter("Activate")
            if(!current_user)
               if(type == "activatepost")
                  loginUser = User.find_by_login_id(params[:session][:login_id].downcase)
                  token = Pouch.find_by_remember_token(params[:session][:token])
                  pouch = Pouch.find_by_id(loginUser.id)
                  if((loginUser && token) && (loginUser.pouch.id == token.id))
                     if(!pouch.activated)
                        if(currentTime < pouch.expiretime)
                           allMode = Maintenancemode.find_by_id(1)
                           if(allMode.maintenance_on)
                              if(loginUser.admin)
                                 activateAccount(pouch)
                              else
                                 flash.now[:error] = "Only the admin can activate at this time."
                                 render "activate"
                              end
                           else
                              activateAccount(pouch)
                           end
                        else
                           flash.now[:error] = "The time limit has expired for account activation!"
                           render "activate"
                        end
                     else
                        flash.now[:error] = "This account has already been activated!"
                        render "activate"
                     end
                  else
                     flash.now[:error] = "Invalid login name/token combination!"
                     render "activate"
                  end
               end
            else
               flash[:error] = "You are already logged in!"
               redirect_to root_path
            end
         #Might consider calling this extendtimelimit
         elsif(type == "resettime" || type == "resettimepost")
            displayGreeter("Extend")
            if(!current_user)
               if(type == "resettimepost")
                  loginUser = User.find_by_login_id(params[:session][:login_id].downcase)
                  vnameUser = User.find_by_vname(params[:session][:vname])
                  pouch = Pouch.find_by_id(loginUser.id)
                  if((loginUser && vnameUser) && (loginUser.id == vnameUser.id))
                     if(!pouch.activated)
                        allMode = Maintenancemode.find_by_id(1)
                        if(allMode.maintenance_on)
                           if(loginUser.admin)
                              extendTimelimit(pouch)
                           else
                              flash.now[:error] = "Only the admin can extend the time limit at this time."
                              render "resettime"
                           end
                        else
                           extendTimelimit(pouch)
                        end
                     else
                        flash.now[:error] = "This account has already been activated!"
                        render "resettime"
                     end
                  else
                     flash.now[:error] = "Invalid login name/vname combination!"
                     render "resettime"
                  end
               end
            else
               flash[:error] = "You are already logged in!"
               redirect_to root_path
            end
         end
      end
end

Код здесь - это то, что вызывает мою страницу.

Метод входа:

<% provide(:h1, "Login") %>
<div class="pageformat">
   <p><%= image_tag(@artpage.art_url(:thumb)) %></p>
   <p><%= @artpage.message %></p>
</div>
<br>
<div class="pageformat">
<%= form_with(scope: :session, url: loginpost_path) do |f| %>
   <p><%= f.label :login_id, "Login Name" %></p>
   <p><%= f.password_field :login_id %></p>
   <p><%= f.label :password %></p>
   <p><%= f.password_field :password %></p>
   <p><%= f.submit "Login" %></p>
<% end %>
</div>
<br>
<div class="pageformat">
   <p class="formlinks">New User? <%= link_to "Register now!", register_path %></p>
   <p class="formlinks">New Account? <%= link_to "Activate now!", activate_path %></p>
   <p class="formlinks">Forgot Password? <%= link_to "Forgot Password", recover_path %></p>
   <br>
   <p class="formlinks"><%= link_to 'Back', root_path %></p>
</div>
...