coder05
coder05

Reputation: 233

Error on User Authentication not working

I have a sessions_helper file that finds a user and if the user is found logins to the application. However, if the user is not found, it should give an error.

module SessionsHelper

  # Log a user in after authenticating, store in session
  def log_in(user)
    if user.id.nil?
      format.html { redirect_to signin_path, notice: 'Invalid Email or password' }
      format.json { head :no_content }
    else
      session[:user_id] = user.id
    end
  end
end

I have a sessions/unauth view.

<div class="col-md-4">
        <div class="panel panel-default">
            <div class="panel-heading">Log In!</div>
            <div class="panel-body">
                <%= form_for(:credentials, url: signin_path) do |f| %>
                    <div class="form-group">
                        <%= f.label :email%>
                        <%= f.email_field :email, class: 'form-control'%>
                    </div>
                        <div class="form-group">
                        <%= f.label :password%>
                        <%= f.password_field :password, class: 'form-control'%>
                    </div>
                    <%= submit_tag %>
                <% end %>
            </div>
        </div>
    </div>

This code allows user to login to app successfully, but if user is not found does not display an error on the application.

Upvotes: 1

Views: 94

Answers (2)

Richard Peck
Richard Peck

Reputation: 76784

You've not published the flash message in the view.

--

Flash

The Rails Flash is a type of session variable used to store data generated in actions. It's means to give you the ability to call alerts / notices in either the view or layout.

There are 3 types of flash, although I think you can use as many as you wish:

  • notice
  • alert
  • error

When you call ... notice: 'Invalid Email or password' }, you're telling rails to populate the flash[:notice] object.

If you want to show this in your view, you need to invoke it, as following:

<div class="col-md-4">
    <div class="panel panel-default">
        <!-- Error -->
        <%= flash[:notice] if flash[:notice] %>

        <!-- Login -->
        <div class="panel-heading">Log In!</div>
        <div class="panel-body">
            <%= form_for(:credentials, url: signin_path) do |f| %>
                <div class="form-group">
                    <%= f.label :email%>
                    <%= f.email_field :email, class: 'form-control'%>
                </div>
                    <div class="form-group">
                    <%= f.label :password%>
                    <%= f.password_field :password, class: 'form-control'%>
                </div>
                <%= submit_tag %>
            <% end %>
        </div>
    </div>
</div>

Upvotes: 1

syafiq faiz
syafiq faiz

Reputation: 188

you need to include this into your view

  <% if flash[:notice] %>
    <div class="notice"><%= flash[:notice] %></div>
  <% end %>

this code will display your error message.

read here

Upvotes: 1

Related Questions