Add Facebook login with Sorcery
The facebook key and secret defined in the sorcery config file are for testing only and need to be changed and not committed to source when app is ready to go live
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
# Place all the behaviors and hooks related to the matching controller here.
|
||||
# All this logic will automatically be available in application.js.
|
||||
# You can use CoffeeScript in this file: http://coffeescript.org/
|
||||
@@ -0,0 +1,3 @@
|
||||
// Place all the styles related to the Oauths controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: http://sass-lang.com/
|
||||
@@ -0,0 +1,32 @@
|
||||
class OauthsController < ApplicationController
|
||||
skip_before_filter :require_login
|
||||
|
||||
# sends the user on a trip to the provider,
|
||||
# and after authorizing there back to the callback url.
|
||||
def oauth
|
||||
login_at(auth_params[:provider])
|
||||
end
|
||||
|
||||
def callback
|
||||
provider = auth_params[:provider]
|
||||
if @user = login_from(provider)
|
||||
redirect_to root_path, :notice => "Logged in with #{provider.titleize}!"
|
||||
else
|
||||
begin
|
||||
@user = create_from(auth_params[:provider])
|
||||
|
||||
reset_session
|
||||
auto_login(@user)
|
||||
redirect_to root_path, :notice => "Signed up with #{provider.titleize}!"
|
||||
rescue
|
||||
redirect_to root_path, :alert => "Failed to login with #{provider.titleize}!"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def auth_params
|
||||
params.permit(:code, :provider)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,2 @@
|
||||
module OauthsHelper
|
||||
end
|
||||
@@ -0,0 +1,3 @@
|
||||
class Authentication < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
end
|
||||
+5
-1
@@ -1,5 +1,7 @@
|
||||
class User < ActiveRecord::Base
|
||||
authenticates_with_sorcery!
|
||||
authenticates_with_sorcery! do |config|
|
||||
config.authentications_class = Authentication
|
||||
end
|
||||
|
||||
validates :password, presence: true, confirmation: true, length: { minimum: 3 }, unless: ("id?" || "password_confirmation?")
|
||||
validates :password_confirmation, presence: true, unless: ("id?" || "password?")
|
||||
@@ -16,5 +18,7 @@ class User < ActiveRecord::Base
|
||||
|
||||
has_many :user_organization_relationships
|
||||
has_many :organizations, through: :user_organization_relationships
|
||||
has_many :authentications, :dependent => :destroy
|
||||
accepts_nested_attributes_for :authentications
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
%h1 Oauths#callback
|
||||
%p Find me in app/views/oauths/callback.html.haml
|
||||
@@ -0,0 +1,2 @@
|
||||
%h1 Oauths#oauth
|
||||
%p Find me in app/views/oauths/oauth.html.haml
|
||||
@@ -8,4 +8,6 @@
|
||||
Create an Account
|
||||
%p
|
||||
#{_ 'user.why_register', 'paragraph'}
|
||||
= link_to 'Login with Facebook', auth_at_provider_path(:provider => :facebook)
|
||||
= render '/users/form'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user