mixing in some sorcery auth

This commit is contained in:
2015-10-07 22:03:31 -05:00
parent 63d4063392
commit 5a9c4a341a
23 changed files with 626 additions and 34 deletions

View File

@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

View File

@ -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/

View File

@ -31,7 +31,7 @@ form {
}
fieldset {
margin: 15px;
border: 0;
margin: 15px;
}
}

View File

@ -4,10 +4,4 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
respond_to :html, :json
def current_user
# temp
Person.new(id: 9999)
end
helper_method :current_user
end

View File

@ -0,0 +1,34 @@
class OauthsController < ApplicationController
skip_before_filter :require_login
def oauth
login_at(params[:provider])
end
def callback
provider = params[:provider]
@user = login_from(provider)
if @user
redirect_to root_path, notice: "Logged in from #{provider.titleize}!"
else
begin
@user = create_from(provider)
# NOTE: this is the place to add '@user.activate!'
# if you are using user_activation submodule
reset_session # protect from session fixation attack
auto_login(@user)
redirect_to root_path, notice: "Logged in from #{provider.titleize}!"
rescue
redirect_to root_path, alert: "Failed to login from #{provider.titleize}!"
end
end
end
private
def auth_params
params.permit(:code, :provider)
end
end

View File

@ -0,0 +1,2 @@
module OauthsHelper
end

View File

@ -0,0 +1,3 @@
class Authentication < ActiveRecord::Base
belongs_to :user
end

View File

@ -1,6 +1,9 @@
class Person < ActiveRecord::Base
authenticates_with_sorcery!
has_many :parenthoods
has_many :children, through: :parenthoods
has_many :authentications, dependent: :destroy
accepts_nested_attributes_for :authentications
accepts_nested_attributes_for :children, reject_if: :all_blank
validates :first_name, presence: true
@ -30,4 +33,10 @@ class Person < ActiveRecord::Base
def to_i
id
end
private
## SorceryCore expects the model to hold a crypted_password field
## Since we are only using external oAuth providers, faking this one out.
def crypted_password; end
end

View File

@ -1,2 +1,4 @@
%h2 Something helpful later
%p= link_to 'Login with Google', auth_at_provider_path(provider: :google)
%h2 Something more helpful later
%p= raw(ap @doc)

View File

View File

@ -0,0 +1,2 @@
%h1 Oauths#callback
%p Find me in app/views/oauths/callback.html.haml

View File

@ -0,0 +1,2 @@
%h1 Oauths#oauth
%p Find me in app/views/oauths/oauth.html.haml