Skip to content

Commit

Permalink
auth works
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Feb 27, 2024
1 parent 356ecc6 commit d354bef
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ AllCops:


Metrics/AbcSize:
Max: 17
Max: 30
Severity: error

Metrics/ClassLength:
Expand Down
6 changes: 0 additions & 6 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@

class ApplicationController < ActionController::Base
before_action :authenticate_person!
skip_before_action :verify_authenticity_token, only: :create


def authenticate_person!
super
end
end
14 changes: 3 additions & 11 deletions app/controllers/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def openid_connect
require 'pry'; binding.pry # rubocop:disable Style/Semicolon,Lint/Debugger
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = Person.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
@user.save
sign_in_and_redirect @user, event: :authentication # this will throw if @user is not activated
set_flash_message(:notice, :success, kind: 'Keycloak') if is_navigational_format?
end

def failure
require 'pry'; binding.pry # rubocop:disable Style/Semicolon,Lint/Debugger
redirect_to root_path
end
end
18 changes: 12 additions & 6 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,24 @@ def picture_size

class << self
def from_omniauth(auth)
require 'pry'; binding.pry # rubocop:disable Style/Semicolon,Lint/Debugger
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.name = auth.info.name # assuming the user model has a name
where(email: auth.info.email).first_or_create do |person|
person.email = auth.info.email
person.name = auth.info.name
person.birthdate = DateTime.new(2020, 1, 1)
person.nationality = 'CH'
person.location = 'Schweiz'
person.title = 'Software Engineer'
person.marital_status = :single
person.company = Company.first
end
end

def new_with_session(params, session)
require 'pry'; binding.pry # rubocop:disable Style/Semicolon,Lint/Debugger
super.tap do |user|
if (data = session['devise.facebook_data'] && session['devise.facebook_data']['extra']['raw_info']) && user.email.blank?
data = session['devise.facebook_data']
session_info = session['devise.facebook_data']['extra']['raw_info']
if data && session_info && user.email.blank?
user.email = data['email']
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
%ul.navbar.text-gray
%li.d-flex.align-items-center.cursor-pointer.ps-2.pe-2.border-start.border-end.h-100
%pzsh-icon.scale-icon-06(name="user")
%text= "Curtis Jackson"
%text= current_person&.name
%li.d-flex.align-items-center.cursor-pointer.ps-2.pe-2
%a.d-flex.align-items-center{:href => "https://github.com/puzzle/skills/issues"}
%pzsh-icon.scale-icon-08.text-gray(name="question-circle")
%li.d-flex.align-items-center.cursor-pointer.border-start.border-end.h-100.ps-2.pe-2
%a= "Logout"
=link_to "Logout", destroy_person_session_path, data: { "turbo-method": :delete }
%div
%pzsh-topbar.p-0
%div.d-flex.justify-content-center
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
identifier: "pitc_skills_rails_backend",
secret: "r609dv7dl50164n4rlga121ott",
host: "sso-test.puzzle.ch",
redirect_uri: "http://localhost:3000/people/1",
redirect_uri: "http://localhost:3000/people/auth/openid_connect/callback",
},
}

Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

devise_scope :person do
get 'sign_in', :to => 'devise/sessions#new', :as => :new_person_session
get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_person_session
post 'sign_in', to: 'devise/sessions#create', as: :person_session
delete 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_person_session
end

resources :people
Expand Down
50 changes: 0 additions & 50 deletions db/migrate/20240226110212_add_devise_to_people.rb

This file was deleted.

0 comments on commit d354bef

Please sign in to comment.