summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Gemfile4
-rw-r--r--Gemfile.lock85
-rw-r--r--app/controllers/callbacks_controller.rb6
-rw-r--r--app/controllers/sessions_controller.rb23
-rw-r--r--app/models/user.rb14
-rw-r--r--config/initializers/devise.rb2
-rw-r--r--config/routes.rb3
-rw-r--r--db/migrate/20210308101347_add_columns_to_users.rb6
-rw-r--r--db/schema.rb32
9 files changed, 147 insertions, 28 deletions
diff --git a/Gemfile b/Gemfile
index 293bba7..c2c35ea 100644
--- a/Gemfile
+++ b/Gemfile
@@ -28,7 +28,9 @@ gem 'jbuilder', '~> 2.7'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false
-gem 'devise'
+gem 'devise', github: 'heartcombo/devise', branch: 'master'
+gem 'omniauth'
+gem 'omniauth-github'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
diff --git a/Gemfile.lock b/Gemfile.lock
index 3aedd58..ac04802 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,3 +1,15 @@
+GIT
+ remote: https://github.com/heartcombo/devise.git
+ revision: 0cd72a56f984a7ff089246f87a8b259120545edd
+ branch: master
+ specs:
+ devise (4.7.3)
+ bcrypt (~> 3.0)
+ orm_adapter (~> 0.1)
+ railties (>= 4.1.0)
+ responders
+ warden (~> 1.2.3)
+
GEM
remote: https://rubygems.org/
specs:
@@ -60,7 +72,7 @@ GEM
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
- bcrypt (3.1.13)
+ bcrypt (3.1.16)
bindex (0.8.1)
bootsnap (1.7.2)
msgpack (~> 1.0)
@@ -68,21 +80,22 @@ GEM
byebug (11.1.3)
concurrent-ruby (1.1.8)
crass (1.0.6)
- devise (4.7.1)
- bcrypt (~> 3.0)
- orm_adapter (~> 0.1)
- railties (>= 4.1.0)
- responders
- warden (~> 1.2.3)
- diff-lcs (1.3)
+ diff-lcs (1.4.4)
erubi (1.10.0)
+ faraday (1.3.0)
+ faraday-net_http (~> 1.0)
+ multipart-post (>= 1.2, < 3)
+ ruby2_keywords
+ faraday-net_http (1.0.1)
ffi (1.15.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
+ hashie (4.1.0)
i18n (1.8.9)
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
activesupport (>= 5.0.0)
+ jwt (2.2.2)
listen (3.4.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
@@ -99,10 +112,29 @@ GEM
mini_portile2 (2.5.0)
minitest (5.14.4)
msgpack (1.4.2)
+ multi_json (1.15.0)
+ multi_xml (0.6.0)
+ multipart-post (2.1.1)
nio4r (2.5.7)
nokogiri (1.11.1)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
+ oauth2 (1.4.4)
+ faraday (>= 0.8, < 2.0)
+ jwt (>= 1.0, < 3.0)
+ multi_json (~> 1.3)
+ multi_xml (~> 0.5)
+ rack (>= 1.2, < 3)
+ omniauth (2.0.3)
+ hashie (>= 3.4.6)
+ rack (>= 1.6.2, < 3)
+ rack-protection
+ omniauth-github (2.0.0)
+ omniauth (~> 2.0)
+ omniauth-oauth2 (~> 1.7.1)
+ omniauth-oauth2 (1.7.1)
+ oauth2 (~> 1.4)
+ omniauth (>= 1.9, < 3)
orm_adapter (0.5.0)
pg (1.2.3)
puma (5.2.2)
@@ -111,6 +143,8 @@ GEM
rack (2.2.3)
rack-mini-profiler (2.3.1)
rack (>= 1.2.0)
+ rack-protection (2.1.0)
+ rack
rack-proxy (0.6.5)
rack
rack-test (1.1.0)
@@ -145,26 +179,27 @@ GEM
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
- responders (3.0.0)
+ responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
- rspec-core (3.9.1)
- rspec-support (~> 3.9.1)
- rspec-expectations (3.9.0)
+ rspec-core (3.10.1)
+ rspec-support (~> 3.10.0)
+ rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
- rspec-support (~> 3.9.0)
- rspec-mocks (3.9.1)
+ rspec-support (~> 3.10.0)
+ rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
- rspec-support (~> 3.9.0)
- rspec-rails (4.0.0.beta4)
+ rspec-support (~> 3.10.0)
+ rspec-rails (4.1.0)
actionpack (>= 4.2)
activesupport (>= 4.2)
railties (>= 4.2)
- rspec-core (~> 3.9)
- rspec-expectations (~> 3.9)
- rspec-mocks (~> 3.9)
- rspec-support (~> 3.9)
- rspec-support (3.9.2)
+ rspec-core (~> 3.10)
+ rspec-expectations (~> 3.10)
+ rspec-mocks (~> 3.10)
+ rspec-support (~> 3.10)
+ rspec-support (3.10.2)
+ ruby2_keywords (0.0.4)
sass-rails (6.0.0)
sassc-rails (~> 2.1, >= 2.1.1)
sassc (2.4.0)
@@ -191,8 +226,8 @@ GEM
turbolinks-source (5.2.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
- warden (1.2.8)
- rack (>= 2.0.6)
+ warden (1.2.9)
+ rack (>= 2.0.9)
web-console (4.1.0)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
@@ -214,9 +249,11 @@ PLATFORMS
DEPENDENCIES
bootsnap (>= 1.4.4)
byebug
- devise
+ devise!
jbuilder (~> 2.7)
listen (~> 3.3)
+ omniauth
+ omniauth-github
pg (~> 1.1)
puma (~> 5.0)
rack-mini-profiler (~> 2.0)
diff --git a/app/controllers/callbacks_controller.rb b/app/controllers/callbacks_controller.rb
new file mode 100644
index 0000000..5efb8e9
--- /dev/null
+++ b/app/controllers/callbacks_controller.rb
@@ -0,0 +1,6 @@
+class CallbacksController < Devise::OmniauthCallbacksController
+ def github
+ @user = User.from_omniauth(request.env["omniauth.auth"])
+ sign_in_and_redirect @user
+ end
+end
diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb
new file mode 100644
index 0000000..ff06f02
--- /dev/null
+++ b/app/controllers/sessions_controller.rb
@@ -0,0 +1,23 @@
+class SessionController < ApplicationController
+ def new
+ # This route is catched by the Omniauth Middleware and is invisible to rake routes
+ redirect_to '/auth/github'
+ end
+
+ def create
+ user = User::Authenticable.authenticate(authorize_params)
+ session[:uid] = user.uid
+ redirect_to root_path
+ end
+
+ def destroy
+ session.delete(:uid)
+ redirect_to root_path
+ end
+
+ private
+
+ def authorize_params
+ request.env.fetch('omniauth.auth')
+ end
+end
diff --git a/app/models/user.rb b/app/models/user.rb
index 4756799..9ad8323 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: [:github]
+
+ class << self
+ def from_omniauth(auth)
+ where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
+ user.provider = auth.provider
+ user.uid = auth.uid
+ user.email = auth.info.email
+ user.password = Devise.friendly_token[0,20]
+ end
+ end
+ end
end
diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb
index 6d0feda..cc58aa6 100644
--- a/config/initializers/devise.rb
+++ b/config/initializers/devise.rb
@@ -259,7 +259,7 @@ Devise.setup do |config|
# ==> OmniAuth
# 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 :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'], scope: 'user,repo'
# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
diff --git a/config/routes.rb b/config/routes.rb
index 54b04d7..2891933 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
- devise_for :users
+ devise_for :users, controllers: { omniauth_callbacks: "callbacks" }
+ get 'session/new', as: :sign_in
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
diff --git a/db/migrate/20210308101347_add_columns_to_users.rb b/db/migrate/20210308101347_add_columns_to_users.rb
new file mode 100644
index 0000000..74be2ee
--- /dev/null
+++ b/db/migrate/20210308101347_add_columns_to_users.rb
@@ -0,0 +1,6 @@
+class AddColumnsToUsers < ActiveRecord::Migration[6.1]
+ def change
+ add_column :users, :provider, :string
+ add_column :users, :uid, :string
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
new file mode 100644
index 0000000..2af8d2c
--- /dev/null
+++ b/db/schema.rb
@@ -0,0 +1,32 @@
+# 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.
+#
+# This file is the source Rails uses to define your schema when running `bin/rails
+# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
+# be faster and is potentially less error prone than running all of your
+# migrations from scratch. Old migrations may fail to apply correctly if those
+# migrations use external dependencies or application code.
+#
+# It's strongly recommended that you check this file into your version control system.
+
+ActiveRecord::Schema.define(version: 2021_03_08_101347) do
+
+ # These are extensions that must be enabled in order to support this database
+ enable_extension "plpgsql"
+
+ create_table "users", force: :cascade do |t|
+ t.datetime "created_at", precision: 6, null: false
+ t.datetime "updated_at", precision: 6, null: false
+ t.string "email", default: "", null: false
+ t.string "encrypted_password", default: "", null: false
+ t.string "reset_password_token"
+ t.datetime "reset_password_sent_at"
+ t.datetime "remember_created_at"
+ t.string "provider"
+ t.string "uid"
+ 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
+
+end