Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rendezvous
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
VeNtura
rendezvous
Commits
fb5bf120
Commit
fb5bf120
authored
Dec 28, 2013
by
tady
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refresh oauth token
parent
a986f463
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
78 additions
and
20 deletions
+78
-20
Gemfile
+4
-0
Gemfile.lock
+10
-0
app/controllers/concerns/rv/mailer.rb
+0
-4
app/controllers/posts_controller.rb
+5
-0
app/controllers/users/omniauth_callbacks_controller.rb
+9
-9
app/models/user.rb
+36
-2
config/initializers/omniauth.rb
+2
-1
db/migrate/20131228110818_add_refresh_token_to_user.rb
+6
-0
db/schema.rb
+6
-4
No files found.
Gemfile
View file @
fb5bf120
...
...
@@ -65,6 +65,8 @@ group :development do
gem
'
thin
'
# gem '
capistrano
', '~> 3.0.1'
gem
'
pry-rails
'
end
group
:production
do
...
...
@@ -86,3 +88,5 @@ gem 'action-gmailer', github: 'popgiro/action-gmailer'
# compose html mail
gem
'
nokogiri
'
gem
'
premailer
'
gem
'
faraday
'
Gemfile.lock
View file @
fb5bf120
...
...
@@ -92,6 +92,7 @@ GEM
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
method_source (0.8.2)
mime-types (1.25.1)
mini_portile (0.5.2)
minitest (4.7.5)
...
...
@@ -121,6 +122,12 @@ GEM
premailer (1.7.9)
css_parser (>= 1.1.9)
htmlentities (>= 4.0.0)
pry (0.9.12.3)
coderay (~> 1.0)
method_source (~> 0.8)
slop (~> 3.4)
pry-rails (0.3.2)
pry (>= 0.9.10)
rack (1.5.2)
rack-mini-profiler (0.1.31)
rack (>= 1.1.3)
...
...
@@ -155,6 +162,7 @@ GEM
sdoc (0.3.20)
json (>= 1.1.3)
rdoc (~> 3.10)
slop (3.4.7)
sprockets (2.10.1)
hike (~> 1.2)
multi_json (~> 1.0)
...
...
@@ -194,12 +202,14 @@ DEPENDENCIES
coderay
coffee-rails (~> 4.0.0)
devise
faraday
jbuilder (~> 1.2)
mail
nokogiri
omniauth-google-oauth2
pg
premailer
pry-rails
rack-mini-profiler
rails (~> 4.0.2)
rails_12factor
...
...
app/controllers/concerns/rv/mailer.rb
View file @
fb5bf120
...
...
@@ -358,10 +358,6 @@ __HTML__
html_body
+=
<<
'__HTML__'
<div style="font-weight: bold; font-size: 18px; line-height: 24px; color: #D03C0F;">
Quick Two Columns to Rows Demo
</div>
<br>
</td>
...
...
app/controllers/posts_controller.rb
View file @
fb5bf120
...
...
@@ -56,8 +56,13 @@ class PostsController < ApplicationController
def
mail
@post
=
set_post
# refresh google oauth token if expired
current_user
.
google_oauth_token_refresh!
if
current_user
.
google_oauth_token_expired?
compose_mail
(
@post
,
current_user
).
deliver
redirect_to
root_path
(
id:
@post
.
id
)
rescue
ActionGmailer
::
DeliveryError
redirect_to
root_path
(
id:
@post
.
id
),
flash:
{
notice:
'Gmail authentication expired.'
}
end
# GET /posts/1/edit
...
...
app/controllers/users/omniauth_callbacks_controller.rb
View file @
fb5bf120
class
Users
::
OmniauthCallbacksController
<
Devise
::
OmniauthCallbacksController
def
google_oauth2
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user
=
User
.
find_for_google_oauth2
(
request
.
env
[
"omniauth.auth"
],
current_user
)
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user
=
User
.
find_for_google_oauth2
(
request
.
env
[
"omniauth.auth"
],
current_user
)
if
@user
.
persisted?
flash
[
:notice
]
=
I18n
.
t
"devise.omniauth_callbacks.success"
,
:kind
=>
"Google"
sign_in_and_redirect
@user
,
:event
=>
:authentication
else
session
[
"devise.google_data"
]
=
request
.
env
[
"omniauth.auth"
]
redirect_to
new_user_registration_url
end
if
@user
.
persisted?
flash
[
:notice
]
=
I18n
.
t
"devise.omniauth_callbacks.success"
,
:kind
=>
"Google"
sign_in_and_redirect
@user
,
:event
=>
:authentication
else
session
[
"devise.google_data"
]
=
request
.
env
[
"omniauth.auth"
]
redirect_to
new_user_registration_url
end
end
end
app/models/user.rb
View file @
fb5bf120
require
'faraday'
class
User
<
ActiveRecord
::
Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
...
...
@@ -10,7 +12,7 @@ class User < ActiveRecord::Base
# Device
def
self
.
find_for_google_oauth2
(
access_token
,
signed_in_resource
=
nil
)
data
=
access_token
.
info
user
=
User
.
where
(
:email
=>
data
[
"email"
]).
first
user
=
User
.
where
(
email:
data
[
"email"
]).
first
unless
user
user
=
User
.
create
(
name:
data
[
"name"
],
...
...
@@ -19,7 +21,39 @@ class User < ActiveRecord::Base
password:
Devise
.
friendly_token
[
0
,
20
]
)
end
user
.
update_attribute
(
:google_auth_token
,
access_token
.
credentials
[
'token'
])
user
.
update_attributes
(
google_auth_token:
access_token
.
credentials
[
'token'
],
google_refresh_token:
access_token
.
credentials
[
'refresh_token'
],
google_token_expires_at:
Time
.
at
(
access_token
.
credentials
[
'expires_at'
])
)
user
end
# check if google oauth token is expired
def
google_oauth_token_expired?
self
.
google_token_expires_at
<
Time
.
now
end
# refresh google oauth token
def
google_oauth_token_refresh!
conn
=
Faraday
.
new
(
url:
'https://accounts.google.com'
)
do
|
builder
|
builder
.
request
:url_encoded
builder
.
adapter
:net_http
end
response
=
conn
.
post
'/o/oauth2/token'
,
{
client_id:
ENV
[
"GOOGLE_KEY"
],
client_secret:
ENV
[
"GOOGLE_SECRET"
],
refresh_token:
self
.
google_refresh_token
,
grant_type:
"refresh_token"
}
res_json
=
JSON
.
parse
(
response
.
body
)
self
.
update_attributes
(
google_auth_token:
res_json
[
'access_token'
],
google_token_expires_at:
Time
.
now
+
res_json
[
'expires_in'
].
seconds
)
end
end
config/initializers/omniauth.rb
View file @
fb5bf120
...
...
@@ -3,7 +3,8 @@ Rails.application.config.middleware.use OmniAuth::Builder do
{
:name
=>
"google_oauth2"
,
:scope
=>
"https://mail.google.com/, userinfo.email, userinfo.profile"
,
:prompt
=>
"select_account"
,
access_type:
'offline'
,
:prompt
=>
"select_account consent"
,
:image_aspect_ratio
=>
"square"
,
:image_size
=>
50
}
...
...
db/migrate/20131228110818_add_refresh_token_to_user.rb
0 → 100644
View file @
fb5bf120
class
AddRefreshTokenToUser
<
ActiveRecord
::
Migration
def
change
add_column
:users
,
:google_refresh_token
,
:string
add_column
:users
,
:google_token_expires_at
,
:datetime
end
end
db/schema.rb
View file @
fb5bf120
...
...
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2013122
6161253
)
do
ActiveRecord
::
Schema
.
define
(
version:
2013122
8110818
)
do
create_table
"post_tags"
,
force:
true
do
|
t
|
t
.
integer
"post_id"
,
null:
false
...
...
@@ -45,17 +45,19 @@ ActiveRecord::Schema.define(version: 20131226161253) do
t
.
string
"image_url"
t
.
datetime
"created_at"
t
.
datetime
"updated_at"
t
.
string
"email"
,
default:
""
,
null:
false
t
.
string
"encrypted_password"
,
default:
""
,
null:
false
t
.
string
"email"
,
default:
""
,
null:
false
t
.
string
"encrypted_password"
,
default:
""
,
null:
false
t
.
string
"reset_password_token"
t
.
datetime
"reset_password_sent_at"
t
.
datetime
"remember_created_at"
t
.
integer
"sign_in_count"
,
default:
0
,
null:
false
t
.
integer
"sign_in_count"
,
default:
0
,
null:
false
t
.
datetime
"current_sign_in_at"
t
.
datetime
"last_sign_in_at"
t
.
string
"current_sign_in_ip"
t
.
string
"last_sign_in_ip"
t
.
string
"google_auth_token"
t
.
string
"google_refresh_token"
t
.
datetime
"google_token_expires_at"
end
add_index
"users"
,
[
"email"
],
name:
"index_users_on_email"
,
unique:
true
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment