1
0
Fork 0
mirror of https://github.com/omniauth/omniauth.git synced 2022-11-09 12:31:49 -05:00

Merge pull request #539 from jjb/patch-1

pretty syntax highlighting, and removed an errant newline
This commit is contained in:
Michael Bleigh 2011-11-10 05:24:37 -08:00
commit 1e67857034

View file

@ -31,13 +31,15 @@ it the same way that you use any other Rack middleware. For example, to
use the built-in Developer strategy in a Sinatra application I might do use the built-in Developer strategy in a Sinatra application I might do
this: this:
require 'sinatra' ```ruby
require 'omniauth' require 'sinatra'
require 'omniauth'
class MyApplication < Sinatra::Base class MyApplication < Sinatra::Base
use Rack::Session use Rack::Session
use OmniAuth::Strategies::Developer use OmniAuth::Strategies::Developer
end end
```
Because OmniAuth is built for *multi-provider* authentication, I may Because OmniAuth is built for *multi-provider* authentication, I may
want to leave room to run multiple strategies. For this, the built-in want to leave room to run multiple strategies. For this, the built-in
@ -47,10 +49,12 @@ code and using each strategy individually as middleware. This is an
example that you might put into a Rails initializer at example that you might put into a Rails initializer at
`config/initializers/omniauth.rb`: `config/initializers/omniauth.rb`:
Rails.application.config.middleware.use OmniAuth::Builder do ```ruby
provider :developer unless Rails.env.production? Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET'] provider :developer unless Rails.env.production?
end provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
end
```
You should look to the documentation for each provider you use for You should look to the documentation for each provider you use for
specific initialization requirements. specific initialization requirements.
@ -78,24 +82,28 @@ application that matches to the callback URL and then performs whatever
steps are necessary for your application. For example, in a Rails app I steps are necessary for your application. For example, in a Rails app I
would add a line in my `routes.rb` file like this: would add a line in my `routes.rb` file like this:
match '/auth/:provider/callback', to: 'sessions#create' ```ruby
match '/auth/:provider/callback', to: 'sessions#create'
```
And I might then have a `SessionsController` with code that looks And I might then have a `SessionsController` with code that looks
something like this: something like this:
class SessionsController < ApplicationController ```ruby
def create class SessionsController < ApplicationController
@user = User.find_or_create_from_auth_hash(auth_hash) def create
self.current_user = @user @user = User.find_or_create_from_auth_hash(auth_hash)
redirect_to '/' self.current_user = @user
end redirect_to '/'
end
protected protected
def auth_hash def auth_hash
request.env['omniauth.auth'] request.env['omniauth.auth']
end end
end end
```
The `omniauth.auth` key in the environment hash gives me my The `omniauth.auth` key in the environment hash gives me my
Authentication Hash which will contain information about the just Authentication Hash which will contain information about the just
@ -121,8 +129,7 @@ OmniAuth, how it works, and how to use it.
OmniAuth is tested under 1.8.7, 1.9.2, 1.9.3, JRuby, Rubinius (1.8 OmniAuth is tested under 1.8.7, 1.9.2, 1.9.3, JRuby, Rubinius (1.8
mode), and Ruby Enterprise Edition. mode), and Ruby Enterprise Edition.
[![CI Build [![CI Build Status](https://secure.travis-ci.org/intridea/omniauth.png)](http://travis-ci.org/intridea/omniauth)
Status](https://secure.travis-ci.org/intridea/omniauth.png)](http://travis-ci.org/intridea/omniauth)
## License ## License