Commit fafd0b11 by Tran Hoang Viet

VietTH: Implement login social facebook, twitter, google plus

parent 857001a1
...@@ -67,6 +67,11 @@ gem 'settingslogic', '~> 2.0.9' ...@@ -67,6 +67,11 @@ gem 'settingslogic', '~> 2.0.9'
gem 'draper', '~> 2.1.0' gem 'draper', '~> 2.1.0'
gem 'devise', '~> 3.5.1' gem 'devise', '~> 3.5.1'
gem 'omniauth'
gem 'omniauth-twitter'
gem "omniauth-google-oauth2"
gem 'omniauth-facebook'
gem 'rolify', '~> 4.0.0' gem 'rolify', '~> 4.0.0'
gem 'pundit', '~> 1.0.1' gem 'pundit', '~> 1.0.1'
# gem 'cancan', '~> 1.6.10' # gem 'cancan', '~> 1.6.10'
......
...@@ -112,6 +112,8 @@ GEM ...@@ -112,6 +112,8 @@ GEM
railties (>= 3.0.0) railties (>= 3.0.0)
faker (1.4.3) faker (1.4.3)
i18n (~> 0.5) i18n (~> 0.5)
faraday (0.9.1)
multipart-post (>= 1.2, < 3)
figaro (1.1.1) figaro (1.1.1)
thor (~> 0.14) thor (~> 0.14)
fission (0.5.0) fission (0.5.0)
...@@ -225,6 +227,7 @@ GEM ...@@ -225,6 +227,7 @@ GEM
haml (>= 4.0.6, < 5.0) haml (>= 4.0.6, < 5.0)
html2haml (>= 1.0.1) html2haml (>= 1.0.1)
railties (>= 4.0.1) railties (>= 4.0.1)
hashie (3.4.2)
hitimes (1.2.2) hitimes (1.2.2)
html2haml (2.0.0) html2haml (2.0.0)
erubis (~> 2.7.0) erubis (~> 2.7.0)
...@@ -244,6 +247,7 @@ GEM ...@@ -244,6 +247,7 @@ GEM
railties (>= 4.2.0) railties (>= 4.2.0)
thor (>= 0.14, < 2.0) thor (>= 0.14, < 2.0)
json (1.8.3) json (1.8.3)
jwt (1.5.1)
kaminari (0.16.3) kaminari (0.16.3)
actionpack (>= 3.0.0) actionpack (>= 3.0.0)
activesupport (>= 3.0.0) activesupport (>= 3.0.0)
...@@ -266,12 +270,37 @@ GEM ...@@ -266,12 +270,37 @@ GEM
minitest (5.7.0) minitest (5.7.0)
multi_json (1.11.2) multi_json (1.11.2)
multi_xml (0.5.5) multi_xml (0.5.5)
multipart-post (2.0.0)
mysql2 (0.3.18) mysql2 (0.3.18)
net-scp (1.2.1) net-scp (1.2.1)
net-ssh (>= 2.6.5) net-ssh (>= 2.6.5)
net-ssh (2.9.2) net-ssh (2.9.2)
nokogiri (1.6.6.2) nokogiri (1.6.6.2)
mini_portile (~> 0.6.0) mini_portile (~> 0.6.0)
oauth (0.4.7)
oauth2 (1.0.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (~> 1.2)
omniauth (1.2.2)
hashie (>= 1.2, < 4)
rack (~> 1.0)
omniauth-facebook (2.0.1)
omniauth-oauth2 (~> 1.2)
omniauth-google-oauth2 (0.2.6)
omniauth (> 1.0)
omniauth-oauth2 (~> 1.1)
omniauth-oauth (1.1.0)
oauth
omniauth (~> 1.0)
omniauth-oauth2 (1.3.1)
oauth2 (~> 1.0)
omniauth (~> 1.2)
omniauth-twitter (1.2.0)
json (~> 1.3)
omniauth-oauth (~> 1.1)
orm_adapter (0.5.0) orm_adapter (0.5.0)
parser (2.2.2.6) parser (2.2.2.6)
ast (>= 1.1, < 3.0) ast (>= 1.1, < 3.0)
...@@ -458,6 +487,10 @@ DEPENDENCIES ...@@ -458,6 +487,10 @@ DEPENDENCIES
meta-tags (~> 2.0.0) meta-tags (~> 2.0.0)
mini_magick (~> 3.8.1) mini_magick (~> 3.8.1)
mysql2 mysql2
omniauth
omniauth-facebook
omniauth-google-oauth2
omniauth-twitter
pry-byebug pry-byebug
pry-rails (~> 0.3.4) pry-rails (~> 0.3.4)
pundit (~> 1.0.1) pundit (~> 1.0.1)
......
class CallbacksController < Devise::OmniauthCallbacksController
def self.provides_callback_for(provider)
class_eval %Q{
def #{provider}
@user = User.from_omniauth(env["omniauth.auth"], current_user)
if @user.persisted?
sign_in_and_redirect @user, event: :authentication and return
set_flash_message(:notice, :success, kind: #{provider}.capitalize) if is_navigational_format?
else
session["devise.#{provider}_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
}
end
[:twitter, :facebook, :google_oauth2].each do |provider|
provides_callback_for provider
end
end
\ No newline at end of file
...@@ -9,9 +9,28 @@ class User < ActiveRecord::Base ...@@ -9,9 +9,28 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable # :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable :recoverable, :rememberable, :trackable, :validatable,
:omniauthable
def cart def cart
Cartman::Cart.new(self.id) Cartman::Cart.new(self.id)
end end
def self.from_omniauth(auth, current_user)
email = auth.info.email || "change@#{auth.provider}-#{auth.uid}.com"
user = current_user || User.find_by(email: email)
if user.blank?
user = User.new
user.password = Devise.friendly_token[0,10]
# user.name = auth.info.name
user.email = auth.info.email || "change@#{auth.provider}-#{auth.uid}.com"
user.provider = auth.provider
user.uid = auth.uid
# user.skip_confirmation!
user.save!
end
user
end
end end
= form_for resource, as: resource_name, url: session_path(resource_name), html: {class: 'form-horizontal'} do |f| = form_for resource, as: resource_name, url: session_path(resource_name), html: {class: 'form-horizontal'} do |f|
.form-group .form-group
.col-sm-offset-2.col-sm-2
= link_to user_omniauth_authorize_path(:facebook), class: 'btn btn-primary' do
%i.fa.fa-facebook-square
Facebook
.col-sm-2
= link_to user_omniauth_authorize_path(:twitter), class: 'btn btn-info' do
%i.fa.fa-twitter-square
Twitter
.col-sm-2
= link_to user_omniauth_authorize_path(:google_oauth2), class: 'btn btn-danger' do
%i.fa.fa-google-plus-square
Google
.form-group
= f.label :email, class: 'col-sm-2 control-label' = f.label :email, class: 'col-sm-2 control-label'
.col-sm-4 .col-sm-4
= f.email_field :email, autofocus: true, class: 'form-control' = f.email_field :email, autofocus: true, class: 'form-control'
...@@ -11,5 +25,5 @@ ...@@ -11,5 +25,5 @@
= f.password_field :password, autocomplete: "off", class: 'form-control' = f.password_field :password, autocomplete: "off", class: 'form-control'
.form-group .form-group
.col-sm-offset-2.col-sm-10 .col-sm-offset-2.col-sm-1
= f.submit "Log in", class: 'btn btn-default' = f.submit "Log in", class: 'btn btn-default'
...@@ -11,11 +11,9 @@ ...@@ -11,11 +11,9 @@
= render '/layouts/header' = render '/layouts/header'
#wrapper.container-fluid #wrapper.container-fluid
= render '/layouts/message'
.row .row
.col-md-6.search-wrapper .col-md-12.search-wrapper
= render 'layouts/search' = render '/layouts/message'
#content.row #content.row
.container .container
......
...@@ -8,4 +8,14 @@ S3_SECRET: WzHwCJowOJoZGQ9DbHWvzdVhYtpOrDpuynEiK4YL ...@@ -8,4 +8,14 @@ S3_SECRET: WzHwCJowOJoZGQ9DbHWvzdVhYtpOrDpuynEiK4YL
FB_APP_ID: 1629224294024346 FB_APP_ID: 1629224294024346
SOLR_URL: http://localhost:8080/solr/venshop SOLR_URL: http://localhost:8080/solr/venshop
\ No newline at end of file
# login social
FB_APP_ID: 1629224294024346 #832201700220898
FB_APP_SECRET: f6e41a232f08cee3f551b6144c6bebd1 #f4acefa45dbe2ebe243993fd9973ffd4
TW_APP_ID: 6cTFNJHzzaS3meGAKlwVL4Y4C #0WmE2Iz8zMNu4URCJI1eXFQcp
TW_APP_SECRET: GRHy4YRqfkts905Yq7lZWPpOGu8Sp4WqAMEpkoh9XtFUnwanN0 #HeBya1RWdXQ8shMJr3Qw89OwN248fQ7bRIg14lo5kndDc7LNXA
GG_APP_ID: 292647706308-v84pk9nt4di2324upih6s4hr8oooh1qr.apps.googleusercontent.com
GG_APP_SECRET: Jju3TCCx2-gIgRw9iahnqeHc
\ No newline at end of file
...@@ -12,7 +12,7 @@ Devise.setup do |config| ...@@ -12,7 +12,7 @@ Devise.setup do |config|
# Configure the e-mail address which will be shown in Devise::Mailer, # Configure the e-mail address which will be shown in Devise::Mailer,
# note that it will be overwritten if you use your own mailer class # note that it will be overwritten if you use your own mailer class
# with default "from" parameter. # with default "from" parameter.
config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' config.mailer_sender = 'venshop@example.com'
# Configure the class responsible to send e-mails. # Configure the class responsible to send e-mails.
# config.mailer = 'Devise::Mailer' # config.mailer = 'Devise::Mailer'
...@@ -262,4 +262,7 @@ Devise.setup do |config| ...@@ -262,4 +262,7 @@ Devise.setup do |config|
# When using OmniAuth, Devise cannot automatically set OmniAuth path, # When using OmniAuth, Devise cannot automatically set OmniAuth path,
# so you need to do it manually. For the users scope, it would be: # so you need to do it manually. For the users scope, it would be:
# config.omniauth_path_prefix = '/my_engine/users/auth' # config.omniauth_path_prefix = '/my_engine/users/auth'
config.omniauth :facebook, ENV['FB_APP_ID'], ENV['FB_APP_SECRET'], scope: 'email', info_fields: 'email'
config.omniauth :twitter, ENV['TW_APP_ID'], ENV['TW_APP_SECRET']
config.omniauth :google_oauth2, ENV['GG_APP_ID'], ENV['GG_APP_SECRET']
end end
Rails.application.routes.draw do Rails.application.routes.draw do
devise_for :users devise_for :users, controllers: { omniauth_callbacks: "callbacks" }
root 'home#index' root 'home#index'
resources :users do resources :users do
......
class AddColumnsToUsers < ActiveRecord::Migration
def change
add_column :users, :provider, :string
add_column :users, :uid, :string
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150716032049) do ActiveRecord::Schema.define(version: 20150722040425) do
create_table "categories", force: :cascade do |t| create_table "categories", force: :cascade do |t|
t.datetime "created_at", null: false t.datetime "created_at", null: false
...@@ -93,6 +93,8 @@ ActiveRecord::Schema.define(version: 20150716032049) do ...@@ -93,6 +93,8 @@ ActiveRecord::Schema.define(version: 20150716032049) do
t.string "last_sign_in_ip", limit: 255 t.string "last_sign_in_ip", limit: 255
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "provider", limit: 255
t.string "uid", limit: 255
end end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment