Commit 930943ec by Trịnh Hoàng Phúc

Facebook authenticate

parent 4439bf12
Pipeline #618 failed with stages
in 0 seconds
...@@ -27,4 +27,19 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController ...@@ -27,4 +27,19 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
# def after_omniauth_failure_path_for(scope) # def after_omniauth_failure_path_for(scope)
# super(scope) # super(scope)
# end # end
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
redirect_to root_path
end
end end
...@@ -2,7 +2,7 @@ class User < ApplicationRecord ...@@ -2,7 +2,7 @@ class User < ApplicationRecord
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable :recoverable, :rememberable, :validatable, :confirmable, :omniauthable, omniauth_providers: %i[facebook]
has_many :applies has_many :applies
has_many :jobs, through: :applies has_many :jobs, through: :applies
...@@ -10,4 +10,24 @@ class User < ApplicationRecord ...@@ -10,4 +10,24 @@ class User < ApplicationRecord
has_many :jobs, through: :favorites has_many :jobs, through: :favorites
mount_uploader :cv, CvUploader mount_uploader :cv, CvUploader
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0, 20]
user.full_name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
# user.skip_confirmation!
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
end end
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
<button type="submit" id="searchsubmit" class="searchform__submit searchform--header__submit"><span class="screen-reader-text">Search</button> <button type="submit" id="searchsubmit" class="searchform__submit searchform--header__submit"><span class="screen-reader-text">Search</button>
</form> </form>
<div class="nav-menu nav-menu--primary"> <div class="nav-menu nav-menu--primary">
<ul id="menu-main-menu" class="nav-menu nav-menu--primary"> <ul id="menu-main-menu" class="nav-menu nav-menu--primary">%>
<% if !current_admin %> <% if !current_admin %>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-99991208"> <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-99991208">
<a href="/jobs">Find A Job</a> <a href="/jobs">Find A Job</a>
...@@ -77,6 +77,7 @@ ...@@ -77,6 +77,7 @@
</li> </li>
<% end %> <% end %>
<% end %> <% end %>
<%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path %>
</ul> </ul>
</div> </div>
</nav> </nav>
......
...@@ -260,7 +260,7 @@ Devise.setup do |config| ...@@ -260,7 +260,7 @@ Devise.setup do |config|
# Add a new OmniAuth provider. Check the wiki for more information on setting # Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks. # up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' # config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
config.omniauth :facebook, "586868888590440", "6cd80c6f7926e1eb729db4a3bf2f9413", token_params: { parse: :json } config.omniauth :facebook, "174972447159044", "24253120f824a205681970a5ebb707ab", token_params: { parse: :json }
# ==> Warden configuration # ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or # If you want to use other strategies, that are not supported by Devise, or
# change the failure app, you can configure them inside the config.warden block. # change the failure app, you can configure them inside the config.warden block.
......
...@@ -4,7 +4,8 @@ Rails.application.routes.draw do ...@@ -4,7 +4,8 @@ Rails.application.routes.draw do
} }
devise_for :users, controllers: { devise_for :users, controllers: {
sessions: 'users/sessions', sessions: 'users/sessions',
confirmations: 'users/confirmations' confirmations: 'users/confirmations',
omniauth_callbacks: 'users/omniauth_callbacks'
} }
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root to: 'home#index' root to: 'home#index'
......
class AddColumnsToUsers < ActiveRecord::Migration[6.0] class AddColumnsToUsers < ActiveRecord::Migration[6.0]
def change def change
add_column :users, :full_name, :string add_column :users, :full_name, :string
add_column :users, :image, :string
add_column :users, :cv, :string add_column :users, :cv, :string
add_column :users, :provider, :string
add_column :users, :uid, :string
end end
end end
...@@ -105,7 +105,10 @@ ActiveRecord::Schema.define(version: 2020_04_23_044651) do ...@@ -105,7 +105,10 @@ ActiveRecord::Schema.define(version: 2020_04_23_044651) do
t.datetime "created_at", precision: 6, null: false t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false
t.string "full_name" t.string "full_name"
t.string "image"
t.string "cv" t.string "cv"
t.string "provider"
t.string "uid"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
......
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