Add Reddit login/logout
This commit is contained in:
parent
785f8a4d00
commit
fa05d6cc91
21 changed files with 185 additions and 84 deletions
2
.env.sample
Normal file
2
.env.sample
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
REDDIT_CLIENT_ID=
|
||||||
|
REDDIT_CLIENT_SECRET=
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -15,3 +15,6 @@
|
||||||
/log/*
|
/log/*
|
||||||
!/log/.keep
|
!/log/.keep
|
||||||
/tmp
|
/tmp
|
||||||
|
|
||||||
|
# mine
|
||||||
|
.env
|
||||||
|
|
29
Gemfile
29
Gemfile
|
@ -1,47 +1,28 @@
|
||||||
source 'https://rubygems.org'
|
source 'https://rubygems.org'
|
||||||
|
|
||||||
|
|
||||||
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
|
|
||||||
gem 'rails', '4.2.4'
|
gem 'rails', '4.2.4'
|
||||||
# Use sqlite3 as the database for Active Record
|
|
||||||
gem 'sqlite3'
|
gem 'sqlite3'
|
||||||
# Use SCSS for stylesheets
|
|
||||||
gem 'sass-rails', '~> 5.0'
|
gem 'sass-rails', '~> 5.0'
|
||||||
# Use Uglifier as compressor for JavaScript assets
|
|
||||||
gem 'uglifier', '>= 1.3.0'
|
gem 'uglifier', '>= 1.3.0'
|
||||||
# Use CoffeeScript for .coffee assets and views
|
|
||||||
gem 'coffee-rails', '~> 4.1.0'
|
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'
|
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'
|
gem 'jbuilder', '~> 2.0'
|
||||||
# bundle exec rake doc:rails generates the API under doc/api.
|
|
||||||
gem 'sdoc', '~> 0.4.0', group: :doc
|
gem 'sdoc', '~> 0.4.0', group: :doc
|
||||||
|
|
||||||
# Use ActiveModel has_secure_password
|
gem 'slim-rails'
|
||||||
# gem 'bcrypt', '~> 3.1.7'
|
|
||||||
|
|
||||||
# Use Unicorn as the app server
|
gem 'omniauth-reddit', :git => 'git://github.com/jackdempsey/omniauth-reddit.git'
|
||||||
# gem 'unicorn'
|
|
||||||
|
|
||||||
# Use Capistrano for deployment
|
|
||||||
# gem 'capistrano-rails', group: :development
|
|
||||||
|
|
||||||
group :development, :test do
|
group :development, :test do
|
||||||
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
|
gem 'dotenv-rails'
|
||||||
gem 'byebug'
|
gem 'byebug'
|
||||||
end
|
end
|
||||||
|
|
||||||
group :development do
|
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'
|
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'
|
gem 'spring'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
54
Gemfile.lock
54
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
|
GEM
|
||||||
remote: https://rubygems.org/
|
remote: https://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
|
@ -37,10 +44,15 @@ GEM
|
||||||
thread_safe (~> 0.3, >= 0.3.4)
|
thread_safe (~> 0.3, >= 0.3.4)
|
||||||
tzinfo (~> 1.1)
|
tzinfo (~> 1.1)
|
||||||
arel (6.0.3)
|
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)
|
binding_of_caller (0.7.2)
|
||||||
debug_inspector (>= 0.0.1)
|
debug_inspector (>= 0.0.1)
|
||||||
builder (3.2.2)
|
builder (3.2.2)
|
||||||
byebug (6.0.2)
|
byebug (6.0.2)
|
||||||
|
coderay (1.1.0)
|
||||||
coffee-rails (4.1.0)
|
coffee-rails (4.1.0)
|
||||||
coffee-script (>= 2.2.0)
|
coffee-script (>= 2.2.0)
|
||||||
railties (>= 4.0.0, < 5.0)
|
railties (>= 4.0.0, < 5.0)
|
||||||
|
@ -49,10 +61,17 @@ GEM
|
||||||
execjs
|
execjs
|
||||||
coffee-script-source (1.9.1.1)
|
coffee-script-source (1.9.1.1)
|
||||||
debug_inspector (0.0.2)
|
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)
|
erubis (2.7.0)
|
||||||
execjs (2.6.0)
|
execjs (2.6.0)
|
||||||
|
faraday (0.9.1)
|
||||||
|
multipart-post (>= 1.2, < 3)
|
||||||
globalid (0.3.6)
|
globalid (0.3.6)
|
||||||
activesupport (>= 4.1.0)
|
activesupport (>= 4.1.0)
|
||||||
|
hashie (3.4.2)
|
||||||
i18n (0.7.0)
|
i18n (0.7.0)
|
||||||
jbuilder (2.3.1)
|
jbuilder (2.3.1)
|
||||||
activesupport (>= 3.0.0, < 5)
|
activesupport (>= 3.0.0, < 5)
|
||||||
|
@ -62,6 +81,7 @@ GEM
|
||||||
railties (>= 4.2.0)
|
railties (>= 4.2.0)
|
||||||
thor (>= 0.14, < 2.0)
|
thor (>= 0.14, < 2.0)
|
||||||
json (1.8.3)
|
json (1.8.3)
|
||||||
|
jwt (1.5.1)
|
||||||
loofah (2.0.3)
|
loofah (2.0.3)
|
||||||
nokogiri (>= 1.5.9)
|
nokogiri (>= 1.5.9)
|
||||||
mail (2.6.3)
|
mail (2.6.3)
|
||||||
|
@ -70,8 +90,24 @@ GEM
|
||||||
mini_portile (0.6.2)
|
mini_portile (0.6.2)
|
||||||
minitest (5.8.1)
|
minitest (5.8.1)
|
||||||
multi_json (1.11.2)
|
multi_json (1.11.2)
|
||||||
|
multi_xml (0.5.5)
|
||||||
|
multipart-post (2.0.0)
|
||||||
nokogiri (1.6.6.2)
|
nokogiri (1.6.6.2)
|
||||||
mini_portile (~> 0.6.0)
|
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 (1.6.4)
|
||||||
rack-test (0.6.3)
|
rack-test (0.6.3)
|
||||||
rack (>= 1.0)
|
rack (>= 1.0)
|
||||||
|
@ -111,6 +147,15 @@ GEM
|
||||||
sdoc (0.4.1)
|
sdoc (0.4.1)
|
||||||
json (~> 1.7, >= 1.7.7)
|
json (~> 1.7, >= 1.7.7)
|
||||||
rdoc (~> 4.0)
|
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)
|
spring (1.4.0)
|
||||||
sprockets (3.3.5)
|
sprockets (3.3.5)
|
||||||
rack (> 1, < 3)
|
rack (> 1, < 3)
|
||||||
|
@ -119,11 +164,10 @@ GEM
|
||||||
activesupport (>= 3.0)
|
activesupport (>= 3.0)
|
||||||
sprockets (>= 2.8, < 4.0)
|
sprockets (>= 2.8, < 4.0)
|
||||||
sqlite3 (1.3.10)
|
sqlite3 (1.3.10)
|
||||||
|
temple (0.7.6)
|
||||||
thor (0.19.1)
|
thor (0.19.1)
|
||||||
thread_safe (0.3.5)
|
thread_safe (0.3.5)
|
||||||
tilt (2.0.1)
|
tilt (2.0.1)
|
||||||
turbolinks (2.5.3)
|
|
||||||
coffee-rails
|
|
||||||
tzinfo (1.2.2)
|
tzinfo (1.2.2)
|
||||||
thread_safe (~> 0.1)
|
thread_safe (~> 0.1)
|
||||||
uglifier (2.7.2)
|
uglifier (2.7.2)
|
||||||
|
@ -139,16 +183,20 @@ PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
|
better_errors
|
||||||
byebug
|
byebug
|
||||||
coffee-rails (~> 4.1.0)
|
coffee-rails (~> 4.1.0)
|
||||||
|
dotenv-rails
|
||||||
jbuilder (~> 2.0)
|
jbuilder (~> 2.0)
|
||||||
jquery-rails
|
jquery-rails
|
||||||
|
omniauth-reddit!
|
||||||
|
quiet_assets
|
||||||
rails (= 4.2.4)
|
rails (= 4.2.4)
|
||||||
sass-rails (~> 5.0)
|
sass-rails (~> 5.0)
|
||||||
sdoc (~> 0.4.0)
|
sdoc (~> 0.4.0)
|
||||||
|
slim-rails
|
||||||
spring
|
spring
|
||||||
sqlite3
|
sqlite3
|
||||||
turbolinks
|
|
||||||
uglifier (>= 1.3.0)
|
uglifier (>= 1.3.0)
|
||||||
web-console (~> 2.0)
|
web-console (~> 2.0)
|
||||||
|
|
||||||
|
|
|
@ -12,5 +12,4 @@
|
||||||
//
|
//
|
||||||
//= require jquery
|
//= require jquery
|
||||||
//= require jquery_ujs
|
//= require jquery_ujs
|
||||||
//= require turbolinks
|
|
||||||
//= require_tree .
|
//= require_tree .
|
||||||
|
|
3
app/assets/javascripts/sessions.coffee
Normal file
3
app/assets/javascripts/sessions.coffee
Normal file
|
@ -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
app/assets/javascripts/welcome.coffee
Normal file
3
app/assets/javascripts/welcome.coffee
Normal file
|
@ -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
app/assets/stylesheets/sessions.scss
Normal file
3
app/assets/stylesheets/sessions.scss
Normal file
|
@ -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/
|
3
app/assets/stylesheets/welcome.scss
Normal file
3
app/assets/stylesheets/welcome.scss
Normal file
|
@ -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/
|
|
@ -1,5 +1,16 @@
|
||||||
class ApplicationController < ActionController::Base
|
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
|
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
|
end
|
||||||
|
|
21
app/controllers/sessions_controller.rb
Normal file
21
app/controllers/sessions_controller.rb
Normal file
|
@ -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
|
4
app/controllers/welcome_controller.rb
Normal file
4
app/controllers/welcome_controller.rb
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
class WelcomeController < ApplicationController
|
||||||
|
def index
|
||||||
|
end
|
||||||
|
end
|
2
app/helpers/sessions_helper.rb
Normal file
2
app/helpers/sessions_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
module SessionsHelper
|
||||||
|
end
|
2
app/helpers/welcome_helper.rb
Normal file
2
app/helpers/welcome_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
module WelcomeHelper
|
||||||
|
end
|
10
app/models/user.rb
Normal file
10
app/models/user.rb
Normal file
|
@ -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
|
|
@ -2,12 +2,18 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>DbzdokkanRails</title>
|
<title>DbzdokkanRails</title>
|
||||||
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
<%= stylesheet_link_tag 'application', media: 'all' %>
|
||||||
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
<%= javascript_include_tag 'application' %>
|
||||||
<%= csrf_meta_tags %>
|
<%= csrf_meta_tags %>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
|
<% if logged_in? %>
|
||||||
|
<p><%= link_to 'Logout', logout_path, method: :delete %></p>
|
||||||
|
<% else %>
|
||||||
|
<p><a href="/auth/reddit">Login</a></p>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<%= yield %>
|
<%= yield %>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
4
app/views/welcome/index.html.slim
Normal file
4
app/views/welcome/index.html.slim
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
- if logged_in?
|
||||||
|
h1= "Welcome, #{current_user.nickname}"
|
||||||
|
- else
|
||||||
|
h1 Welcome. Please login!
|
6
config/initializers/omniauth.rb
Normal file
6
config/initializers/omniauth.rb
Normal file
|
@ -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
|
|
@ -1,56 +1,9 @@
|
||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
# The priority is based upon order of creation: first created -> highest priority.
|
get 'auth/:provider/callback', to: 'sessions#create'
|
||||||
# See how all your routes lay out with "rake routes".
|
get 'auth/failure', to: 'sessions#failure'
|
||||||
|
delete 'logout', to: 'sessions#destroy', as: 'logout'
|
||||||
|
|
||||||
# You can have the root of your site routed with "root"
|
resources :welcome, only: [:index]
|
||||||
# root 'welcome#index'
|
|
||||||
|
|
||||||
# Example of regular route:
|
root to: 'welcome#index'
|
||||||
# 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
|
|
||||||
end
|
end
|
||||||
|
|
12
db/migrate/20150930004246_create_users.rb
Normal file
12
db/migrate/20150930004246_create_users.rb
Normal file
|
@ -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
|
25
db/schema.rb
Normal file
25
db/schema.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue