Skip to content

Commit

Permalink
devise omniauth
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedCuerci committed Nov 30, 2024
1 parent 393709b commit 3bbf5ea
Show file tree
Hide file tree
Showing 18 changed files with 414 additions and 21 deletions.
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ group :test do
end

gem "devise", "~> 4.9"
gem "omniauth"
gem "omniauth-google-oauth2"
gem 'omniauth-rails_csrf_protection'

Check failure on line 62 in Gemfile

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

# pro bootstrap
gem 'sassc-rails'

Check failure on line 65 in Gemfile

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
67 changes: 67 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,25 @@ GEM
warden (~> 1.2.3)
drb (2.2.1)
erubi (1.13.0)
faraday (2.12.1)
faraday-net_http (>= 2.0, < 3.5)
json
logger
faraday-net_http (3.4.0)
net-http (>= 0.5.0)
ffi (1.17.0-aarch64-linux-gnu)
ffi (1.17.0-aarch64-linux-musl)
ffi (1.17.0-arm-linux-gnu)
ffi (1.17.0-arm-linux-musl)
ffi (1.17.0-arm64-darwin)
ffi (1.17.0-x86-linux-gnu)
ffi (1.17.0-x86-linux-musl)
ffi (1.17.0-x86_64-darwin)
ffi (1.17.0-x86_64-linux-gnu)
ffi (1.17.0-x86_64-linux-musl)
globalid (1.2.1)
activesupport (>= 6.1)
hashie (5.0.0)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
Expand All @@ -123,6 +140,8 @@ GEM
jsbundling-rails (1.3.1)
railties (>= 6.0.0)
json (2.8.2)
jwt (2.9.3)
base64
language_server-protocol (3.17.0.3)
logger (1.6.1)
loofah (2.23.1)
Expand All @@ -138,6 +157,10 @@ GEM
mini_mime (1.1.5)
minitest (5.25.2)
msgpack (1.7.5)
multi_xml (0.7.1)
bigdecimal (~> 3.1)
net-http (0.5.0)
uri
net-imap (0.5.1)
date
net-protocol
Expand All @@ -160,6 +183,28 @@ GEM
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
racc (~> 1.4)
oauth2 (2.0.9)
faraday (>= 0.17.3, < 3.0)
jwt (>= 1.0, < 3.0)
multi_xml (~> 0.5)
rack (>= 1.2, < 4)
snaky_hash (~> 2.0)
version_gem (~> 1.1)
omniauth (2.1.2)
hashie (>= 3.4.6)
rack (>= 2.2.3)
rack-protection
omniauth-google-oauth2 (1.2.0)
jwt (>= 2.9)
oauth2 (~> 2.0)
omniauth (~> 2.0)
omniauth-oauth2 (~> 1.8)
omniauth-oauth2 (1.8.0)
oauth2 (>= 1.4, < 3)
omniauth (~> 2.0)
omniauth-rails_csrf_protection (1.0.2)
actionpack (>= 4.2)
omniauth (~> 2.0)
orm_adapter (0.5.0)
parallel (1.26.3)
parser (3.3.6.0)
Expand All @@ -172,6 +217,10 @@ GEM
nio4r (~> 2.0)
racc (1.8.1)
rack (3.1.8)
rack-protection (4.1.1)
base64 (>= 0.1.0)
logger (>= 1.6.0)
rack (>= 3.0.0, < 4)
rack-session (2.0.0)
rack (>= 3.0.0)
rack-test (2.1.0)
Expand Down Expand Up @@ -248,13 +297,24 @@ GEM
rubocop-rails
ruby-progressbar (1.13.0)
rubyzip (2.3.2)
sassc (2.4.0)
ffi (~> 1.9)
sassc-rails (2.1.2)
railties (>= 4.0.0)
sassc (>= 2.0)
sprockets (> 3.0)
sprockets-rails
tilt
securerandom (0.3.2)
selenium-webdriver (4.27.0)
base64 (~> 0.2)
logger (~> 1.4)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
snaky_hash (2.0.1)
hashie
version_gem (~> 1.1, >= 1.1.1)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand All @@ -276,6 +336,7 @@ GEM
railties (>= 6.0.0)
stringio (3.1.2)
thor (1.3.2)
tilt (2.4.0)
timeout (0.4.2)
turbo-rails (2.0.11)
actionpack (>= 6.0.0)
Expand All @@ -285,7 +346,9 @@ GEM
unicode-display_width (3.1.2)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
uri (1.0.2)
useragent (0.16.10)
version_gem (1.1.4)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.2.1)
Expand Down Expand Up @@ -325,9 +388,13 @@ DEPENDENCIES
devise (~> 4.9)
jbuilder
jsbundling-rails
omniauth
omniauth-google-oauth2
omniauth-rails_csrf_protection
puma (>= 5.0)
rails (~> 7.2.1)
rubocop-rails-omakase
sassc-rails
selenium-webdriver
sprockets-rails
sqlite3 (>= 1.4)
Expand Down
17 changes: 2 additions & 15 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS (and SCSS, if configured) file within this directory, lib/assets/stylesheets, or any plugin's
* vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/
@import "bootstrap";

18 changes: 18 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Users::OmniauthCallbacksController < ApplicationController
def google_oauth2
@user = User.from_omniauth(request.env['omniauth.auth'])

Check failure on line 3 in app/controllers/users/omniauth_callbacks_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

if @user.persisted?
sign_in_and_redirect @user, event: :authentication
# set_flash_message(:notice, :sucess, kind: 'Google') if is_navigational_format?
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'

