diff --git a/Gemfile b/Gemfile index c52956b..2429ba2 100644 --- a/Gemfile +++ b/Gemfile @@ -57,3 +57,9 @@ group :test do end gem "devise", "~> 4.9" +gem "omniauth" +gem "omniauth-google-oauth2" +gem 'omniauth-rails_csrf_protection' + +# pro bootstrap +gem 'sassc-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 98e32b2..7b1e20b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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 @@ -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) @@ -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) @@ -248,6 +297,14 @@ 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) @@ -255,6 +312,9 @@ GEM 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) @@ -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) @@ -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) @@ -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) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 288b9ab..91110c1 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -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"; + diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb new file mode 100644 index 0000000..525af3c --- /dev/null +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -0,0 +1,18 @@ +class Users::OmniauthCallbacksController < ApplicationController + def google_oauth2 + @user = User.from_omniauth(request.env['omniauth.auth']) + + 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' + 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 failure + redirect_to root_path + end +end diff --git a/app/helpers/users/omniauth_callbacks_helper.rb b/app/helpers/users/omniauth_callbacks_helper.rb new file mode 100644 index 0000000..7c6e5e0 --- /dev/null +++ b/app/helpers/users/omniauth_callbacks_helper.rb @@ -0,0 +1,2 @@ +module Users::OmniauthCallbacksHelper +end diff --git a/app/javascript/application.js b/app/javascript/application.js index fe5afee..bdf2984 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -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) { diff --git a/app/javascript/stylesheets/application.scss b/app/javascript/stylesheets/application.scss new file mode 100644 index 0000000..cbd46a7 --- /dev/null +++ b/app/javascript/stylesheets/application.scss @@ -0,0 +1 @@ +@import "bootstrap"; diff --git a/app/models/user.rb b/app/models/user.rb index 4756799..4cc60cc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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] + + + def self.from_omniauth(access_token) + data = access_token.info + user = User.where(email: data['email']).first + + unless user + user = User.create(email: data['email'], password: Devise.friendly_token[0,20]) + end + + user + end end diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 2085730..41f7fc5 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -1,2 +1,5 @@ -
Find me in app/views/home/index.html.erb
+ + +<%= notice %>
<%= alert %>
<%= yield %> + + +