Skip to content

Commit b497860

Browse files
committed
Update README.md: Add a working example
1 parent 45f2fc7 commit b497860

File tree

1 file changed

+136
-12
lines changed

1 file changed

+136
-12
lines changed

README.md

Lines changed: 136 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,150 @@ on the [GitHub Applications Page](https://github.com/settings/applications).
66

77
## Basic Usage
88

9-
use OmniAuth::Builder do
10-
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']
9+
Add to your `Gemfile`:
10+
```
11+
gem 'omniauth-github'
12+
```
13+
14+
Then `bundle install`
15+
16+
Create an initializer `config/initializers/omniauth.rb`:
17+
```
18+
Rails.application.config.middleware.use OmniAuth::Builder do
19+
provider :github, ENV['GITHUB_CLIENT_ID'], ENV['GITHUB_CLIENT_SECRET']
20+
end
21+
```
22+
## Github Callback
23+
24+
When a user on your site visits `/auth/github` and will be redirected to Github and asked to verify permissions. When they accept, they will be sent back to your site based on the callback setting for the application you setup. [https://developer.github.com/v3/oauth/](Read Github OAuth documentation)
25+
26+
When the user is redirected back to your site, the callback URL will have the following variables available:
27+
The response to Rails in `env.omniauth`:
28+
29+
`env.omniauth # => [:uid, :provider, info: :name, credentials: :token]`
30+
31+
## Example
32+
33+
Setup your Rails app and signup for a OAuth Application as described above.
34+
35+
### 1. Generate a new controller:
36+
37+
```
38+
rails g controller Sessions
39+
```
40+
41+
### 2. Generate a user model:
42+
43+
```
44+
rails g model user provider uid name oauth_token oauth_expires_at:datetime
45+
rake db:migrate
46+
```
47+
48+
### 3. Add to your `Gemfile`:
49+
50+
```
51+
gem 'omniauth-github'
52+
Then `bundle install`
53+
```
54+
55+
56+
### 4. Create an initializer `config/initializers/omniauth.rb`:
57+
58+
```
59+
Rails.application.config.middleware.use OmniAuth::Builder do
60+
provider :github, ENV['GITHUB_CLIENT_ID'], ENV['GITHUB_CLIENT_SECRET']
61+
end
62+
```
63+
64+
### 5. Add these routes to `config/routes.rb`:
65+
66+
```
67+
get 'auth/:provider/callback', to: 'sessions#create'
68+
get 'auth/failure', to: redirect('/')
69+
get 'signout', to: 'sessions#destroy', as: 'signout'
70+
71+
resources :sessions, only: [:create, :destroy]
72+
```
73+
74+
### 6. Add create and destroy actions to SessionsController:
75+
76+
```
77+
def create
78+
user = User.from_omniauth(env["omniauth.auth"])
79+
session[:user_id] = user.id
80+
redirect_to root_path
81+
end
82+
83+
def destroy
84+
session[:user_id] = nil
85+
redirect_to root_path
86+
end
87+
```
88+
89+
### 7. Add a class method to User:
90+
91+
```
92+
class User < ActiveRecord::Base
93+
94+
def self.from_omniauth(auth)
95+
where(provider: auth[:provider], uid: auth[:uid]).first_or_initialize.tap do |user|
96+
user.provider = auth.provider
97+
user.uid = auth.uid
98+
user.name = auth.info.name
99+
user.oauth_token = auth.credentials.token
100+
user.oauth_expires_at = Time.at(auth.credentials.expires_at) if auth.credentials.try(:expires_at)
101+
user.save!
11102
end
103+
end
104+
end
105+
```
12106

13-
## Github Enterprise Usage
107+
### 8. Add a `current_user` method to ApplicationController:
108+
109+
```
110+
class ApplicationController < ActionController::Base
111+
112+
helper_method :current_user
113+
114+
def current_user
115+
@current_user ||= User.find(session[:user_id]) if session[:user_id]
116+
end
117+
end
118+
```
14119

15-
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'],
16-
{
17-
:client_options => {
18-
:site => 'https://github.YOURDOMAIN.com/api/v3',
19-
:authorize_url => 'https://github.YOURDOMAIN.com/login/oauth/authorize',
20-
:token_url => 'https://github.YOURDOMAIN.com/login/oauth/access_token',
21-
}
22-
}
120+
### 9. Add link to signin in application layout:
121+
122+
```
123+
<div>
124+
<% if current_user %>
125+
Signed in as <strong><%= current_user.name %></strong>!
126+
<%= link_to "Sign out", signout_path, id: "sign_out" %>
127+
<% else %>
128+
<%= link_to "Sign in with Github", "/auth/github", id: "sign_in" %>
129+
<% end %>
130+
</div>
131+
```
132+
133+
This example based on the article by RichOnRails, [Google authentication in Ruby on Rails](http://richonrails.com/articles/google-authentication-in-ruby-on-rails)
134+
135+
## Github Enterprise Usage
136+
```
137+
provider :github, ENV['GITHUB_CLIENT_ID'], ENV['GITHUB_CLIENT_SECRET'],
138+
{
139+
:client_options => {
140+
:site => 'https://github.YOURDOMAIN.com/api/v3',
141+
:site => 'https://github.YOURDOMAIN.com/',
142+
:authorize_url => 'https://github.YOURDOMAIN.com/login/oauth/authorize',
143+
:token_url => 'https://github.YOURDOMAIN.com/login/oauth/access_token',
144+
}
145+
}
146+
```
23147

24148
## Scopes
25149

26150
GitHub API v3 lets you set scopes to provide granular access to different types of data:
27151

28-
use OmniAuth::Builder do
152+
use OmniAuth::Builder do
29153
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'], scope: "user,repo,gist"
30154
end
31155

0 commit comments

Comments
 (0)