Commit f0b50904 by Mykhailo Makohin

finish registration via linkedin

parent f0fb8ffd
...@@ -18,6 +18,7 @@ gem 'devise' ...@@ -18,6 +18,7 @@ gem 'devise'
gem 'omniauth' gem 'omniauth'
gem 'omniauth-facebook' gem 'omniauth-facebook'
gem 'omniauth-google-oauth2' gem 'omniauth-google-oauth2'
gem 'omniauth-linkedin-oauth2'
group :development, :test do group :development, :test do
gem 'byebug', platform: :mri gem 'byebug', platform: :mri
......
...@@ -109,6 +109,8 @@ GEM ...@@ -109,6 +109,8 @@ GEM
jwt (>= 2.0) jwt (>= 2.0)
omniauth (>= 1.1.1) omniauth (>= 1.1.1)
omniauth-oauth2 (>= 1.6) omniauth-oauth2 (>= 1.6)
omniauth-linkedin-oauth2 (1.0.0)
omniauth-oauth2
omniauth-oauth2 (1.6.0) omniauth-oauth2 (1.6.0)
oauth2 (~> 1.1) oauth2 (~> 1.1)
omniauth (~> 1.9) omniauth (~> 1.9)
...@@ -205,6 +207,7 @@ DEPENDENCIES ...@@ -205,6 +207,7 @@ DEPENDENCIES
omniauth omniauth
omniauth-facebook omniauth-facebook
omniauth-google-oauth2 omniauth-google-oauth2
omniauth-linkedin-oauth2
puma (~> 3.0) puma (~> 3.0)
rails (~> 5.0.7, >= 5.0.7.2) rails (~> 5.0.7, >= 5.0.7.2)
sass-rails (~> 5.0) sass-rails (~> 5.0)
......
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
skip_before_action :verify_authenticity_token
def facebook def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb) sign_in_with "Facebook"
@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 end
def failure def failure
redirect_to root_path redirect_to root_path
end end
def google_oauth2 def linkedin
@user = User.from_omniauth(request.env['omniauth.auth']) sign_in_with "LinkedIn"
end
if @user.persisted? def google_oauth2
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google' sign_in_with "Google"
sign_in_and_redirect @user, event: :authentication
else
session['devise.google_data'] = request.env['omniauth.auth'].except(:extra)
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end end
def sign_in_with(provider_name)
@user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect @user, :event => :authentication
set_flash_message(:notice, :success, :kind => provider_name) if is_navigational_format?
end end
end end
\ No newline at end of file
...@@ -2,7 +2,7 @@ class User < ApplicationRecord ...@@ -2,7 +2,7 @@ class User < ApplicationRecord
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :omniauthable, :recoverable, :rememberable, :omniauthable,
omniauth_providers: [:facebook, :google_oauth2] omniauth_providers: [:facebook, :google_oauth2, :linkedin]
before_create do before_create do
self.email = 'example123@gmail.com' if email.nil? self.email = 'example123@gmail.com' if email.nil?
......
<% unless current_user %> <% unless current_user %>
<%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path %> <%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path %>
<%= link_to "Sign in with Google", user_google_oauth2_omniauth_authorize_path %> <%= link_to "Sign in with Google", user_google_oauth2_omniauth_authorize_path %>
<%= link_to "Sign in with LinkedIn", user_linkedin_omniauth_authorize_path %>
<% else %> <%= current_user[:name] %> <% else %> <%= current_user[:name] %>
<%= link_to "Logout", destroy_user_session_path, method: :delete %> <%= link_to "Logout", destroy_user_session_path, method: :delete %>
<% end %> <% end %>
...@@ -30,4 +30,6 @@ Devise.setup do |config| ...@@ -30,4 +30,6 @@ Devise.setup do |config|
config.omniauth :google_oauth2, "444952886435-8s76oeuc53otc8q84jork9mq4php7e7t.apps.googleusercontent.com", config.omniauth :google_oauth2, "444952886435-8s76oeuc53otc8q84jork9mq4php7e7t.apps.googleusercontent.com",
"vfJkP71fOkfDKVYa3RgXR3lW", {} "vfJkP71fOkfDKVYa3RgXR3lW", {}
config.omniauth :linkedin, "860rz2x1z3udrb", "peK2u8waxWK5h3Bq",{}
end end
...@@ -2,6 +2,8 @@ development: ...@@ -2,6 +2,8 @@ development:
secret_key_base: b1b8b2c8f26c31fb891d8f8eaf37f5bc26ea31ac8913a4acfe7e9013853eeb3df5e681187e8ed35df9e6526d9528973791f904727fe56d250d903ff21083ece6 secret_key_base: b1b8b2c8f26c31fb891d8f8eaf37f5bc26ea31ac8913a4acfe7e9013853eeb3df5e681187e8ed35df9e6526d9528973791f904727fe56d250d903ff21083ece6
GOOGLE_CLIENT_ID: 444952886435-8s76oeuc53otc8q84jork9mq4php7e7t.apps.googleusercontent.com GOOGLE_CLIENT_ID: 444952886435-8s76oeuc53otc8q84jork9mq4php7e7t.apps.googleusercontent.com
GOOGLE_SECRET_KEY: vfJkP71fOkfDKVYa3RgXR3lW GOOGLE_SECRET_KEY: vfJkP71fOkfDKVYa3RgXR3lW
LINKEDIN_CLIENT_ID: 860rz2x1z3udrb
LINKEDIN_SECRET_KEY: peK2u8waxWK5h3Bq
test: test:
secret_key_base: dd24cc39a6555956e85cb7d60612c685b91249ed8760a957b155ced0177eb172674137f73ba33d8b886f14089469703830d76ddbd42a996897d8b77aac43b33a secret_key_base: dd24cc39a6555956e85cb7d60612c685b91249ed8760a957b155ced0177eb172674137f73ba33d8b886f14089469703830d76ddbd42a996897d8b77aac43b33a
......
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20190829110429) do ActiveRecord::Schema.define(version: 20190829110429) do
create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
......
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