Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

70 lines (59 sloc) 1.822 kB
require 'bundler/setup'
Bundler.require
require 'sinatra'
Bundler.require :development if development?
require 'yaml'
require_relative './env.rb'
class Controller < Sinatra::Base
helpers do
def current_user
@current_user ||= DB[:users].filter(id: session[:user_id]).first if session[:user_id]
end
end
configure do
use Rack::Session::Cookie, :key => 'session',
:path => '/',
:expire_after => 2592000,
:secret => 'github.history.secret'
use OmniAuth::Strategies::GitHub, $config['github']['client_id'], $config['github']['client_secret']
end
before do
end
get '/' do
@title = "Github History"
if current_user
puts "Signed in as #{current_user[:username]}"
else
puts "Not logged in"
end
erb :'index'
end
["/sign_in/?", "/sign-in/?", "/signin/?", "/log_in/?", "/log-in/?", "/login/?", "/sign_up/?", "/sign-up/?", "/signup/?"].each do |path|
get path do
redirect '/auth/github'
end
end
["/sign_out/?", "/sign-out/?", "/signout/?", "/log_out/?", "/log-out/?", "/logout/?"].each do |path|
get path do
session[:user_id] = nil
redirect '/'
end
end
get '/auth/github/callback' do
auth = request.env["omniauth.auth"]
user = DB[:users].filter(uid: auth["uid"]).first
if user.nil?
DB[:users] << {
uid: auth["uid"],
username: auth["info"]["nickname"],
github_access_token: auth["credentials"]["token"],
date_created: Time.now
}
user = DB[:users].filter(uid: auth["uid"]).first
else
DB[:users].filter(uid: auth["uid"]).update(github_access_token: auth["credentials"]["token"])
end
session[:user_id] = user[:id]
redirect '/'
end
end
Jump to Line
Something went wrong with that request. Please try again.