Skip to content

Commit 887fdaa

Browse files
committed
Add google user info to tutorial
1 parent 79464ca commit 887fdaa

File tree

7 files changed

+90
-5
lines changed

7 files changed

+90
-5
lines changed

Brocfile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var EmberApp = require('ember-cli/lib/broccoli/ember-app');
44

55
var app = new EmberApp();
66

7+
app.import('bower_components/jquery/dist/jquery.js');
8+
79
// Use `app.import` to add additional libraries to the generated
810
// output files.
911
//

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,24 @@ ENV['torii'] = {
120120

121121

122122
### Test & Enjoy
123-
run `ember server` and try it out!
123+
124+
run `ember server` and try it out!
125+
126+
127+
128+
### Bonus: Get user info from google
129+
130+
We are going to create a custom torii provider that will also get the users information from google.
131+
We need the custom provider because the default google-ouath2 uses the code workflow rather than the
132+
token workflow that the google plus api's need. This code extends [torii oauth2-bearer](https://github.com/Vestorly/torii/blob/master/lib/torii/providers/oauth2-bearer.js) and borrows from [torri google-oauth2](https://github.com/Vestorly/torii/blob/master/lib/torii/providers/google-oauth2.js). It also uses jQuery for the GET request, if there is a better way let me know!
133+
134+
- [Custom Provider](app/torii-providers/google-token)
135+
136+
Then update:
137+
138+
- [environment.js](config/environment.js) torii provider to match the custom providers name (this provider also uses scope `profile email`)
139+
- Your [login action](app/routes/login.js) to match the custom providers name
140+
141+
Then enable the Google+ API in your [google dev console](https://console.developers.google.com/project)
142+
143+
You now will have access to `session.content.userName` and `session.content.userEmail`

app/routes/login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Ember from 'ember';
33
export default Ember.Route.extend({
44
actions: {
55
googleLogin: function() {
6-
this.get('session').authenticate('simple-auth-authenticator:torii', 'google-oauth2');
6+
this.get('session').authenticate('simple-auth-authenticator:torii', 'google-token');
77
return;
88
}
99
}

app/templates/login.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<h3>Login Route</h3>
2-
<a {{action 'googleLogin'}}>Login with Google</a>
2+
<button {{action 'googleLogin'}}>Login with Google</button>

app/templates/protected.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<h3>Protected Route</h3>
22

3+
Hello {{session.content.userName}}! Your email is {{session.content.userEmail}}
4+
5+
<br><br>
6+
37
You shouldn't see this unless logged in
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {configurable} from 'torii/configuration';
2+
import Oauth2Bearer from 'torii/providers/oauth2-bearer';
3+
4+
var GoogleToken = Oauth2Bearer.extend({
5+
name: 'google-token',
6+
baseUrl: 'https://accounts.google.com/o/oauth2/auth',
7+
8+
// additional params that this provider requires
9+
requiredUrlParams: ['state'],
10+
optionalUrlParams: ['scope', 'request_visible_actions', 'access_type'],
11+
12+
requestVisibleActions: configurable('requestVisibleActions', ''),
13+
14+
accessType: configurable('accessType', ''),
15+
16+
responseParams: ['token'],
17+
18+
scope: configurable('scope', 'email'),
19+
20+
state: configurable('state', 'STATE'),
21+
22+
redirectUri: configurable('redirectUri',
23+
'http://localhost:8000/oauth2callback'),
24+
25+
open: function(){
26+
var name = this.get('name'),
27+
url = this.buildUrl(),
28+
redirectUri = this.get('redirectUri'),
29+
responseParams = this.get('responseParams');
30+
31+
var client_id = this.get('client_id');
32+
33+
return this.get('popup').open(url, responseParams).then(function(authData){
34+
var missingResponseParams = [];
35+
36+
responseParams.forEach(function(param){
37+
if (authData[param] === undefined) {
38+
missingResponseParams.push(param);
39+
}
40+
});
41+
42+
if (missingResponseParams.length){
43+
throw "The response from the provider is missing " +
44+
"these required response params: " + responseParams.join(', ');
45+
}
46+
47+
return $.get("https://www.googleapis.com/plus/v1/people/me", {access_token: authData.token}).then(function(user){
48+
return {
49+
userName: user.displayName,
50+
userEmail: user.emails[0].value,
51+
provider: name,
52+
redirectUri: redirectUri
53+
};
54+
});
55+
});
56+
}
57+
});
58+
59+
export default GoogleToken;

config/environment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ module.exports = function(environment) {
4545

4646
ENV['torii'] = {
4747
providers: {
48-
'google-oauth2': {
48+
'google-token': {
4949
apiKey: '',
50-
scope: 'profile',
50+
scope: 'profile email',
5151
redirectUri: 'http://localhost:4200'
5252
}
5353
}

0 commit comments

Comments
 (0)