mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Use sign_in and authenticate methods.
This commit is contained in:
parent
9051d842c0
commit
b0a2da72b5
7 changed files with 34 additions and 37 deletions
|
@ -22,7 +22,7 @@ class ConfirmationsController < ApplicationController
|
|||
self.resource = resource_class.confirm!(:confirmation_token => params[:confirmation_token])
|
||||
|
||||
if resource.errors.empty?
|
||||
sign_in_automatically(resource, resource_name)
|
||||
sign_in(resource_name, resource)
|
||||
set_flash_message :success, :confirmed
|
||||
redirect_to root_path
|
||||
else
|
||||
|
|
|
@ -28,7 +28,7 @@ class PasswordsController < ApplicationController
|
|||
self.resource = resource_class.reset_password!(params[resource_name])
|
||||
|
||||
if resource.errors.empty?
|
||||
sign_in_automatically(resource, resource_name)
|
||||
sign_in(resource_name, resource)
|
||||
set_flash_message :success, :updated
|
||||
redirect_to root_path
|
||||
else
|
||||
|
|
|
@ -9,7 +9,7 @@ class SessionsController < ApplicationController
|
|||
|
||||
# POST /resource/sign_in
|
||||
def create
|
||||
if sign_in(resource_name)
|
||||
if authenticate(resource_name)
|
||||
set_flash_message :success, :signed_in
|
||||
redirect_back_or_to root_path
|
||||
else
|
||||
|
|
|
@ -4,7 +4,7 @@ module Devise
|
|||
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
helper_method :warden, :signed_in?, :authenticated?,
|
||||
helper_method :warden, :signed_in?,
|
||||
*Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?"] }.flatten
|
||||
end
|
||||
end
|
||||
|
@ -14,29 +14,31 @@ module Devise
|
|||
request.env['warden']
|
||||
end
|
||||
|
||||
# Sign in a user through warden, but does not take any action (like
|
||||
# redirect).
|
||||
def sign_in(scope)
|
||||
# Attempts to authenticate the given scope by running authentication hooks,
|
||||
# but does not redirect in case of failures.
|
||||
def authenticate(scope)
|
||||
warden.authenticate(:scope => scope)
|
||||
end
|
||||
|
||||
# Check if a user is authenticated.
|
||||
def sign_in!(scope)
|
||||
# Attempts to authenticate the given scope by running authentication hooks,
|
||||
# redirecting in case of failures.
|
||||
def authenticate!(scope)
|
||||
warden.authenticate!(:scope => scope)
|
||||
end
|
||||
|
||||
# Proxy to the authenticated? method on warden.
|
||||
# Check if the given scope is signed in session, without running
|
||||
# authentication hooks.
|
||||
def signed_in?(scope)
|
||||
warden.authenticated?(scope)
|
||||
end
|
||||
|
||||
# Set the warden user with the scope, sign in the resource automatically
|
||||
# (without credentials).
|
||||
def sign_in_automatically(resource, scope)
|
||||
# Set the warden user with the scope, signing in the resource automatically,
|
||||
# without running hooks.
|
||||
def sign_in(scope, resource)
|
||||
warden.set_user(resource, :scope => scope)
|
||||
end
|
||||
|
||||
# Sign out based on scope
|
||||
# Sign out based on scope.
|
||||
def sign_out(scope, *args)
|
||||
warden.raw_session.inspect # Without this inspect here. The session does not clear.
|
||||
warden.logout(scope, *args)
|
||||
|
@ -52,27 +54,23 @@ module Devise
|
|||
# User => :authenticable
|
||||
# Admin => :authenticable
|
||||
#
|
||||
# Generated Filters:
|
||||
# sign_in_user!
|
||||
# sign_in_admin!
|
||||
# Generated methods:
|
||||
# authenticate_user! # Signs user in or redirect
|
||||
# authenticate_admin! # Signs admin in or redirect
|
||||
# user_signed_in? # Checks whether there is an user signed in or not
|
||||
# admin_signed_in? # Checks whether there is an admin signed in or not
|
||||
# current_user # Current signed in user
|
||||
# current_admin # Currend signed in admin
|
||||
# user_session # Session data available only to the user scope
|
||||
# admin_session # Session data available only to the admin scope
|
||||
#
|
||||
# Use:
|
||||
# before_filter :sign_in_user! # Tell devise to use :user map
|
||||
# before_filter :sign_in_admin! # Tell devise to use :admin map
|
||||
#
|
||||
# Generated helpers:
|
||||
# sign_in_user! # Checks whether there is an user signed in or not
|
||||
# sign_in_admin! # Checks whether there is an admin signed in or not
|
||||
# user_signed_in? # Checks whether there is an user signed in or not
|
||||
# admin_signed_in? # Checks whether there is an admin signed in or not
|
||||
# current_user # Current signed in user
|
||||
# current_admin # Currend signed in admin
|
||||
# user_session # Session data available only to the user scope
|
||||
# admin_session # Session data available only to the admin scope
|
||||
# before_filter :authenticate_user! # Tell devise to use :user map
|
||||
# before_filter :authenticate_admin! # Tell devise to use :admin map
|
||||
#
|
||||
Devise.mappings.each_key do |mapping|
|
||||
class_eval <<-METHODS, __FILE__, __LINE__
|
||||
def sign_in_#{mapping}!
|
||||
def authenticate_#{mapping}!
|
||||
warden.authenticate!(:scope => :#{mapping})
|
||||
end
|
||||
|
||||
|
|
|
@ -54,12 +54,12 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
|||
|
||||
test 'proxy user_authenticate! to authenticate with user scope' do
|
||||
@mock_warden.expects(:authenticate!).with(:scope => :user)
|
||||
@controller.sign_in_user!
|
||||
@controller.authenticate_user!
|
||||
end
|
||||
|
||||
test 'proxy admin_authenticate! to authenticate with admin scope' do
|
||||
@mock_warden.expects(:authenticate!).with(:scope => :admin)
|
||||
@controller.sign_in_admin!
|
||||
@controller.authenticate_admin!
|
||||
end
|
||||
|
||||
test 'proxy user_authenticated? to authenticate with user scope' do
|
||||
|
@ -90,8 +90,7 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
|||
end
|
||||
|
||||
test 'sign in automatically proxy to set user on warden' do
|
||||
user = OpenStruct.new
|
||||
@mock_warden.expects(:set_user).with(user, :scope => :user).returns(true)
|
||||
@controller.sign_in_automatically(user, :user)
|
||||
@mock_warden.expects(:set_user).with(user = mock, :scope => :user).returns(true)
|
||||
@controller.sign_in(:user, user)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
class AdminsController < ApplicationController
|
||||
before_filter :sign_in_admin!
|
||||
before_filter :authenticate_admin!
|
||||
|
||||
def index
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
class UsersController < ApplicationController
|
||||
before_filter :sign_in_user!
|
||||
before_filter :authenticate_user!
|
||||
|
||||
def index
|
||||
user_session[:cart] = "Cart"
|
||||
|
|
Loading…
Reference in a new issue