Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
ven-job
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Trịnh Hoàng Phúc
ven-job
Commits
930943ec
Commit
930943ec
authored
May 14, 2020
by
Trịnh Hoàng Phúc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Facebook authenticate
parent
4439bf12
Pipeline
#618
failed with stages
in 0 seconds
Changes
7
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
47 additions
and
4 deletions
+47
-4
app/controllers/users/omniauth_callbacks_controller.rb
+15
-0
app/models/user.rb
+21
-1
app/views/layouts/application.html.erb
+2
-1
config/initializers/devise.rb
+1
-1
config/routes.rb
+2
-1
db/migrate/20200410070154_add_columns_to_users.rb
+3
-0
db/schema.rb
+3
-0
No files found.
app/controllers/users/omniauth_callbacks_controller.rb
View file @
930943ec
...
...
@@ -27,4 +27,19 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
# def after_omniauth_failure_path_for(scope)
# super(scope)
# 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
app/models/user.rb
View file @
930943ec
...
...
@@ -2,7 +2,7 @@ class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise
:database_authenticatable
,
:registerable
,
:recoverable
,
:rememberable
,
:validatable
,
:confirmable
:recoverable
,
:rememberable
,
:validatable
,
:confirmable
,
:omniauthable
,
omniauth_providers:
%i[facebook]
has_many
:applies
has_many
:jobs
,
through: :applies
...
...
@@ -10,4 +10,24 @@ class User < ApplicationRecord
has_many
:jobs
,
through: :favorites
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
app/views/layouts/application.html.erb
View file @
930943ec
...
...
@@ -35,7 +35,7 @@
<button
type=
"submit"
id=
"searchsubmit"
class=
"searchform__submit searchform--header__submit"
><span
class=
"screen-reader-text"
>
Search
</button>
</form>
<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
%>
<li
class=
"menu-item menu-item-type-post_type menu-item-object-page menu-item-99991208"
>
<a
href=
"/jobs"
>
Find A Job
</a>
...
...
@@ -77,6 +77,7 @@
</li>
<%
end
%>
<%
end
%>
<%=
link_to
"Sign in with Facebook"
,
user_facebook_omniauth_authorize_path
%>
</ul>
</div>
</nav>
...
...
config/initializers/devise.rb
View file @
930943ec
...
...
@@ -260,7 +260,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
:facebook
,
"
586868888590440"
,
"6cd80c6f7926e1eb729db4a3bf2f9413
"
,
token_params:
{
parse: :json
}
config
.
omniauth
:facebook
,
"
174972447159044"
,
"24253120f824a205681970a5ebb707ab
"
,
token_params:
{
parse: :json
}
# ==> Warden configuration
# 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.
...
...
config/routes.rb
View file @
930943ec
...
...
@@ -4,7 +4,8 @@ Rails.application.routes.draw do
}
devise_for
:users
,
controllers:
{
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
root
to:
'home#index'
...
...
db/migrate/20200410070154_add_columns_to_users.rb
View file @
930943ec
class
AddColumnsToUsers
<
ActiveRecord
::
Migration
[
6.0
]
def
change
add_column
:users
,
:full_name
,
:string
add_column
:users
,
:image
,
:string
add_column
:users
,
:cv
,
:string
add_column
:users
,
:provider
,
:string
add_column
:users
,
:uid
,
:string
end
end
db/schema.rb
View file @
930943ec
...
...
@@ -105,7 +105,10 @@ ActiveRecord::Schema.define(version: 2020_04_23_044651) do
t
.
datetime
"created_at"
,
precision:
6
,
null:
false
t
.
datetime
"updated_at"
,
precision:
6
,
null:
false
t
.
string
"full_name"
t
.
string
"image"
t
.
string
"cv"
t
.
string
"provider"
t
.
string
"uid"
t
.
index
[
"confirmation_token"
],
name:
"index_users_on_confirmation_token"
,
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment