How to authenticate and sign in user in my controller

2,685 views
Skip to first unread message

Paul

unread,
Nov 7, 2010, 7:25:05 PM11/7/10
to Devise
Hi! I'm devise and rails newbie.
Everything is cool except one problem:

i have controller with action "auto" which named RequestController
in action "auto" is different for re
i need to let user log in on this page and if the credentials he
specified are wrong, show message and if he authenticated ok, hide log
in form

so i've put this code in related View:

<%= form_tag :controller=>"requests", :action => "auto" do %>

<% if @login_msg %>
<h4><%= @login_msg %> </h4>
<% end %>
<table class="login">
<tr>
<td class="left"><%= label_tag :email, t(:email)
%></td>
<td><%= text_field_tag :email,
nil, :title=>"Логин", :class=>"text" %></td>
</tr>

<tr>
<td class="left"><%= label_tag :password,
t(:password) %></td>
<td><%= password_field_tag :password,
nil, :title=>"Пароль", :class=>"text" %></td>
</tr>
</table>
<%= submit_tag
t(:sign_in).capitalize, :class=>"green_button centered tmarg" %>

<% end %>

and in action wrote this:

class RequestsController < ApplicationController
require 'devise/strategies/authenticatable'
authorize_resource

def auto
if request.post?
@user = warden.authenticate! :basic, params[:email],
params[:password]
sign_in(:user, @user)
end
end

"warden.authenticate! :basic, params[:email], params[:password]" is
wrong, i get error "Invalid strategy basic".

Please, could you tell me how to authenticate and sign in user and get
result in this action.

Thanks in advance.

Paul.

Nicholas Young

unread,
Nov 7, 2010, 8:51:21 PM11/7/10
to plataforma...@googlegroups.com
Doesn't devise's database_authenticatable do what you want? Generally, to implement authentication, you don't need to create your own controller.

Nicholas

Sent from my iPhone

Paul

unread,
Nov 7, 2010, 10:10:18 PM11/7/10
to Devise
Sorry i don't really understand what is database_authenticatable and
what is it for. What you mean?
I read that "database_authenticatable" is strategy that's all i know
about it.

My goal is to let user sign in on this page instead of using Devise's
sign in page. Without redirects.
So i use my controller.
And i think that there is a method that takes as arguments email and
password and sets authentication coockie and returns result true or
false so i can change behaviour of the page. O something like that.


On 8 ноя, 04:51, Nicholas Young <nicho...@nicholaswyoung.com> wrote:
> Doesn't devise's database_authenticatable do what you want? Generally, to implement authentication, you don't need to create your own controller.
>
> Nicholas
>
> Sent from my iPhone
>

Carlos Antonio da Silva

unread,
Nov 8, 2010, 4:46:38 AM11/8/10
to plataforma...@googlegroups.com
warden.authenticate! is going to trigger all registered strategies (including Devise ones), and it's going to call the Failure app and redirect. What does return the user is warden.authenticate, without the bang!, as you can see here: https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb#L48
--
At.
Carlos A. da Silva

Paul

unread,
Nov 9, 2010, 5:57:19 PM11/9/10
to Devise
Yes. thank you!
And how to pass login and password?

On 8 ноя, 12:46, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> warden.authenticate! is going to trigger all registered strategies
> (including Devise ones), and it's going to call the Failure app and
> redirect. What does return the user is warden.authenticate, without the
> bang!, as you can see here:https://github.com/plataformatec/devise/blob/master/lib/devise/contro...

Paul

unread,
Nov 10, 2010, 6:23:26 PM11/10/10
to Devise
I've solved this problem!

Really it's not good solution but it works.

So

I have this code in my View "auto.html.erb":

<%= form_for(:user, :url => user_session_path ) do |f| %>
<%= f.text_field :email %>
<%= f.password_field :password %>
<%= f.submit t(:sign_in).capitalize %>
<%= hidden_field_tag :go_back %>
<%# end %>

this form posts login and password and hidden field(it tells to
redirect back) to the sign in page

I've copied /Library/Ruby/Gems/1.8/gems/devise-1.1.3/app/controllers/
devise/sessions_controller.rb to my app/controllers/devise/
sessions_controller.rb

Then modified it's action "create" and added action "go_back_redirect"

def go_back_redirect
clean_up_passwords(build_resource)
session[:sign_in_result] = "failed"
redirect_to request.referer
end

# POST /resource/sign_in
def create
redirect_to request.referer if (params[:go_back] &&
current_user.nil?)
@dest = params[:go_back] ? "go_back_redirect" : "new"
resource = warden.authenticate!(:scope => resource_name, :recall
=> @dest)
set_flash_message :notice, :signed_in
if !params[:go_back]
sign_in_and_redirect(resource_name, resource)
else
sign_in(resource_name, resource)
session[:sign_in_result] = "ok"
redirect_to request.referer
end
end

and in my controller i have log in result ('failed' or 'ok') so i can
show message if login failed and user do not leave my page /requests/
auto

def auto
@msg1 = session[:sign_in_result]
end
Reply all
Reply to author
Forward
0 new messages