Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Omniauth done

  • Loading branch information...
commit 22821aa9b98c3226a02d8b64e3f1e43ce35c3637 1 parent 9a2b718
@prakhar1989 authored
View
3  Gemfile
@@ -6,3 +6,6 @@ gem "rack-flash3"
gem 'bson_ext'
gem 'bcrypt-ruby'
gem "pry"
+gem 'omniauth'
+gem 'omniauth-facebook'
+gem 'omniauth-twitter'
View
42 Gemfile.lock
@@ -6,8 +6,44 @@ GEM
bson (1.6.2)
bson_ext (1.6.2)
bson (~> 1.6.2)
+ coderay (1.0.6)
+ faraday (0.8.0)
+ multipart-post (~> 1.1)
+ hashie (1.2.0)
+ httpauth (0.1)
+ method_source (0.7.1)
mongo (1.6.2)
bson (~> 1.6.2)
+ multi_json (1.3.5)
+ multipart-post (1.1.5)
+ oauth (0.4.6)
+ oauth2 (0.6.1)
+ faraday (~> 0.7)
+ httpauth (~> 0.1)
+ multi_json (~> 1.3)
+ omniauth (1.1.0)
+ hashie (~> 1.2)
+ rack
+ omniauth-facebook (1.3.0)
+ omniauth-oauth2 (~> 1.0.2)
+ omniauth-oauth (1.0.1)
+ oauth
+ omniauth (~> 1.0)
+ omniauth-oauth2 (1.0.2)
+ oauth2 (~> 0.6.0)
+ omniauth (~> 1.0)
+ omniauth-twitter (0.0.11)
+ multi_json (~> 1.3)
+ omniauth-oauth (~> 1.0)
+ pry (0.9.9.6)
+ coderay (~> 1.0.5)
+ method_source (~> 0.7.1)
+ slop (>= 2.4.4, < 3)
+ pry (0.9.9.6-x86-mingw32)
+ coderay (~> 1.0.5)
+ method_source (~> 0.7.1)
+ slop (>= 2.4.4, < 3)
+ win32console (~> 1.3)
rack (1.4.1)
rack-flash3 (1.0.1)
rack
@@ -21,7 +57,9 @@ GEM
sinatra-mongo (0.1.0)
mongo
sinatra (>= 0.9.4)
+ slop (2.4.4)
tilt (1.3.3)
+ win32console (1.3.2-x86-mingw32)
PLATFORMS
ruby
@@ -31,6 +69,10 @@ DEPENDENCIES
bcrypt-ruby
bson_ext
mongo
+ omniauth
+ omniauth-facebook
+ omniauth-twitter
+ pry
rack-flash3
sinatra
sinatra-mongo
View
32 app.rb
@@ -0,0 +1,32 @@
+require 'omniauth'
+require 'omniauth-twitter'
+require 'omniauth-facebook'
+require 'sinatra'
+
+use Rack::Session::Cookie
+use OmniAuth::Strategies::Twitter, 'loCcf78CNJieKohWf5KNCg', 'iPk4PAUEvQ1OPbsJhdei8U52NlhA5Z8N7Mc4DBgV79Q'
+use OmniAuth::Builder do
+ provider :facebook, '351336024898423', '426e74cac68fe2a9182ffe7db3744399'
+end
+
+get '/' do
+ <<-HTML
+<a href='/auth/twitter'>Sign in with Twitter</a>
+<a href='/auth/facebook'>Sign in with facebook</a>
+ HTML
+end
+
+post '/auth/:name/callback' do
+ auth = request.env['omniauth.auth']
+ # do whatever you want with the information!
+end
+
+get '/auth/:name/callback' do
+ content_type 'application/json'
+ auth = request.env['omniauth.auth']
+ MultiJson.dump(auth, :pretty=>true)
+end
+
+get '/auth/failure' do
+ "Oops! GG!"
+end
View
27 lib/tryshit.rb
@@ -1,5 +1,24 @@
-require 'bcrypt'
-pw_hash = BCrypt::Password.create("prakhar")
+require 'sinatra'
+require 'omniauth'
+
+use Rack::Session::Cookie
+use OmniAuth::Builder do
+ provider :twitter, 'loCcf78CNJieKohWf5KNCg', 'iPk4PAUEvQ1OPbsJhdei8U52NlhA5Z8N7Mc4DBgV79Q'
+end
+
+get '/' do
+ <<-HTML
+<a href='/auth/twitter'>Sign in with Twitter</a>
+
+<form action='/auth/open_id' method='post'>
+ <input type='text' name='identifier'/>
+ <input type='submit' value='Sign in with OpenID'/>
+</form>
+ HTML
+end
+
+post '/auth/:name/callback' do
+ auth = request.env['omniauth.auth']
+ # do whatever you want with the information!
+end
-puts pw_hash == "wassap"
-puts pw_hash == "prakhar"
View
8 lib/validate.rb
@@ -23,11 +23,11 @@ def authorize_user(user,session)
def logged_in?(session)
session[:authorize]
end
- def register_user(email,password)
- pw_hash = BCrypt::Password.create(password)
- mongo["users"].insert({"email"=>email,"password_hash"=>pw_hash})
+ def register_user(email,password,provider)
+ pw_hash = BCrypt::Password.create(password)
+ mongo["users"].insert({"email"=>email,"password_hash"=>pw_hash, "provider"=>provider})
end
end
register Validate
-end
+end
View
26 main.rb
@@ -2,12 +2,19 @@
require 'sinatra/mongo'
require 'rack-flash'
require './lib/validate'
+require 'omniauth'
+require 'omniauth-twitter'
+require 'omniauth-facebook'
configure do
set :public_folder, File.dirname(__FILE__) + '/static'
set :mongo, 'mongo://localhost:27017/blogera'
enable :sessions
use Rack::Flash, :sweep => true
+ use OmniAuth::Strategies::Twitter, 'loCcf78CNJieKohWf5KNCg', 'iPk4PAUEvQ1OPbsJhdei8U52NlhA5Z8N7Mc4DBgV79Q'
+ use OmniAuth::Builder do
+ provider :facebook, '351336024898423', '426e74cac68fe2a9182ffe7db3744399'
+ end
end
get '/' do
@@ -17,7 +24,8 @@
post '/signup' do
if validate_email(params[:email]) and validate_password(params[:password])
- register_user(params[:email],params[:password])
+ provider = "standard"
+ register_user(params[:email],params[:password], provider)
authorize_user(params[:email],session)
flash[:success] = "You have successfully registered!"
redirect('/home')
@@ -55,3 +63,19 @@
end
erb :home, :locals => { :session => session }
end
+
+get '/auth/:name/callback' do
+ content_type 'application/json'
+ auth = request.env['omniauth.auth']
+ uid = auth["uid"] #TODO: Is it fine to use UID as password?
+ nickname = auth["info"]["nickname"]
+ provider = auth["provider"]
+ register_user(nickname, uid, provider)
+ authorize_user(nickname, session)
+ redirect('/home')
+end
+
+get '/auth/failure' do
+ flash[:error] = "We encountered an error while fetching your data from twitter. Please try again!"
+ redirect('/')
+end
View
BIN  static/lib/icons/facebook_32.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  static/lib/icons/twitter_32.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
5 views/signup.erb
@@ -1,10 +1,11 @@
<div class="hero-unit">
<h1>Blogera!</h1>
- <p>Write your blogs by sending emails to our postman Blogera
- Sign up below and start posting!</p>
+ <p>Write your blogs by sending emails to our postman Blogera</p>
<p><a class="btn btn-primary btn-large">Learn more &raquo;</a></p>
</div>
+<h5>Sign up with Blogera by filling the form below or connect with <a href="/auth/twitter"><img src="lib/icons/twitter_32.png" /></a>
+or <a href="/auth/facebook"><img src="lib/icons/facebook_32.png" /></a> </h5>
<form class="well form-inline" action="/signup" method="POST">
<input name="email" type="text" class="input-large" placeholder="Email">
<input name="password" type="password" class="input-large" placeholder="Password">
Please sign in to comment.
Something went wrong with that request. Please try again.