Rails Devise Login 401 Unauthorized - ruby-on-rails

What I am trying to do
I am trying to set up the Authentication with Devise in the Session Controller, methods new and create.
I am using Rails 5 and I customized the View new.html.erb.
What is the problem
I am not able to authenticate successfully after signing out from the Account. The problem is connected with the sign_in_params hash, which is passed blank to the new method of the session_controller
Testing of the functionality
I conducted a test of the Login, by including a breakpoint in my Account::SessionsController, I noticed that the sign_in_params hash was blank, then the program exits with 401 Unauthorized.
8: def new
9: self.resource = resource_class.new(sign_in_params)
=> 10: clean_up_passwords(resource)
11: yield resource if block_given?
12: respond_with(resource, serialize_options(resource))
13: end
sign_in_params => {}
My Code
I was able by resetting my code to identify the cause of my problem. It is connected to the custom Devise View that I modified in app/views/devise/sessions/new.html.erb
This is the code:
<form class="login-form" action="index.html">
<div class="login-wrap">
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<p class="login-img"><i class="icon_lock_alt"></i></p>
<div class="field">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_profile"></i></span>
<%= f.email_field :email, autofocus: true, :class => "form-control", :placeholder => "Username" %>
<!--<input type="text" class="form-control" placeholder="Username" autofocus>-->
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_key_alt"></i></span>
<%= f.password_field :password, autocomplete: "off", :class => "form-control", :placeholder => "Password" %>
</div>
<% if devise_mapping.rememberable? -%>
<label class="checkbox">
<%= f.check_box :remember_me %>
<%= f.label :remember_me, :class => "remember_me_Label" %>
<span class="pull-right"> <%= link_to "Forgot Password?", {}, :id => "lostPw" %></span>
</label>
<% end -%>
<%= f.submit "Login", :class => "btn btn-primary btn-lg btn-block" %>
<%= link_to "Signup", new_account_registration_path, :method => :get, class: "btn btn-info btn-lg btn-block signuptext" %>
<% end %>
</div>
</form>
In the log, I have the following important informations, but no errors:
Started GET "/account/index.html?utf8=%E2%9C%93&authenticity_token=s1aA%2FNcZo1HIkF1qJOlrIAlM9rJM3y1ced8tQLiWnGsSugBVs0AYKVJR8QLa1I%2BT500sYu7H1%2BPQ2UwH3JG2ew%3D%3D&account%5Bemail%5D=test%40email.com&account%5Bpassword%5D=[FILTERED]&account%5Bremember_me%5D=0&commit=Login" for 127.0.0.1 at 2017-02-06 19:16:04 +0100 (pid:8311)
2017-02-06 19:16:04.557 [fyi] Processing by AccountController#index as HTML (pid:8311)
2017-02-06 19:16:04.557 [fyi] Parameters: {"utf8"=>"✓", "authenticity_token"=>"s1aA/NcZo1HIkF1qJOlrIAlM9rJM3y1ced8tQLiWnGsSugBVs0AYKVJR8QLa1I+T500sYu7H1+PQ2UwH3JG2ew==", "account"=>{"email"=>"test#email.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Login"} (pid:8311)
2017-02-06 19:16:04.558 [fyi] Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms) (pid:8311)
Following is the solution of the problem
Thanks
Fabrizio Bertoglio

Solution
The problem is in the html code of new.html.erb, as I did not remove the html <form> tags from the html and this would cause problems with my <%= form_for > <% end %> rails tag. This is the reason why strong_params is empty.
The form was not submitting any value to the controller, the problem could have been easily discovered by debugging directly the new.html.erb view instead of the controller.
Was helpful having the input from Max, that suggested me to restart all over with Devise, so that I could understand that the problem was connected with the form and not with rails strong parameters.
This is the correct new.html.erb.
<%= form_for(resource, as: resource_name, url: session_path(resource_name), html: { class: "login-form"}) do |f| %>
<div class="login-wrap">
<p class="login-img"><i class="icon_lock_alt"></i></p>
<div class="input-group">
<span class="input-group-addon"><i class="icon_profile"></i></span>
<%#= f.label :email %>
<%= f.email_field :email, autofocus: true, :class => "form-control", :placeholder => "Username" %>
<!--<input type="text" class="form-control" placeholder="Username" autofocus>-->
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_key_alt"></i></span>
<%#= f.label :password %>
<%= f.password_field :password, autocomplete: "off", :class => "form-control", :placeholder => "Password" %>
</div>
<% if devise_mapping.rememberable? -%>
<label class="checkbox">
<%= f.check_box :remember_me %>
<%= f.label :remember_me, :class => "remember_me_Label" %>
<span class="pull-right"> <%= link_to "Forgot Password?", {}, :id => "lostPw" %></span>
</label>
<% end -%>
<%= f.submit "Login", :class => "btn btn-primary btn-lg btn-block" %>
<%= link_to "Signup", new_registration_path(resource_name), :method => :get, class: "btn btn-info btn-lg btn-block signuptext" %>
<div class="links ">
<%= render "devise/shared/links2" %>
</div>
</div>
<% end %>
<div class="text-right">
<div class="credits">
<!--
All the links in the footer should remain intact.
You can delete the links only if you purchased the pro version.
Licensing information: https://bootstrapmade.com/license/
Purchase the pro version form: https://bootstrapmade.com/buy/?theme=NiceAdmin
-->
Business Bootstrap Themes by BootstrapMade
</div>
</div>
</div>

