Skip to content

Commit

Permalink
added handling when token expires - ported from aes plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
takeit committed Apr 1, 2015
1 parent 6a1366e commit 7b6ad09
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 16 deletions.
Expand Up @@ -46,6 +46,7 @@ public function indexAction(Request $request, $articleNumber = null, $language =

return $this->render('NewscoopNewscoopBundle:Playlists:index.html.twig', array(
'clientId' => $client ? $client->getPublicId() : '',
'redirectUris' => $client->getRedirectUrisString(),
'editorView' => $editorView,
'articleNumber' => $articleNumber,
'language' => $language,
Expand Down
Expand Up @@ -9,8 +9,7 @@
angular.module('playlistsApp').factory('authInterceptor', [
'$injector',
'$q',
'$window',
function ($injector, $q, $window) {
function ($injector, $q) {
// NOTE: userAuth service is not injected directly, because it depends
// on the $http service and the latter's provider uses this
// authInterceptor service --> circular dependency.
Expand Down Expand Up @@ -64,8 +63,8 @@ angular.module('playlistsApp').factory('authInterceptor', [
// obtain a new token and then repeat the failed request.
failedRequestConfig = response.config;
retryDeferred = $q.defer();
$window.sessionStorage.setItem('token', '');
userAuth.newToken()

userAuth.newTokenByLoginModal()
.then(function () {
// new token successfully obtained, repeat the request
$http = $injector.get('$http');
Expand Down
@@ -1,16 +1,59 @@
(function () {
'use strict';

/**
* Constructor function for the login modal controller
*
* @class ModalLoginCtrl
*/
function ModalLoginCtrl($modalInstance) {
var self = this,
tokenRegex = new RegExp('access_token=(\\w+)');

// On successful login, Newscoop login form redirects user to some
// redirect URL and that URL contains the new authentication token.
// Upon redirect, the iframe in modal body is reloaded and we catch
// its onLoad event, giving us a chance to extract new token from URL.

/**
* Updates article's workflow status on the server.
*
* @method iframeLoadedHandler
* @param location {Object} window.location object of the page
* loaded in the modal's iframe
*/
self.iframeLoadedHandler = function (location) {
var matches,
token;

if (typeof location.hash !== 'string') {
return;
}

matches = tokenRegex.exec(location.hash);

if (matches !== null) {
token = matches[1];
$modalInstance.close(token);
}
// if token is not found (perhaps due to the failed login),
// nothing happens and the modal stays open
};
}

ModalLoginCtrl.$inject = ['$modalInstance'];

/**
* A service for managing user authentication.
*
* @class userAuth
*/
angular.module('playlistsApp').service('userAuth', [
'$http',
'$modal',
'$q',
'$window',
function ($http, $q, $window) {
function ($http, $modal, $q, $window) {
var self = this;

/**
Expand All @@ -35,24 +78,32 @@
};

/**
* It makes a request and on success it
* Opens a modal with Newscoop login form. On successful login it
* stores the new authentication token into session storage and
* resolves given promise with it.
*
* @method newToken
* @method newTokenByLoginModal
* @return {Object} promise object
*/
self.newToken = function () {
var deferred = $q.defer();

$http.get(Routing.generate("newscoop_gimme_users_getuseraccesstoken", {
clientId: clientId
}))
.success(function (response) {
$window.sessionStorage.setItem('token', response.access_token);
deferred.resolve(response.access_token);
self.newTokenByLoginModal = function () {
var deferred = $q.defer(),
dialog;

dialog = $modal.open({
templateUrl: '../../bundles/newscoopnewscoop/views/modal-login.html',
controller: ModalLoginCtrl,
controllerAs: 'ctrl',
windowClass: 'modalLogin',
backdrop: 'static'
});

dialog.result.then(function (token) {
$window.sessionStorage.setItem('token', token);
flashMessage(Translator.trans('Successfully refreshed authentication token.', {}, 'messages'));
deferred.resolve(token);
})
.catch(function (reason) {
flashMessage(Translator.trans('Failed to refresh authentication token.', {}, 'messages'), 'error');
deferred.reject(reason);
});

Expand Down
Expand Up @@ -285,3 +285,5 @@ unpublished: unpublished
nopermissions: "You don't have permissions to access this resource."
importantmessage: "Important Info"
anonymous: "Anonymous"
'Successfully refreshed authentication token.': 'Successfully refreshed authentication token.'
'Failed to refresh authentication token.': 'Failed to refresh authentication token.'
Expand Up @@ -320,6 +320,7 @@

<script type="text/javascript">
var clientId = '{{ clientId }}';
var redirectUris = '{{ redirectUris }}';
$(document).ready(function() {
$('.input-filter').attr('placeholder', "{{'Search'|trans({}, 'messages') }}...");
});
Expand All @@ -339,6 +340,7 @@
<script src="{{ asset('/bundles/newscoopnewscoop/js/playlists/controllers/featured-articles.js') }}"></script>
<script src="{{ asset('/bundles/newscoopnewscoop/js/playlists/controllers/articles-filters.js') }}"></script>
<script src="{{ asset('/bundles/newscoopnewscoop/js/playlists/directives/loading-container.js') }}"></script>
<script src="{{ asset('/bundles/newscoopnewscoop/js/playlists/directives/sf-iframe.js') }}"></script>
<script src="{{ asset('/bundles/newscoopnewscoop/js/playlists/filters/lists-search.js') }}"></script>
<script src="{{ asset('/bundles/newscoopnewscoop/js/playlists/services/modal-factory.js') }}"></script>
<script src="{{ asset('/bundles/newscoopnewscoop/js/playlists/services/user-auth.js') }}"></script>
Expand Down

0 comments on commit 7b6ad09

Please sign in to comment.