1
0
Fork 0

Add Reddit login/logout

This commit is contained in:
Andrew Tomaka 2015-09-29 21:21:05 -04:00
parent 785f8a4d00
commit fa05d6cc91
21 changed files with 185 additions and 84 deletions

2
.env.sample Normal file
View File

@ -0,0 +1,2 @@
REDDIT_CLIENT_ID=
REDDIT_CLIENT_SECRET=

3
.gitignore vendored
View File

@ -15,3 +15,6 @@
/log/*
!/log/.keep
/tmp
# mine
.env

29
Gemfile
View File

@ -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

View File

@ -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)

View File

@ -12,5 +12,4 @@
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

View 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/

View 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/

View 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/

View 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/

View File

@ -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

View 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

View File

@ -0,0 +1,4 @@
class WelcomeController < ApplicationController
def index
end
end

View File

@ -0,0 +1,2 @@
module SessionsHelper
end

View File

@ -0,0 +1,2 @@
module WelcomeHelper
end

10
app/models/user.rb Normal file
View 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

View File

@ -2,12 +2,18 @@
<html>
<head>
<title>DbzdokkanRails</title>
<%= 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 %>
</head>
<body>
<% if logged_in? %>
<p><%= link_to 'Logout', logout_path, method: :delete %></p>
<% else %>
<p><a href="/auth/reddit">Login</a></p>
<% end %>
<%= yield %>
</body>

View File

@ -0,0 +1,4 @@
- if logged_in?
h1= "Welcome, #{current_user.nickname}"
- else
h1 Welcome. Please login!

View 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

View File

@ -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

View 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
View 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