Related

devise Register one users sign up, but need two different register paths

As i been work for this project for while, as this one was not on the scopes, until yesterday, as the clients and project manager change mind as they want two different signup as it was one single sign up before but now want change two, but i was thinking it possible use same one user, but different path
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h2 class="text-center">Sign up</h2>
</div>
<div class="col-sm-offset-4 col-sm-4 margin-button-bottom">
<div class="col-sm-12 text center">
<div class="inner-addon right-addon">
<i class="custom custom-icon-arrow-right"></i>
<%= link_to "I want hire Equipment", new_user_registration_path, class: "btn btn-black-free-account btn-lg btn-block", role:"button" %>
</div>
</div>
</div>
<div class="col-sm-offset-4 col-sm-4 margin-button-bottom">
<div class="col-sm-12 text center">
<div class="inner-addon right-addon">
<i class="custom custom-icon-arrow-right"></i>
<p><%= link_to "I represent a business with equipment available for hire", new_user_registration_path, class: "btn btn-black-free-account btn-lg btn-block", role:"button" %></p>
</div>
</div>
</div>
<br/>
so if this button "I represent a business with equipment available for hire" show form same as user but add field is Company
Here is two different
devise register new.html.erb
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h2 class="text-center">Sign up</h2>
<br />
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render 'shared/regmessage' %>
<div class="form-group">
<%= f.text_field :fullname, autofocus: true, placeholder: "Full name", class: "form-control", autocomplete: "fullname" %>
</div>
<div class="form-group">
<%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control", autocomplete: "email" %>
</div>
<div class="form-group">
<% if #minimum_password_length %>
<em>(
<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, placeholder: "Password", class: "form-control", autocomplete: "new-password" %>
</div>
<div class="actions">
<%= f.submit "Sign up", class: "btn btn-black-free-account btn-block" %>
</div>
<% end %>
<hr />
<%= link_to user_facebook_omniauth_authorize_path, class: "btn btn-block btn-social btn-facebook" do %>
<span class="fa fa-facebook"></span> Sign in with Facebook
<% end %>
<hr />
<%= link_to user_google_oauth2_omniauth_authorize_path, class: "btn btn-block btn-social btn-google" do %>
<span class="fa fa-google"></span> Sign in with Google
<% end %>
<br/>
</div>
</div>
same as above but add is company field in the new_company.html.erb
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h2 class="text-center">Sign up</h2>
<br />
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render 'shared/regmessage' %>
<div class="form-group">
<%= f.text_field :company, autofocus: true, placeholder: "Company Name", class: "form-control", autocomplete: "Company-name" %>
</div>
<div class="form-group">
<%= f.text_field :fullname, autofocus: true, placeholder: "Full name", class: "form-control", autocomplete: "fullname" %>
</div>
<div class="form-group">
<%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control", autocomplete: "email" %>
</div>
<div class="form-group">
<% if #minimum_password_length %>
<em>(
<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, placeholder: "Password", class: "form-control", autocomplete: "new-password" %>
</div>
<div class="actions">
<%= f.submit "Sign up", class: "btn btn-black-free-account btn-block" %>
</div>
<% end %>
<br/>
<%= link_to user_facebook_omniauth_authorize_path, class: "btn btn-block btn-social btn-facebook" do %>
<span class="fa fa-facebook"></span> Sign in with Facebook
<% end %>
<br/>
<%= link_to user_google_oauth2_omniauth_authorize_path, class: "btn btn-block btn-social btn-google" do %>
<span class="fa fa-google"></span> Sign in with Google
<% end %>
<br/>
</div>
</div>
Route.rb
devise_for :users,
path: '',
path_names: {sign_in: 'login', sign_out: 'logout', edit: 'profile', sign_up: 'registration'},
:controllers => {omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations', }
register controller.rb
class RegistrationsController < Devise::RegistrationsController
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
end
I was thinking something like java-script or controller if i click business button then the route will know and add company field into new.html.erb rather decided to go which one of two new and new company.html.erb
maybe new ID in the new.html.erb or need configure at route or controller?
In your registrations controller set a flag variable #for_company for example based on the params[:for_company] presence:
class RegistrationsController < Devise::RegistrationsController
def new
#for_company = params[:for_company].present?
super
end
end
And in your Sign Up page simply add for_company: true param to a link to a company sign up:
<%= link_to "I represent a business with equipment available for hire", new_user_registration_path(for_company: true), class: "..." %>
Then just show :company field in case if #for_company is true.

Customizing Devise sign_in authentication with bootstrap

I am using Devise for authentication, but I want to use my own form input.
I have checked that Devise's sign_in works. I was able to login with this on localhost:3000/users/sign_in :
<h2>Log in</h2>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>
<div class="actions">
<%= f.submit "Log in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
I created on index page this form, but I could not get it to work (I was not able to login with the code below):
<form class="form-inline">
<div class="form-group">
<input type="email" class="form-control" id="user_email" placeholder="email?">
</div>
<div class="form-group">
<label for="exampleInputPassword2">/</label>
<input type="text" class="form-control" id="user_password" placeholder="password?">
</div>
<button type="submit" class="btn btn-primary">Sign In</button>
</form>
I read formhelper on rubyonrails.org website and bootstrap's css page but it still did not clear up how to use bootstrap's form.
Here are some of the things I don't understand:
The following code <input type="email" class="form-control" id="user_email" placeholder="email?">, what should I put for type= and id=? How can I tell bootstrap's form to submit a post request for my session? I can somewhat read and interpret Devise' form: <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> by looking at it: resource is :user, the url is being sent to user's session. I could not find information online about bootstrap's form. How can I integrate my own custom form using bootstrap with Devise' session?
Your problem has not really something to do with Bootstrap at first. I would try to get the form working, before you add bootstrap.
The from could be created like this:
<%= form_for(:user, :url => session_path(:user)) do |f| %>
<%= f.text_field :email %>
<%= f.password_field :password %>
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
<%= f.submit 'Sign in' %>
<%= link_to "Forgot your password?", new_password_path(:user) %>
<% end %>
Now you add some bootstrap flavor to this form:
<%= form_for(:user, :url => session_path(:user)) do |f| %>
<div class="form-group">
<%= f.text_field :email, placeholder: 'example#example.com' %>
</div>
<div class="form-group">
<%= f.password_field :password %>
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<%= f.submit 'Sign in', class: 'btn btn-primary' %>
<%# link_to "Forgot your password?", new_password_path(:user) %>
<% end %>
Most of the information I is from this page of the devise wiki.

Customize the devise edit method - Rails 4

I have a problem with customising Devise method. I want to edit an existing profile. But it doesn't change.
Here's my code
My controller:
class DashboardController < Users::RegistrationsController::ApplicationController
def index
end
def update
#user = current_user
if #user.update_attributes(user_params)
redirect_to 'index', :notice => "Successfully updated profile."
else
Rails.logger.info(#user.errors.messages.inspect)
render :action => 'edit'
end
end
def edit
end
private
def user_params
params.require(:user).permit(:avatar, :username, :email, :password, :password_confirmation)
end
end
My edit view
<div class="container sign-in-up">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<br>
<div class="text-center">
<h1><b>Edit the profile</b> </h2>
<div class="tab-content">
<div class="tab-pane fade in active" id="new">
<fieldset>
<div class="form-group">
<div class="right-inner-addon">
<i class="fa fa-envelope"></i>
<%= form_for current_user, url: edit_dashboard_path(current_user), html: { method: :get, multipart: true } do |f|%>
<div class="home_field " >
<%= f.label :username%>
<br/>
<%= f.text_field :username, autofocus: true, :placeholder => 'Username', class:"form-control input-lg " %>
</div>
<div class="home_field ">
<%= f.label :email %>
<br/>
<%= f.email_field :email, autofocus: true, :placeholder => 'Email' , class:"form-control input-lg" %>
</div>
<div class="home_field ">
<%= f.label :password%>
<br/>
<%= f.password_field :password, :placeholder => 'Password', class:"form-control input-lg"%>
</div>
<div class="home_field ">
<%= f.label :password_confirmation%>
<br/>
<%= f.password_field :password_confirmation,:placeholder => 'Confirm your password' , class:"form-control input-lg"%>
</div>
<div class ="home_field">
<%= f.file_field :avatar %>
<br>
</div>
<div>
</div>
<div class="home_field">
<%= f.submit 'Edit the profile', class: "btn btn-primary btn-lg btn-block btn-group" %>
<br/>
</div>
<% end %>
</fieldset>
It can be a problem in my layout code, where i make the link to the edit page
<%= link_to "Edit the Profile", edit_dashboard_path %>
Your form is submitting to the edit dashboard using a get, which is the same page you are on. Instead you want to update the current user something like this:
<%= form_for current_user, html: { multipart: true } do |f|%>
This will default to the update attribute and CRUD action defined in your routes file for a user
I would suggest changing this line:
<%= form_for current_user, url: edit_dashboard_path(current_user), html: { method: :get, multipart: true } do |f|%>
In the url use your path to the 'update' method, not the edit (use rake routes on your command line)
In the method use :put instead of :get

Customize Bootstrap Form In Rails View

I can't figure out the proper formatting to modify my form partial in my view to look like the bootstrap form below with a appended button like this;
<div class="col-lg-9">
<div class="input-group input-group-lg">
<input type="text" class="form-control">
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
</div><!-- /input-group -->
I would like the form to post to :home and the appended button to replace the default f.submit button in a Scaffold. Here's a Scaffold form partial that I'd like to modify.
<%= form_for #anesu, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :home, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :home, :class => 'text_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
anesus_path, :class => 'btn' %>
</div>
<% end %>
Please guide me on how to format my code and the right methods to use
<%= form_for #anesu do |f| %>
<div class="col-lg-9">
<div class="input-group input-group-lg">
<%= f.text_field :home, :class => 'form-control' %>
<span class="input-group-btn">
<%= f.submit "Go!", :class => 'btn btn-primary' %>
</span>
</div>
</div>
<% end %>

Edit form wrongly being submitted as GET - Rails

I'm trying to edit using a form <%= form_for #agent, :action => 'agents/update', :method => :put do |f| %> but when I click on Save Changes the form is submitted as a GET request instead of a PUT to the update action.
I have resources :agents in my routes.rb file.
In agents_controller.rb the show and update actions are:
def show
#agent = Agent.find(params[:id])
#subscription = #agent.subscription
end
def edit
#agent = Agent.find(params[:id])
#subscription = #agent.subscription
end
def update
#agent = Agent.find(params[:id])
#subscription = #agent.subscription
#agent.update_attributes(:agent)
end
It's pretty depressing when you can't get a standard edit form working, any help appreciated.
EDIT
agents/edit.html.erb
<%= form_for #agent do |f| %>
<ul>
<% for message in #agent.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
<div class="control-group">
<label class="control-label" style="width:200px;margin-right:20px"><strong>First name</strong></label>
<div class="controls">
<%= f.text_field :first_name, :placeholder => 'First name', :class => '', :style => '' %>
</div>
</div>
<div class="control-group">
<label class="control-label" style="width:200px;margin-right:20px"><strong>Last name</strong></label>
<div class="controls">
<%= f.text_field :last_name, :placeholder => 'Last name', :class => '', :style => '' %>
</div>
</div>
<div class="control-group">
<label class="control-label" style="width:200px;margin-right:20px"><strong>Email</strong></label>
<div class="controls">
<%= f.text_field :email, :placeholder => 'Your email', :class => '', :style => '' %>
</div>
</div>
<div class="control-group">
<label class="control-label" style="width:200px;margin-right:20px"><strong>Phone</strong></label>
<div class="controls">
<%= f.text_field :phone, :placeholder => 'Your phone number', :class => '', :style => '' %>
</div>
</div>
<div class="form-actions">
<%= f.submit "Save Changes", :class => 'btn btn-success' %>
<%= link_to 'Cancel', leads_path, :class => 'btn' %>
</div>
<% end %>
You shouldn't need to supply a method nor an action for a simple form like this. A simple
<% form_for #agent do |f| %>
should do you. Have you tried that? Are there any other forms on the page that could be conflicting? If so, try removing those other forms first, then see how that goes.
Fixed it. This post covered the exact same issue: HTML <form> tag causing Rails form to submit a GET instead of POST request
The GET request is being caused by a form tag around the rails form which was there to add the twitter bootstrap styling class "form-horizontal". Removed the extra form tag and it works fine.

Resources