[#9] Added login via GitHub
This commit is contained in:
parent
b539e73d2f
commit
7cc27f4e13
1
Gemfile
1
Gemfile
|
@ -7,6 +7,7 @@ gem 'pg'
|
|||
gem 'schema_plus'
|
||||
########
|
||||
gem 'devise'
|
||||
gem 'omniauth-github'
|
||||
gem 'pundit'
|
||||
gem 'rbtrace'
|
||||
|
||||
|
|
21
Gemfile.lock
21
Gemfile.lock
|
@ -169,6 +169,7 @@ GEM
|
|||
actionpack (>= 3.2, < 5)
|
||||
activesupport (>= 3.2, < 5)
|
||||
hashdiff (0.3.0)
|
||||
hashie (3.4.2)
|
||||
hirb (0.7.3)
|
||||
html2haml (2.0.0)
|
||||
erubis (~> 2.7.0)
|
||||
|
@ -197,6 +198,7 @@ GEM
|
|||
railties (>= 3.2)
|
||||
sprockets-rails
|
||||
json (1.8.3)
|
||||
jwt (1.5.0)
|
||||
kaminari (0.17.0)
|
||||
actionpack (>= 3.0.0)
|
||||
activesupport (>= 3.0.0)
|
||||
|
@ -238,6 +240,7 @@ GEM
|
|||
key_struct (~> 0.4)
|
||||
msgpack (0.7.6)
|
||||
multi_json (1.12.1)
|
||||
multi_xml (0.5.5)
|
||||
multipart-post (2.0.0)
|
||||
nest (1.1.2)
|
||||
redis
|
||||
|
@ -248,6 +251,12 @@ GEM
|
|||
rails (>= 3.1)
|
||||
nokogiri (1.6.7.2)
|
||||
mini_portile2 (~> 2.0.0.rc2)
|
||||
oauth2 (1.0.0)
|
||||
faraday (>= 0.8, < 0.10)
|
||||
jwt (~> 1.0)
|
||||
multi_json (~> 1.3)
|
||||
multi_xml (~> 0.5)
|
||||
rack (~> 1.2)
|
||||
octokit (4.3.0)
|
||||
sawyer (~> 0.7.0, >= 0.5.3)
|
||||
ohm (1.3.2)
|
||||
|
@ -256,6 +265,15 @@ GEM
|
|||
scrivener (~> 0.0.3)
|
||||
ohm-expire (0.1.3.2)
|
||||
ohm (>= 0.1.5)
|
||||
omniauth (1.2.2)
|
||||
hashie (>= 1.2, < 4)
|
||||
rack (~> 1.0)
|
||||
omniauth-github (1.1.2)
|
||||
omniauth (~> 1.0)
|
||||
omniauth-oauth2 (~> 1.1)
|
||||
omniauth-oauth2 (1.3.0)
|
||||
oauth2 (~> 1.0)
|
||||
omniauth (~> 1.2)
|
||||
orm_adapter (0.5.0)
|
||||
paperclip (4.3.6)
|
||||
activemodel (>= 3.2.0)
|
||||
|
@ -567,6 +585,7 @@ DEPENDENCIES
|
|||
octokit (~> 4.0)
|
||||
ohm (~> 1.3.2)
|
||||
ohm-expire (~> 0.1.3)
|
||||
omniauth-github
|
||||
paperclip
|
||||
pg
|
||||
puma
|
||||
|
@ -612,4 +631,4 @@ DEPENDENCIES
|
|||
will_paginate
|
||||
|
||||
BUNDLED WITH
|
||||
1.11.2
|
||||
1.13.1
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
||||
|
||||
def github
|
||||
oauthorize 'GitHub'
|
||||
end
|
||||
|
||||
def passthru
|
||||
render file: "#{Rails.root}/public/404.html", status: 404, layout: false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def oauthorize(kind)
|
||||
provider = kind.downcase
|
||||
@user = find_for_ouath(env["omniauth.auth"], current_user)
|
||||
if @user && @user.persisted?
|
||||
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", kind: action_name.classify
|
||||
sign_in_and_redirect @user, event: :authentication
|
||||
else
|
||||
session["devise.#{provider}_data"] = env["omniauth.auth"]
|
||||
redirect_to new_user_registration_url
|
||||
end
|
||||
end
|
||||
|
||||
def find_for_ouath(auth, resource=nil)
|
||||
provider, uid = auth['provider'], auth['uid']
|
||||
authentication = Authentication.find_or_initialize_by(provider: provider, uid: uid)
|
||||
if authentication.new_record?
|
||||
if user_signed_in? # New authentication method for current_user
|
||||
authentication.user = current_user
|
||||
else # Register new user from session
|
||||
case provider
|
||||
when 'github'
|
||||
name = auth['info']['nickname'] || auth['info']['name']
|
||||
else
|
||||
raise ActiveRecord::RecordNotFound
|
||||
end
|
||||
user = User.find_or_initialize_by email: auth['info']['email']
|
||||
if user.new_record?
|
||||
user.name = name
|
||||
user.uname = name.gsub(/\s/, '').underscore
|
||||
user.password = Devise.friendly_token[0,20]
|
||||
user.confirmed_at = Time.zone.now
|
||||
user.save
|
||||
end
|
||||
authentication.user = user
|
||||
end
|
||||
authentication.save
|
||||
end
|
||||
return authentication.user
|
||||
end
|
||||
|
||||
end
|
|
@ -15,6 +15,7 @@ class User < Avatar
|
|||
|
||||
devise :database_authenticatable, :registerable, :recoverable,
|
||||
:rememberable, :validatable, :lockable, :confirmable
|
||||
devise :omniauthable, omniauth_providers: %i(github)
|
||||
|
||||
has_one :notifier, class_name: 'SettingsNotifier', dependent: :destroy #:notifier
|
||||
has_one :builds_setting, class_name: 'UserBuildsSetting', dependent: :destroy
|
||||
|
|
|
@ -17,3 +17,5 @@
|
|||
= f.button :submit, t('layout.devise.shared_links.sign_in')
|
||||
.row
|
||||
= link_to t('layout.devise.shared_links.forgot_password'), new_password_path(resource_name)
|
||||
rd-widget-footer
|
||||
== render 'devise/shared/providers'
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
- if devise_mapping.omniauthable?
|
||||
.row
|
||||
.col-md-12
|
||||
.pull-left
|
||||
= t('.sign_up_with')
|
||||
.pull-right
|
||||
- resource_class.omniauth_providers.each do |provider|
|
||||
a.text-muted> href=omniauth_authorize_path(resource_name, provider)
|
||||
i.fa.fa-2x class="fa-#{provider}-square"
|
|
@ -184,6 +184,7 @@ Devise.setup do |config|
|
|||
# Add a new OmniAuth provider. Check the wiki for more information on setting
|
||||
# up on your models and hooks.
|
||||
# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
|
||||
config.omniauth :github, APP_CONFIG['keys']['github']['id'], APP_CONFIG['keys']['github']['secret'], scope: 'user:email'
|
||||
|
||||
# require 'openid/store/filesystem'
|
||||
# config.omniauth :openid, :name => 'open_id' #, :store => OpenID::Store::Filesystem.new('./tmp')
|
||||
|
|
|
@ -10,6 +10,9 @@ en:
|
|||
other: "%{count} errors prevented %{resource} from being saved:"
|
||||
|
||||
devise:
|
||||
shared:
|
||||
providers:
|
||||
sign_up_with: 'or sign in with'
|
||||
failure:
|
||||
already_authenticated: 'You are already signed in.'
|
||||
unauthenticated: 'You need to sign in or sign up before continuing.'
|
||||
|
|
|
@ -12,6 +12,9 @@ ru:
|
|||
other: "%{resource}: сохранение не удалось из-за %{count} ошибки"
|
||||
|
||||
devise:
|
||||
shared:
|
||||
providers:
|
||||
sign_up_with: 'или войти с помощью'
|
||||
failure:
|
||||
already_authenticated: 'Вы уже вошли в систему.'
|
||||
unauthenticated: "Вам необходимо войти в систему или зарегистрироваться."
|
||||
|
|
|
@ -21,11 +21,13 @@ Rails.application.routes.draw do
|
|||
end
|
||||
|
||||
devise_scope :user do
|
||||
get 'users/sign_up' => 'users/registrations#new', as: :new_user_registration
|
||||
post 'users' => 'users/registrations#create', as: :user_registration
|
||||
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'
|
||||
get 'users/sign_up' => 'users/registrations#new', as: :new_user_registration
|
||||
post 'users' => 'users/registrations#create', as: :user_registration
|
||||
end
|
||||
|
||||
devise_for :users, controllers: {
|
||||
omniauth_callbacks: 'users/omniauth_callbacks',
|
||||
confirmations: 'users/confirmations'
|
||||
}, skip: [:registrations]
|
||||
|
||||
|
|
Loading…
Reference in New Issue