Skip to content

Commit

Permalink
Merge pull request #48 from nasum/iss-36
Browse files Browse the repository at this point in the history
FIX #36 登録できるメールアドレスのドメインを限定
  • Loading branch information
Masaya Nasu committed Jan 10, 2016
2 parents 6f3b031 + 8d2b875 commit 4224a64
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
15 changes: 10 additions & 5 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ class SessionsController < ApplicationController
def callback
auth = request.env['omniauth.auth']

user = User.find_by(google_uid: auth[:uid])
unless user
user = User.create(google_uid: auth[:uid], name: auth[:info][:name], email: auth[:info][:email], image_url: auth[:info][:image])
if auth.info['email'].split('@')[1] == ENV['RESTRICT_DOMAIN']
user = User.find_by(google_uid: auth[:uid])
user = User.form_omniauth(auth) unless user
session[:user_id] = user.id
redirect_to root_path
else
flash[:error] = "
ドメインが#{ENV['RESTRICT_DOMAIN']}ではありません。正しいドメインでサインイン・ログインしてください
"
redirect_to root_path
end
session[:user_id] = user.id
redirect_to root_path
end

def destroy
Expand Down
9 changes: 9 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@

class User < ActiveRecord::Base
has_many :poem, dependent: :destroy

def self.form_omniauth(auth)
User.create(
google_uid: auth[:uid],
name: auth[:info][:name],
email: auth[:info][:email],
image_url: auth[:info][:image]
)
end
end
3 changes: 2 additions & 1 deletion config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"]
options = { hd: ENV["RESTRICT_DOMAIN"] }
provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], options
end
OmniAuth.config.on_failure = Proc.new { |env|
OmniAuth::FailureEndpoint.new(env).redirect_to_failure
Expand Down

0 comments on commit 4224a64

Please sign in to comment.