Skip to content

Instantly share code, notes, and snippets.

@stammy
Forked from aliang/application_controller.rb
Created April 23, 2011 09:36
Show Gist options
  • Save stammy/938501 to your computer and use it in GitHub Desktop.
Save stammy/938501 to your computer and use it in GitHub Desktop.
Force SSL on Devise routes only, then redirect back
class ApplicationController < ActionController::Base
# Tell Devise to redirect after sign_in
def after_sign_in_path_for(resource_or_scope)
some_url(:protocol => 'http')
end
# Tell Devise to redirect after sign_out
def after_sign_out_path_for(resource_or_scope)
some_url(:protocol => 'http')
end
end
class RegistrationsController < Devise::RegistrationsController
private
# Tell Devise to redirect after account update
def after_update_path_for(resource)
me_url(:protocol => 'http')
end
end
JumpTask::Application.routes.draw do
# This class just makes the SSL constraint DRY
class SSL
def self.matches?(request)
# This way you don't need SSL for your development server
return true unless Rails.env.production?
request.ssl?
end
end
# Require SSL for Devise
constraints SSL do
devise_for :users, :controllers => {
:registrations => 'registrations'
} do {
# your custom Devise routes here
}
end
end
# Redirect to SSL from non-SSL so you don't get 404s
# Repeat for any custom Devise routes
match "/users(/*path)", :to => redirect { |_, request|
"https://" + request.host_with_port + request.fullpath }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment