Skip to content

Commit

Permalink
[Serverless] Success to get user info, finally! #7
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoyuki-nakabayashi committed Jul 15, 2018
1 parent c4aeec9 commit eb55b8f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
12 changes: 7 additions & 5 deletions Serverless/24-hour-video/js/user-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var userController = {
}
});

var idToken = localStorage.getItem('userToken');
var idToken = localStorage.getItem('accessToken');

if (idToken) {
this.configureAuthenticatedRequests();
Expand All @@ -47,7 +47,7 @@ var userController = {
configureAuthenticatedRequests: function() {
$.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('userToken'));
xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('accessToken'));
}
});
},
Expand All @@ -71,7 +71,8 @@ var userController = {
that.data.auth0Lock.hide();
if (error) return alert("Auth0 error:" + error);

localStorage.setItem("userToken", authResult.accessToken);
localStorage.setItem("idToken", authResult.idToken);
localStorage.setItem("accessToken", authResult.accessToken);
that.configureAuthenticatedRequests();
that.showUserAuthenticationDetails(profile);
})
Expand All @@ -97,8 +98,9 @@ var userController = {

this.uiElements.profileButton.click(function (e) {
var url = that.data.config.apiBaseUrl + '/user-profile';
$.get(url, function (data, status) {
alert(JSON.stringify(data));
$.get(url, function(data, status) {
$('#user-profile-raw-json').text(JSON.stringify(data, null, 2));
$('#user-profile-modal').modal();
})
});
}
Expand Down
53 changes: 20 additions & 33 deletions Serverless/user-profile/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
'use strict';

var jwt = require('jsonwebtoken');
var request = require('request');

exports.handler = function(event, context, callback){
if (!event.authToken) {
callback('Could not find authToken');
return;
if (!event.authToken) {
callback('Could not find authToken');
return;
}

var options = {
url: 'https://'+ process.env.DOMAIN + '/userinfo',
method: 'GET',
json: true,
headers: {
'Authorization': event.authToken
}
};

var token = event.authToken.split(' ')[1];

var secretBuffer = new Buffer(process.env.AUTH0_SECRET);
jwt.verify(token, secretBuffer, function(err, decoded){
if(err){
console.log('Failed jwt verification: ', err, 'auth: ', event.authToken);
callback('Authorization Failed');
} else {

var body = {
'id_token': token
};

var options = {
url: 'https://'+ process.env.DOMAIN + '/tokeninfo',
method: 'POST',
json: true,
body: body
};

request(options, function(error, response, body){
if (!error && response.statusCode === 200) {
callback(null, body);
} else {
callback(error);
}
});
}
})
request(options, function(error, response, body){
if (!error && response.statusCode === 200) {
console.log('Success to get user info.');
callback(null, body);
} else {
callback(error);
}
});
};

0 comments on commit eb55b8f

Please sign in to comment.