Created
August 21, 2010 13:00
-
-
Save tekknikk/542276 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SIGN UP | |
curl -H "Content-Type:application/json" -H "Accept:application/json" \ | |
-d "{\"user\":{\"password_confirmation\":\"12345678\", \"password\":\"12345678\", \"email\":\"[email protected]\"}}" \ | |
http://your_server.com/users | |
SIGN IN | |
curl -H "Content-Type:application/json" -H "Accept:application/json" \ | |
-d "{\"user\":{\"remember_me\":\"0\", \"password\":\"12345678\", \"email\":\"[email protected]\"}}" \ | |
http://your_server.com/users/sign_in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RegistrationsController < ApplicationController | |
prepend_before_filter :require_no_authentication, :only => [ :new, :create ] | |
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy] | |
include Devise::Controllers::InternalHelpers | |
# WITH JSON RESPONSE | |
def sign_in_and_redirect(resource_or_scope, resource=nil, skip=false) | |
scope = Devise::Mapping.find_scope!(resource_or_scope) | |
resource ||= resource_or_scope | |
sign_in(scope, resource) unless skip | |
respond_to do |format| | |
format.html {redirect_to stored_location_for(scope) || after_sign_in_path_for(resource) } | |
format.json { render :json => { :success => true, :session_id => request.session_options[:id], :current_user => current_user} } | |
end | |
end | |
# GET /resource/sign_in | |
def new | |
build_resource | |
render_with_scope :new | |
end | |
# WITH JSON RESPONSE | |
def create | |
build_resource | |
if resource.save | |
set_flash_message :notice, :signed_up | |
sign_in_and_redirect(resource_name, resource) | |
else | |
respond_to do |format| | |
format.html { render_with_scope :new } | |
format.json { render :json => {:success => false, | |
:error => resource.errors, :status => :unprocessable_entity}} | |
end | |
end | |
end | |
# GET /resource/edit | |
def edit | |
render_with_scope :edit | |
end | |
# PUT /resource | |
def update | |
if self.resource.update_with_password(params[resource_name]) | |
set_flash_message :notice, :updated | |
redirect_to after_sign_in_path_for(self.resource) | |
else | |
render_with_scope :edit | |
end | |
end | |
# DELETE /resource | |
def destroy | |
self.resource.destroy | |
set_flash_message :notice, :destroyed | |
sign_out_and_redirect(self.resource) | |
end | |
protected | |
# Authenticates the current scope and dup the resource | |
def authenticate_scope! | |
send(:"authenticate_#{resource_name}!") | |
self.resource = send(:"current_#{resource_name}").dup | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionsController < ApplicationController | |
prepend_before_filter :require_no_authentication, :only => [ :new, :create ] | |
include Devise::Controllers::InternalHelpers | |
# WITH JSON RESPONSE | |
def sign_in_and_redirect(resource_or_scope, resource=nil, skip=false) | |
scope = Devise::Mapping.find_scope!(resource_or_scope) | |
resource ||= resource_or_scope | |
sign_in(scope, resource) unless skip | |
respond_to do |format| | |
format.html {redirect_to stored_location_for(scope) || after_sign_in_path_for(resource) } | |
format.json { render :json => { :success => true, :session_id => request.session_options[:id], :resource => resource } } | |
end | |
end | |
# GET /resource/sign_in | |
def new | |
unless flash[:notice].present? | |
Devise::FLASH_MESSAGES.each do |message| | |
set_now_flash_message :alert, message if params.try(:[], message) == "true" | |
end | |
end | |
build_resource | |
render_with_scope :new | |
end | |
# WITH JSON RESPONSE | |
def create | |
build_resource | |
if resource = authenticate(resource_name) | |
set_flash_message :notice, :signed_in | |
sign_in_and_redirect(resource_name, resource, true) | |
elsif [:custom, :redirect].include?(warden.result) | |
throw :warden, :scope => resource_name | |
else | |
set_now_flash_message :alert, (warden.message || :invalid) | |
clean_up_passwords(build_resource) | |
respond_to do |format| | |
format.html { render_with_scope :new } | |
format.json { render :json => {:success => false, :status => warden.message}} | |
end | |
end | |
end | |
# GET /resource/sign_out | |
def destroy | |
set_flash_message :notice, :signed_out if signed_in?(resource_name) | |
sign_out_and_redirect(resource_name) | |
end | |
protected | |
def clean_up_passwords(object) | |
object.clean_up_passwords if object.respond_to?(:clean_up_passwords) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment