Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Commit

Permalink
Set up auth/sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
EricPickup committed Feb 8, 2019
1 parent 21f2856 commit c5c4bbb
Show file tree
Hide file tree
Showing 29 changed files with 200 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -21,3 +21,5 @@
/yarn-error.log

.byebug_history

/config/local_env.yml
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -52,3 +52,5 @@ end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'omniauth-google-oauth2'
23 changes: 23 additions & 0 deletions Gemfile.lock
Expand Up @@ -64,14 +64,18 @@ GEM
crass (1.0.4)
erubi (1.8.0)
execjs (2.7.0)
faraday (0.15.4)
multipart-post (>= 1.2, < 3)
ffi (1.10.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
hashie (3.6.0)
i18n (1.5.3)
concurrent-ruby (~> 1.0)
jbuilder (2.8.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
jwt (2.1.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -86,9 +90,27 @@ GEM
mini_portile2 (2.4.0)
minitest (5.11.3)
multi_json (1.13.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
nio4r (2.3.1)
nokogiri (1.10.1)
mini_portile2 (~> 2.4.0)
oauth2 (1.4.1)
faraday (>= 0.8, < 0.16.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.9.0)
hashie (>= 3.4.6, < 3.7.0)
rack (>= 1.6.2, < 3)
omniauth-google-oauth2 (0.6.0)
jwt (>= 2.0)
omniauth (>= 1.1.1)
omniauth-oauth2 (>= 1.5)
omniauth-oauth2 (1.6.0)
oauth2 (~> 1.1)
omniauth (~> 1.9)
public_suffix (3.0.3)
puma (3.12.0)
rack (2.0.6)
Expand Down Expand Up @@ -180,6 +202,7 @@ DEPENDENCIES
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
omniauth-google-oauth2
puma (~> 3.7)
rails (~> 5.1.6, >= 5.1.6.1)
sass-rails (~> 5.0)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/events.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/session.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/events.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the Events controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/session.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the Session controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
12 changes: 12 additions & 0 deletions app/controllers/application_controller.rb
@@ -1,3 +1,15 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

helper_method :current_user, :logged_in?

skip_before_action :verify_authenticity_token

def current_user
current_user ||= User.find(session[:user_id]) if session[:user_id]
end

def logged_in?
!!current_user
end
end
4 changes: 4 additions & 0 deletions app/controllers/events_controller.rb
@@ -0,0 +1,4 @@
class EventsController < ApplicationController
def index
end
end
24 changes: 24 additions & 0 deletions app/controllers/session_controller.rb
@@ -0,0 +1,24 @@
class SessionController < ApplicationController
def create
user = User.find_or_create_by(:email => auth_hash[:info][:email]) do |user|
user.email = auth_hash[:info][:email]
user.name = auth_hash[:info][:name]
end

session[:user_id] = user.id

redirect_to root_path
end

def destroy
reset_session

redirect_to root_path
end

private

def auth_hash
request.env["omniauth.auth"]
end
end
2 changes: 2 additions & 0 deletions app/helpers/events_helper.rb
@@ -0,0 +1,2 @@
module EventsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/session_helper.rb
@@ -0,0 +1,2 @@
module SessionHelper
end
2 changes: 2 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,2 @@
class User < ApplicationRecord
end
11 changes: 11 additions & 0 deletions app/views/events/index.html.erb
@@ -0,0 +1,11 @@
<center>
<br><br>
<h1>Login Page</h1><br>

<% if current_user %>
<p>Signed in as: <%= current_user.email %></p>
<%= link_to "Log out", :logout %>
<% else %>
<%= link_to "Log in", :google_auth %>
<% end %>
</center>
2 changes: 2 additions & 0 deletions app/views/session/create.html.erb
@@ -0,0 +1,2 @@
<h1>Session#create</h1>
<p>Find me in app/views/session/create.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/session/destroy.html.erb
@@ -0,0 +1,2 @@
<h1>Session#destroy</h1>
<p>Find me in app/views/session/destroy.html.erb</p>
5 changes: 0 additions & 5 deletions bin/rails
@@ -1,9 +1,4 @@
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
5 changes: 0 additions & 5 deletions bin/rake
@@ -1,9 +1,4 @@
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
require_relative '../config/boot'
require 'rake'
Rake.application.run
17 changes: 0 additions & 17 deletions bin/spring

This file was deleted.

10 changes: 10 additions & 0 deletions config/application.rb
Expand Up @@ -11,8 +11,18 @@ class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1

config.before_configuration do
env_file = File.join(Rails.root, 'config', 'local_env.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value
end if File.exists?(env_file)
end

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV['GOOGLE_CLIENT'], ENV['GOOGLE_SECRET']
end
end
end
10 changes: 9 additions & 1 deletion config/routes.rb
@@ -1,3 +1,11 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get 'events/index'

get 'session/destroy', :as => 'logout'

get 'auth/google_oauth2', :as => 'google_auth'
match 'auth/:provider/callback' => 'session#create', :via => [:post, :get]

# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root :to => "events#index"
end
8 changes: 8 additions & 0 deletions db/migrate/20190207051645_create_users.rb
@@ -0,0 +1,8 @@
class CreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20190207052734_add_email_to_user.rb
@@ -0,0 +1,5 @@
class AddEmailToUser < ActiveRecord::Migration[5.1]
def change
add_column :users, :email, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20190207052747_add_name_to_user.rb
@@ -0,0 +1,5 @@
class AddNameToUser < ActiveRecord::Migration[5.1]
def change
add_column :users, :name, :string
end
end
22 changes: 22 additions & 0 deletions db/schema.rb
@@ -0,0 +1,22 @@
# 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.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20190207052747) do

create_table "users", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email"
t.string "name"
end

end
9 changes: 9 additions & 0 deletions test/controllers/events_controller_test.rb
@@ -0,0 +1,9 @@
require 'test_helper'

class EventsControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get events_index_url
assert_response :success
end

end
14 changes: 14 additions & 0 deletions test/controllers/session_controller_test.rb
@@ -0,0 +1,14 @@
require 'test_helper'

class SessionControllerTest < ActionDispatch::IntegrationTest
test "should get create" do
get session_create_url
assert_response :success
end

test "should get destroy" do
get session_destroy_url
assert_response :success
end

end
11 changes: 11 additions & 0 deletions test/fixtures/users.yml
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/user_test.rb
@@ -0,0 +1,7 @@
require 'test_helper'

class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit c5c4bbb

Please sign in to comment.