Skip to content

Commit

Permalink
omniauth controller & google auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles-Butler committed Oct 15, 2020
1 parent 3dcc698 commit 14c1c7f
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 5 deletions.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/omniauth.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the omniauth controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
20 changes: 20 additions & 0 deletions app/controllers/omniauth_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class OmniauthController < ApplicationController

def google_oauth2
@user = User.create_from_google_data(request.env['omniauth.auth'])
if @user.persisted?
sign_in_and_redirect @user
set_flash_message(:notice, :success, kind: 'Google') if is_navigation_format?
else
flash[:error] = 'There was a problem signing you in through Google, Please register or try signing in later.'
redirect_to new_user_registration_url
end
end

def failure
flash[:error] = 'There was a problem signing you in through Google, Please register or try signing in later.'
redirect_to new_user_registration_url
end
end

end
2 changes: 2 additions & 0 deletions app/helpers/omniauth_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module OmniauthHelper
end
12 changes: 10 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
class User < ApplicationRecord
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]

has_many :ratings
has_many :rated_games, through: :ratings, source: :game

#but also user has created many games to the list
has_many :games

def self.create_from_provider_data(provider_data)
where(provider: provider_data.provider, uid: provider_data.uid).first_or_create do |user|
user.email = provider_data.info.email
user.password =Devise.friendly_token[0, 20]
end
end
end
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
devise_for :users
devise_for :users, controllers: {omniauth_callbacks: 'omniauth'}
root 'home#index'

resources :users
Expand All @@ -9,7 +9,6 @@
resources :ratings
resources :platforms

get '/auth/:provider/callback' => 'sessions#omniauth'

# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
6 changes: 6 additions & 0 deletions db/migrate/20201015105146_update_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class UpdateUsers < ActiveRecord::Migration[6.0]
def change
add_column(:users, :provider, :string, limit: 50, null: false, default: '')
add_column(:users, :uid, :string, limit: 500, null: false, default: '')
end
end
11 changes: 11 additions & 0 deletions db/migrate/20201015105418_create_sessions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateSessions < ActiveRecord::Migration[6.0]
def change
create_table :sessions do |t|
t.string :session_id, null: false
t.text :data
t.timestamps
end
add_index :sessions, :session_id, unique: true
add_index :sessions, :updated_at
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_10_11_022940) do
ActiveRecord::Schema.define(version: 2020_10_15_105418) do

create_table "games", force: :cascade do |t|
t.string "title"
Expand Down Expand Up @@ -41,6 +41,15 @@
t.index ["user_id"], name: "index_ratings_on_user_id"
end

create_table "sessions", force: :cascade do |t|
t.string "session_id", null: false
t.text "data"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["session_id"], name: "index_sessions_on_session_id", unique: true
t.index ["updated_at"], name: "index_sessions_on_updated_at"
end

create_table "users", force: :cascade do |t|
t.string "username"
t.datetime "created_at", precision: 6, null: false
Expand All @@ -50,6 +59,8 @@
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.string "provider", limit: 50, default: "", null: false
t.string "uid", limit: 500, default: "", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/omniauth_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class OmniauthControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit 14c1c7f

Please sign in to comment.