diff --git a/.travis.yml b/.travis.yml index 8b624b3..8ffbdcb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,3 +3,5 @@ rvm: - 2.1.1 before_install: - cp config/secrets.yml.sample config/secrets.yml +before_script: + - bin/rake db:migrate RAILS_ENV=test diff --git a/app/assets/javascripts/sessions.js b/app/assets/javascripts/sessions.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/sessions.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/javascripts/users.js b/app/assets/javascripts/users.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/users.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/sessions.css.scss b/app/assets/stylesheets/sessions.css.scss new file mode 100644 index 0000000..7bef9cf --- /dev/null +++ b/app/assets/stylesheets/sessions.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the sessions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/users.css.scss b/app/assets/stylesheets/users.css.scss new file mode 100644 index 0000000..1efc835 --- /dev/null +++ b/app/assets/stylesheets/users.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the users controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..c02c56f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,13 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + + helper_method :current_user + + private + + # @return [User] + def current_user + @current_user ||= User.find(session[:user_id]) if session + end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..7011a92 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,15 @@ +class SessionsController < ApplicationController + + def create + auth = request.env["omniauth.auth"] + user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth) + session[:user_id] = user.id + redirect_to root_url, :notice => "Signed in!" + end + + def destroy + sessions[:user_id] = nil + redirect_to root_path, notice: 'Signed out!' + end + +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..3e74dea --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,2 @@ +class UsersController < ApplicationController +end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..309f8b2 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..2310a24 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..6029a6d --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,11 @@ +class User < ActiveRecord::Base + + # @return [User] + def self.create_with_omniauth(auth) + create! do |user| + user.provider = auth["provider"] + user.uid = auth["uid"] + user.name = auth["info"]["name"] + end + end +end diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 2bc7295..132fdcc 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -27,6 +27,7 @@ html lang="en" a.brand href="/"= site_name .container.nav-collapse ul.nav + li= link_to "Sign in", "/auth/github" li= link_to "Project on GitHub", "http://github.com/android-frontier" .container style="margin-top: 64px" diff --git a/config/initializers/omniauth_github.rb b/config/initializers/omniauth_github.rb index fcaef06..d602248 100644 --- a/config/initializers/omniauth_github.rb +++ b/config/initializers/omniauth_github.rb @@ -4,5 +4,11 @@ ) config = Rails.application.secrets - provider :github, config['github_key'], config['github_secret'], scope: scope + provider :github, config['github_key'], config['github_secret'], scope: scope, + client_options: { + site: config['github_site'], + authorize_url: config['github_authorize_url'], + token_url: config['github_token_url'], + } + end diff --git a/config/routes.rb b/config/routes.rb index 3c79ba5..fc7c010 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,4 +20,9 @@ } end end + + + # sessions + get "/auth/:provider/callback" => "sessions#create" + get "/signout" => "sessions#destroy", :as => :signout end diff --git a/config/secrets.yml.sample b/config/secrets.yml.sample index c2279c7..5989cf8 100644 --- a/config/secrets.yml.sample +++ b/config/secrets.yml.sample @@ -9,6 +9,9 @@ development: artifact_root_path: <%= ENV["ARTIFACT_ROOT_PATH"] %> github_key: <%= ENV["GITHUB_KEY"] %> github_secret: <%= ENV["GITHUB_SECRET"] %> + github_site: 'https://api.github.com/' + github_authorize_url: 'https://github.com/login/oauth/authorize' + github_token_url: 'https://github.com/login/oauth/access_token' site: *site test: diff --git a/db/migrate/20140808095155_create_users.rb b/db/migrate/20140808095155_create_users.rb new file mode 100644 index 0000000..cb4a56e --- /dev/null +++ b/db/migrate/20140808095155_create_users.rb @@ -0,0 +1,11 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users do |t| + t.string :provider + t.string :uid + t.string :name + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 4dfbb16..8eee1b1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,6 +11,14 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 20140808095155) do + + create_table "users", force: true do |t| + t.string "provider" + t.string "uid" + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end end diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb new file mode 100644 index 0000000..d30ebc3 --- /dev/null +++ b/test/controllers/sessions_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SessionsControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 0000000..d23f182 --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UsersControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..2f72a66 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + provider: MyString + uid: MyString + name: MyString + +two: + provider: MyString + uid: MyString + name: MyString diff --git a/test/helpers/sessions_helper_test.rb b/test/helpers/sessions_helper_test.rb new file mode 100644 index 0000000..7d44e09 --- /dev/null +++ b/test/helpers/sessions_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class SessionsHelperTest < ActionView::TestCase +end diff --git a/test/helpers/users_helper_test.rb b/test/helpers/users_helper_test.rb new file mode 100644 index 0000000..96af37a --- /dev/null +++ b/test/helpers/users_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class UsersHelperTest < ActionView::TestCase +end diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..82f61e0 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end