-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from kaishuu0123/master
Release
- Loading branch information
Showing
36 changed files
with
667 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# refs: https://qiita.com/mnishiguchi/items/e15bbef61287f84b546e | ||
class OmniauthCallbacksController < Devise::OmniauthCallbacksController | ||
# いくつプロバイダーを利用しようが処理は共通しているので本メソッドをエイリアスとして流用。 | ||
def callback_for_all_providers | ||
unless request.env['omniauth.auth'].present? | ||
flash[:danger] = 'Authentication data was not provided' | ||
redirect_to root_url && return | ||
end | ||
provider = __callee__.to_s | ||
user = OAuthService::GetOAuthUser.call(request.env['omniauth.auth']) | ||
# ユーザーがデータベースに保存されており、且つemailを確認済みであれば、ユーザーをログインする。 | ||
if user.persisted? && user.email_verified? | ||
sign_in_and_redirect user, event: :authentication | ||
set_flash_message(:notice, :success, kind: provider.capitalize) if is_navigational_format? | ||
else | ||
# user.reset_confirmation! | ||
flash[:warning] = t('.need_info_before_signup', default: 'We need your email address before proceeding.') | ||
redirect_to finish_signup_path(user, provider: provider) | ||
end | ||
end | ||
|
||
alias twitter callback_for_all_providers | ||
alias github callback_for_all_providers | ||
alias google_oauth2 callback_for_all_providers | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# refs: https://qiita.com/mnishiguchi/items/e15bbef61287f84b546e | ||
class OmniauthFinishedController < ApplicationController | ||
skip_authorization_check only: [:finish_signup] | ||
before_action :authenticate_user!, except: :finish_signup | ||
|
||
def finish_signup | ||
@user = User.find(params[:id]) | ||
@provider = params[:provider] | ||
|
||
if (request.post? || request.patch?) && @user.update(user_params) | ||
# @user.send_confirmation_instructions unless @user.confirmed? | ||
sign_in(@user, bypass: true) | ||
redirect_to root_url, notice: t('devise.omniauth_callbacks.success', kind: @provider.capitalize) | ||
end | ||
end | ||
|
||
private | ||
|
||
# user_paramsにアクセスするため。 | ||
def user_params | ||
accessible = [ :username, :email ] | ||
accessible << [ :password, :password_confirmation ] unless params[:user][:password].blank? | ||
params.require(:user).permit(accessible) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# refs: https://qiita.com/mnishiguchi/items/e15bbef61287f84b546e | ||
class RegistrationsController < Devise::RegistrationsController | ||
|
||
protected | ||
|
||
# Override | ||
def update_resource(resource, params) | ||
resource.update_without_password(params) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# refs: https://qiita.com/mnishiguchi/items/e15bbef61287f84b546e | ||
module OAuthPolicy | ||
class Base | ||
attr_reader :provider, :uid, :name, :nickname, :email, :url, :image_url, | ||
:description, :other, :credentials, :raw_info | ||
end | ||
|
||
class Twitter < OAuthPolicy::Base | ||
def initialize(auth) | ||
@provider = auth['provider'] | ||
@uid = auth['uid'] | ||
@name = auth['info']['name'] | ||
@nickname = auth['info']['nickname'] | ||
@email = '' | ||
@url = auth['info']['urls']['Twitter'] | ||
@image_url = auth['info']['image'] | ||
@description = auth['info']['description'].try(:truncate, 255) | ||
@credentials = auth['credentials'].to_json | ||
@raw_info = auth['extra']['raw_info'].to_json | ||
freeze | ||
end | ||
end | ||
|
||
class Github < OAuthPolicy::Base | ||
def initialize(auth) | ||
@provider = auth['provider'] | ||
@uid = auth['uid'] | ||
@name = auth['info']['name'] | ||
@nickname = '' | ||
@email = '' | ||
@url = 'https://github.com/' | ||
@image_url = auth['info']['image'] | ||
@description = '' | ||
@credentials = auth['credentials'].to_json | ||
@raw_info = auth['extra']['raw_info'].to_json | ||
freeze | ||
end | ||
end | ||
|
||
class GoogleOauth2 < OAuthPolicy::Base | ||
def initialize(auth) | ||
@provider = auth['provider'] | ||
@uid = auth['uid'] | ||
@name = auth['info']['name'] | ||
@nickname = '' | ||
@email = '' | ||
@url = 'https://google.com/' | ||
@image_url = auth['info']['image'] | ||
@description = '' | ||
@credentials = auth['credentials'].to_json | ||
@raw_info = auth['extra']['raw_info'].to_json | ||
freeze | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# refs: https://qiita.com/mnishiguchi/items/e15bbef61287f84b546e | ||
# == Schema Information | ||
# | ||
# Table name: social_profiles | ||
# | ||
# id :integer not null, primary key | ||
# user_id :integer | ||
# provider :string | ||
# uid :string | ||
# name :string | ||
# nickname :string | ||
# email :string | ||
# url :string | ||
# image_url :string | ||
# description :string | ||
# others :text | ||
# credentials :text | ||
# raw_info :text | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class SocialProfile < ApplicationRecord | ||
belongs_to :user | ||
store :others | ||
|
||
validates_uniqueness_of :uid, scope: :provider | ||
|
||
def self.find_for_oauth(auth) | ||
profile = find_or_create_by(uid: auth.uid, provider: auth.provider) | ||
profile.save_oauth_data!(auth) | ||
profile | ||
end | ||
|
||
def save_oauth_data!(auth) | ||
return unless valid_oauth?(auth) | ||
|
||
provider = auth["provider"] | ||
policy = policy(provider, auth) | ||
|
||
self.update_attributes( uid: policy.uid, | ||
name: policy.name, | ||
nickname: policy.nickname, | ||
email: policy.email, | ||
url: policy.url, | ||
image_url: policy.image_url, | ||
description: policy.description, | ||
credentials: policy.credentials, | ||
raw_info: policy.raw_info ) | ||
end | ||
|
||
private | ||
|
||
def policy(provider, auth) | ||
class_name = "#{provider}".classify | ||
"OAuthPolicy::#{class_name}".constantize.new(auth) | ||
end | ||
|
||
def valid_oauth?(auth) | ||
(self.provider.to_s == auth['provider'].to_s) && (self.uid == auth['uid']) | ||
end | ||
end |
Oops, something went wrong.