Check failure on line 8 in app/controllers/users/omniauth_callbacks_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Check failure on line 8 in app/controllers/users/omniauth_callbacks_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
else
session['devise.google_data'] = request.env['omniauth.auth'].except(:extra)

Check failure on line 10 in app/controllers/users/omniauth_callbacks_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Check failure on line 10 in app/controllers/users/omniauth_callbacks_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end
end

def failure
redirect_to root_path
end
end
2 changes: 2 additions & 0 deletions app/helpers/users/omniauth_callbacks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Users::OmniauthCallbacksHelper
end
1 change: 1 addition & 0 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Entry point for the build script in your package.json
import "@hotwired/turbo-rails"
import "./controllers"
import "./stylesheets/application.scss"; // Corrigido
import "bootstrap"

if ("serviceWorker" in navigator) {
Expand Down
1 change: 1 addition & 0 deletions app/javascript/stylesheets/application.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "bootstrap";
14 changes: 13 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,17 @@ class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
:recoverable, :rememberable, :validatable, :omniauthable, omniauth_providers: [:google_oauth2]

Check failure on line 5 in app/models/user.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 5 in app/models/user.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.


def self.from_omniauth(access_token)
data = access_token.info
user = User.where(email: data['email']).first

Check failure on line 10 in app/models/user.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

unless user
user = User.create(email: data['email'], password: Devise.friendly_token[0,20])
end

user
end
end
5 changes: 4 additions & 1 deletion app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
<h1>Home#index</h1>
<h1 class="container">Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>


<h1 class="container text-center">Título</h1>
4 changes: 4 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module" %>
<link rel="manifest" href="<%= asset_path('manifest.json') %>">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>

<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
NErbfJe7hEXz/NbzcUFUosSXjS7v7rmCxsDhPHtsHfpkQYT9/IcuuQ2SEUSM71tL2zKqej59TBfHaUvKHzWxUFQuSnAeRkefG921qxvt6zj2li3JyFxKUsXBhQ3Ijv/kM8y5QFP8aAXH1AKXh/N0jyH77TjuP4GVEyjMsP3ua7tGnj9Owk7FRW9m+wGu+QgtPXNnnf/TYpPVJ2hF13SB8Tn8qw+LNAx1xAjTnyhWxJbKHTYiAHS7rKvxfmpP/8ynZiskLn/0vg/mfJBxGlnnwLALK9SVcbFDXGmPVJqos6EJ18qVthbVsv0qoal5DaYBAYgKefRppxfabSXbQ53KRj8K0PuJHcHF4YnCCwg8g0yOWde8TqtBNR4CTQEifQYGrBjAtJZBkGjAw5QUnLMwykp2qx0J--86zCWKDrO9j9AHb9--+86tKLe0kS98bZNwLWPAwA==
bwbp8JDzwHisChD7f51bElZ/pHLkE3d2vzQRzw+l0TkdzojLfMWi0R9K1pavdUz1AcgVryywuM2ao5ByqXs6UaXjMto3hCY9/7MHvf63+WNFlFRLDAfVZrxEGio4RMuV1GpAAvnRjiZgKFVfjvxAQ8vLTPBiGfOfLQOTAxgqphAPMOpz7ew7Yj5qrmKX7Je9nuFnC72uOH7a6XNDKu37IKGVYzwUPCpjnuy1yBYYLgmzx7fnpgfkf+6P7chmLE/visouRmLUZt2GKfXfzIx9tDsMYKhQTuWRftWifEkt5Ch5WTWH5KO4ejgEjbWDevzvkCnU3dBaFBYzLUX+2B18LMCWZgD/ivf0Rxku84gpjwVezObmeznSfb3vD1zLKh67Citg7N8DwKh1/Oa5VwqokIx2VWZUI/0To938eXraglhW93cHpqEJ53AGfzNLCQEG3t0hKuqwBFQCeR6n61hfezwIY16uPEIjJXI1gBcW9zjq8gQ88+Rqsc72WIg6DOq/IVPPp8jTRtcsE10m6NWKQDi23XoiUyuQgJRgxVGgmf+gVbFtb1kJus8H+gFOpfy/u1qZZLoLFaPACo0KjnY2aqeE43SUyEfQxFL4fg==--jYe8P6AZXKKXizXI--FPBCYODzAier1S6tAJnBrg==
4 changes: 4 additions & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@
# 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 :google_oauth2, Rails.application.credentials.google[:client_id], Rails.application.credentials.google[:client_secret], {
scope: 'email,profile',
prompt: 'select_account'
}

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
Expand Down
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Rails.application.routes.draw do
devise_for :users
# devise_for :users, controllers: {
#     omniauth_callbacks: 'users/omniauth_callbacks'
#   }

devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }

root "home#index"
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20241130173157_add_omniauth_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddOmniauthToUsers < ActiveRecord::Migration[7.2]
def change
add_column :users, :provider, :string
add_column :users, :uid, :string
add_column :users, :avatar_url, :string
add_column :users, :fullname, :string
end
end
6 changes: 5 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"@hotwired/stimulus": "^3.2.2",
"@hotwired/turbo-rails": "^8.0.12",
"@popperjs/core": "^2.11.8",
"bootstrap": "^5.3.3"
"bootstrap": "^5.3.3",
"esbuild-plugin-sass": "^1.0.1"
},
"devDependencies": {
"esbuild": "^0.24.0"
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/users/omniauth_callbacks_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class Users::OmniauthCallbacksControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
Loading

0 comments on commit 3bbf5ea

Please sign in to comment.