Skip to content

Instantly share code, notes, and snippets.

@avinashbot
Created April 10, 2017 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avinashbot/f298efca5622d77e4ba65abc57c253e4 to your computer and use it in GitHub Desktop.
Save avinashbot/f298efca5622d77e4ba65abc57c253e4 to your computer and use it in GitHub Desktop.
Redd Webapp Example (from https://redd.it/61p1hj)
require 'bundler/setup'
require 'sinatra'
require 'redd/middleware'
use Rack::Session::Cookie
use Redd::Middleware,
user_agent: 'Redd:Your App:v1.0.0 (by /u/<your username>)',
client_id: '<your client id>',
secret: '<your app secret>',
redirect_uri: '<your redirect uri>',
scope: %w(identity),
via: '/login'
get '/' do
'<a href="/login">login with reddit</a>'
end
get '/redirect' do
redirect to('/profile') if request.env['redd.error'].nil?
if request.env['redd.error'].message == 'access_denied'
'Sorry, you clicked decline. <a href="/login">Login again?</a>'
elsif request.env['redd.error'].message == 'invalid_state'
'Did you login through our website? <a href="/login">(No)</a>'
else
puts "Error while logging in!"
raise request.env['redd.error'] # Raise a 500 and make a mental note to look at the logs later
end
end
get '/profile' do
"Hi, #{request.env['redd.session'].me.name}! <a href='/logout'>Log out</a>"
end
get '/logout' do
request.env['redd.session'] = nil
redirect to('/')
end
source 'https://rubygems.org'
gem 'sinatra', '2.0.0.rc2' # Currently the latest Sinatra.
gem 'redd', '~> 0.8'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment