diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..af35a07 --- /dev/null +++ b/.env.sample @@ -0,0 +1,2 @@ +REDDIT_CLIENT_ID= +REDDIT_CLIENT_SECRET= diff --git a/.gitignore b/.gitignore index 050c9d9..0c4b3e8 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ /log/* !/log/.keep /tmp + +# mine +.env diff --git a/Gemfile b/Gemfile index 37bc11c..b60d02f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,47 +1,28 @@ source 'https://rubygems.org' - -# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.4' -# Use sqlite3 as the database for Active Record gem 'sqlite3' -# Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' -# Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' -# Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.1.0' -# See https://github.com/rails/execjs#readme for more supported runtimes -# gem 'therubyracer', platforms: :ruby -# Use jquery as the JavaScript library gem 'jquery-rails' -# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks -gem 'turbolinks' -# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.0' -# bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc -# Use ActiveModel has_secure_password -# gem 'bcrypt', '~> 3.1.7' +gem 'slim-rails' -# Use Unicorn as the app server -# gem 'unicorn' - -# Use Capistrano for deployment -# gem 'capistrano-rails', group: :development +gem 'omniauth-reddit', :git => 'git://github.com/jackdempsey/omniauth-reddit.git' group :development, :test do - # Call 'byebug' anywhere in the code to stop execution and get a debugger console + gem 'dotenv-rails' gem 'byebug' end group :development do - # Access an IRB console on exception pages or by using <%= console %> in views + gem 'better_errors' + gem 'quiet_assets' gem 'web-console', '~> 2.0' - - # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end diff --git a/Gemfile.lock b/Gemfile.lock index d12767e..793a84b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,10 @@ +GIT + remote: git://github.com/jackdempsey/omniauth-reddit.git + revision: 4e54423f2d8a000e010998b53fa653adb7137baa + specs: + omniauth-reddit (0.0.2) + omniauth-oauth2 (~> 1.1) + GEM remote: https://rubygems.org/ specs: @@ -37,10 +44,15 @@ GEM thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) arel (6.0.3) + better_errors (2.1.1) + coderay (>= 1.0.0) + erubis (>= 2.6.6) + rack (>= 0.9.0) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) builder (3.2.2) byebug (6.0.2) + coderay (1.1.0) coffee-rails (4.1.0) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) @@ -49,10 +61,17 @@ GEM execjs coffee-script-source (1.9.1.1) debug_inspector (0.0.2) + dotenv (2.0.2) + dotenv-rails (2.0.2) + dotenv (= 2.0.2) + railties (~> 4.0) erubis (2.7.0) execjs (2.6.0) + faraday (0.9.1) + multipart-post (>= 1.2, < 3) globalid (0.3.6) activesupport (>= 4.1.0) + hashie (3.4.2) i18n (0.7.0) jbuilder (2.3.1) activesupport (>= 3.0.0, < 5) @@ -62,6 +81,7 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.3) + jwt (1.5.1) loofah (2.0.3) nokogiri (>= 1.5.9) mail (2.6.3) @@ -70,8 +90,24 @@ GEM mini_portile (0.6.2) minitest (5.8.1) multi_json (1.11.2) + multi_xml (0.5.5) + multipart-post (2.0.0) nokogiri (1.6.6.2) mini_portile (~> 0.6.0) + oauth2 (1.0.0) + faraday (>= 0.8, < 0.10) + jwt (~> 1.0) + multi_json (~> 1.3) + multi_xml (~> 0.5) + rack (~> 1.2) + omniauth (1.2.2) + hashie (>= 1.2, < 4) + rack (~> 1.0) + omniauth-oauth2 (1.3.1) + oauth2 (~> 1.0) + omniauth (~> 1.2) + quiet_assets (1.1.0) + railties (>= 3.1, < 5.0) rack (1.6.4) rack-test (0.6.3) rack (>= 1.0) @@ -111,6 +147,15 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + slim (3.0.6) + temple (~> 0.7.3) + tilt (>= 1.3.3, < 2.1) + slim-rails (3.0.1) + actionmailer (>= 3.1, < 5.0) + actionpack (>= 3.1, < 5.0) + activesupport (>= 3.1, < 5.0) + railties (>= 3.1, < 5.0) + slim (~> 3.0) spring (1.4.0) sprockets (3.3.5) rack (> 1, < 3) @@ -119,11 +164,10 @@ GEM activesupport (>= 3.0) sprockets (>= 2.8, < 4.0) sqlite3 (1.3.10) + temple (0.7.6) thor (0.19.1) thread_safe (0.3.5) tilt (2.0.1) - turbolinks (2.5.3) - coffee-rails tzinfo (1.2.2) thread_safe (~> 0.1) uglifier (2.7.2) @@ -139,16 +183,20 @@ PLATFORMS ruby DEPENDENCIES + better_errors byebug coffee-rails (~> 4.1.0) + dotenv-rails jbuilder (~> 2.0) jquery-rails + omniauth-reddit! + quiet_assets rails (= 4.2.4) sass-rails (~> 5.0) sdoc (~> 0.4.0) + slim-rails spring sqlite3 - turbolinks uglifier (>= 1.3.0) web-console (~> 2.0) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index e07c5a8..646c5ab 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -12,5 +12,4 @@ // //= require jquery //= require jquery_ujs -//= require turbolinks //= require_tree . diff --git a/app/assets/javascripts/sessions.coffee b/app/assets/javascripts/sessions.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/sessions.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/ diff --git a/app/assets/javascripts/welcome.coffee b/app/assets/javascripts/welcome.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/welcome.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/ diff --git a/app/assets/stylesheets/sessions.scss b/app/assets/stylesheets/sessions.scss new file mode 100644 index 0000000..ccb1ed2 --- /dev/null +++ b/app/assets/stylesheets/sessions.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/welcome.scss b/app/assets/stylesheets/welcome.scss new file mode 100644 index 0000000..4e19aa4 --- /dev/null +++ b/app/assets/stylesheets/welcome.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Welcome 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..33af337 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,16 @@ 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 + helper_method :logged_in? + + private + + def current_user + @current_user ||= User.find(session[:user_id]) if session[:user_id] + end + + def logged_in? + @logged_in ||= !!current_user + end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..e44cfcb --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,21 @@ +class SessionsController < ApplicationController + def create + user = User.create_with_omniauth(env['omniauth.auth']) + session[:user_id] = user.id + + require 'pp' + pp env['omniauth.auth'] + + redirect_to root_path, notice: 'You are now logged in' + end + + def destroy + session[:user_id] = nil + + redirect_to root_path, notice: 'You have been logged out' + end + + def failure + redirect_to root_path, notice: 'Failed to login' + end +end diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb new file mode 100644 index 0000000..35c05dc --- /dev/null +++ b/app/controllers/welcome_controller.rb @@ -0,0 +1,4 @@ +class WelcomeController < ApplicationController + def index + end +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/welcome_helper.rb b/app/helpers/welcome_helper.rb new file mode 100644 index 0000000..eeead45 --- /dev/null +++ b/app/helpers/welcome_helper.rb @@ -0,0 +1,2 @@ +module WelcomeHelper +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..1bddd8b --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,10 @@ +class User < ActiveRecord::Base + def self.create_with_omniauth(auth) + where(provider: auth[:provider], uid: auth[:uid]).first_or_create do |user| + user.provider = auth[:provider] + user.uid = auth[:uid] + user.nickname = auth[:info][:name] + user.email = auth[:info][:email] + end + end +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 93889d6..447a43f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -2,12 +2,18 @@ DbzdokkanRails - <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> - <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> + <%= stylesheet_link_tag 'application', media: 'all' %> + <%= javascript_include_tag 'application' %> <%= csrf_meta_tags %> + <% if logged_in? %> +

<%= link_to 'Logout', logout_path, method: :delete %>

+ <% else %> +

Login

+ <% end %> + <%= yield %> diff --git a/app/views/welcome/index.html.slim b/app/views/welcome/index.html.slim new file mode 100644 index 0000000..acd372f --- /dev/null +++ b/app/views/welcome/index.html.slim @@ -0,0 +1,4 @@ +- if logged_in? + h1= "Welcome, #{current_user.nickname}" +- else + h1 Welcome. Please login! diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 0000000..c33147a --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,6 @@ +Rails.application.config.middleware.use OmniAuth::Builder do + provider :reddit, + ENV['REDDIT_CLIENT_ID'], + ENV['REDDIT_CLIENT_SECRET'], + scope: 'identity' +end diff --git a/config/routes.rb b/config/routes.rb index 3f66539..eb69c8e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,56 +1,9 @@ Rails.application.routes.draw do - # The priority is based upon order of creation: first created -> highest priority. - # See how all your routes lay out with "rake routes". + get 'auth/:provider/callback', to: 'sessions#create' + get 'auth/failure', to: 'sessions#failure' + delete 'logout', to: 'sessions#destroy', as: 'logout' - # You can have the root of your site routed with "root" - # root 'welcome#index' + resources :welcome, only: [:index] - # Example of regular route: - # get 'products/:id' => 'catalog#view' - - # Example of named route that can be invoked with purchase_url(id: product.id) - # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase - - # Example resource route (maps HTTP verbs to controller actions automatically): - # resources :products - - # Example resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end - # - # collection do - # get 'sold' - # end - # end - - # Example resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - - # Example resource route with more complex sub-resources: - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', on: :collection - # end - # end - - # Example resource route with concerns: - # concern :toggleable do - # post 'toggle' - # end - # resources :posts, concerns: :toggleable - # resources :photos, concerns: :toggleable - - # Example resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end + root to: 'welcome#index' end diff --git a/db/migrate/20150930004246_create_users.rb b/db/migrate/20150930004246_create_users.rb new file mode 100644 index 0000000..3121755 --- /dev/null +++ b/db/migrate/20150930004246_create_users.rb @@ -0,0 +1,12 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users do |t| + t.string :provider + t.string :uid + t.string :nickname + t.string :email + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..dabf3ed --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,25 @@ +# encoding: UTF-8 +# 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: 20150930004246) do + + create_table "users", force: :cascade do |t| + t.string "provider" + t.string "uid" + t.string "nickname" + t.string "email" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end