mirror of
https://github.com/indentlabs/notebook.git
synced 2025-10-26 11:19:22 +00:00
Omniauth implementation.
This commit is contained in:
parent
f73f0f91e1
commit
9daaad1f5c
20
app/controllers/users/omniauth_callbacks_controller.rb
Normal file
20
app/controllers/users/omniauth_callbacks_controller.rb
Normal file
@ -0,0 +1,20 @@
|
||||
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
||||
|
||||
User::SOCIAL_SITES.each do |name|
|
||||
define_method name do
|
||||
social_site
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def social_site
|
||||
@user = OmniauthUser.from_omniauth(request.env["omniauth.auth"], current_user)
|
||||
if @user.errors.messages.present?
|
||||
redirect_to user_session_path, alert: @user.errors.full_messages.first
|
||||
else
|
||||
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: request.env["omniauth.auth"].provider
|
||||
sign_in_and_redirect @user, event: :authentication
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -2,4 +2,13 @@ class OmniauthUser < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
|
||||
validates :user, presence: true
|
||||
|
||||
def self.from_omniauth(auth, current_user)
|
||||
omniauth_user = find_by(provider: auth.provider, uid: auth.uid, email: auth.info.email)
|
||||
return omniauth_user.user if omniauth_user.present?
|
||||
|
||||
user = current_user || User.find_by(email: auth.info.email) || User.create(email: auth.info.email, password: Devise.friendly_token[0,20])
|
||||
user.omniauth_users.create(provider: auth.provider, uid: auth.uid, email: auth.info.email)
|
||||
user
|
||||
end
|
||||
end
|
||||
|
||||
@ -3,10 +3,13 @@ require 'digest/md5'
|
||||
##
|
||||
# a person using the Notebook.ai web application. Owns all other content.
|
||||
class User < ActiveRecord::Base
|
||||
SOCIAL_SITES = [:facebook, :google_oauth2, :twitter]
|
||||
|
||||
# Include default devise modules. Others available are:
|
||||
# :confirmable, :lockable, :timeoutable and :omniauthable
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :trackable, :validatable
|
||||
:recoverable, :rememberable, :trackable, :validatable,
|
||||
:omniauthable, omniauth_providers: SOCIAL_SITES
|
||||
|
||||
include HasContent
|
||||
include Authority::UserAbilities
|
||||
@ -30,6 +33,8 @@ class User < ActiveRecord::Base
|
||||
|
||||
has_many :content_change_events
|
||||
|
||||
has_many :omniauth_users
|
||||
|
||||
# TODO: Swap this out with a has_many when we transition from a scratchpad to users having multiple documents
|
||||
has_one :document
|
||||
|
||||
|
||||
@ -262,4 +262,8 @@ Devise.setup do |config|
|
||||
# When using OmniAuth, Devise cannot automatically set OmniAuth path,
|
||||
# so you need to do it manually. For the users scope, it would be:
|
||||
# config.omniauth_path_prefix = '/my_engine/users/auth'
|
||||
|
||||
config.omniauth :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET']
|
||||
config.omniauth :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET']
|
||||
config.omniauth :twitter, ENV['TWITTER_ID'], ENV['TWITTER_SECRET']
|
||||
end
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
Rails.application.routes.draw do
|
||||
|
||||
devise_for :users, :controllers => { registrations: 'registrations' }
|
||||
devise_for :users, controllers: { registrations: 'registrations', omniauth_callbacks: 'users/omniauth_callbacks' }
|
||||
resources :users
|
||||
|
||||
get '/unsubscribe/emails/:code', to: 'emails#one_click_unsubscribe'
|
||||
|
||||
Loading…
Reference in New Issue
Block a user