Created
March 15, 2012 00:29
-
-
Save sethvincent/2040699 to your computer and use it in GitHub Desktop.
sinatra, omniauth, and datamapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%w(rubygems oa-oauth dm-core dm-sqlite-adapter dm-migrations sinatra).each { |dependency| require dependency } | |
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/database.db") | |
class User | |
include DataMapper::Resource | |
property :id, Serial | |
property :uid, String | |
property :name, String | |
property :nickname, String | |
property :created_at, DateTime | |
end | |
DataMapper.finalize | |
DataMapper.auto_upgrade! | |
# You'll need to customize the following line. Replace the CONSUMER_KEY | |
# and CONSUMER_SECRET with the values you got from Twitter | |
# (https://dev.twitter.com/apps/new). | |
use OmniAuth::Strategies::Twitter, 'Kp9THFHyOduYm3p8BrLQQ', 'f3kkG47FzMCvh45ee5ddBFwHW7QSzfb479z0JTehrR4' | |
enable :sessions | |
helpers do | |
def current_user | |
@current_user ||= User.get(session[:user_id]) if session[:user_id] | |
end | |
end | |
get '/' do | |
if current_user | |
# The following line just tests to see that it's working. | |
# If you've logged in your first user, '/' should load: "1 ... 1"; | |
# You can then remove the following line, start using view templates, etc. | |
current_user.id.to_s + " ... " + session[:user_id].to_s | |
'<h1>THIS SHIT IS FUCKING WORKING!!!!</h1>' | |
else | |
'<a href="/sign_up">create an account</a> or <a href="/sign_in">sign in with Twitter</a>' | |
# if you replace the above line with the following line, | |
# the user gets signed in automatically. Could be useful. | |
# Could also break user expectations. | |
# redirect '/auth/twitter' | |
end | |
end | |
#get '/auth/:provider/callback' do | |
# auth = request.env['omniauth.auth'] | |
# #"Hello, #{auth['user_info']['name']}, you logged in via #{params['provider']}." | |
# redirect '/' | |
#end | |
get '/auth/:provider/callback' do | |
auth = request.env["omniauth.auth"] | |
user = User.first_or_create({ :uid => auth["uid"]}, { | |
:uid => auth["user_info"]["uid"], | |
:nickname => auth["user_info"]["nickname"], | |
:name => auth["user_info"]["name"], | |
:created_at => Time.now }) | |
session[:user_id] = user.id | |
redirect '/' | |
end | |
# any of the following routes should work to sign the user in: | |
# /sign_up, /signup, /sign_in, /signin, /log_in, /login | |
["/sign_in/?", "/signin/?", "/log_in/?", "/login/?", "/sign_up/?", "/signup/?"].each do |path| | |
get path do | |
redirect '/auth/twitter' | |
end | |
end | |
# either /log_out, /logout, /sign_out, or /signout will end the session and log the user out | |
["/sign_out/?", "/signout/?", "/log_out/?", "/logout/?"].each do |path| | |
get path do | |
session[:user_id] = nil | |
redirect '/' | |
end | |
end | |
get '/auth/failure' do | |
"Fail World" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'foreman' | |
gem 'oa-oauth' | |
gem 'dm-core' | |
gem 'dm-sqlite-adapter' | |
gem 'dm-migrations' | |
gem 'rack', "~> 1.3.6" | |
gem 'sinatra' | |
gem 'thin' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment