Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
fix(app): handle stale tokens
Browse files Browse the repository at this point in the history
- check /auth/user every route transition
- display 'session expired' notification on expiry
Closes #23
  • Loading branch information
Christine Yu committed Mar 31, 2016
1 parent 93db2b9 commit 657e11c
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/scripts/annotations/tests/annotations.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe('Annotations:', function () {
beforeEach(module('ngApp.annotations', 'core.services', 'ngProgressLite'));
beforeEach(module(function ($provide) {
$provide.value('AuthRestangular', {});
$provide.value('notify', {});
$provide.value('config', {});
}));

Expand Down
2 changes: 0 additions & 2 deletions app/scripts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ function appRun(gettextCatalog: any,
});
});

UserService.login();

ProjectsService.getProjects({
size: 100
})
Expand Down
23 changes: 12 additions & 11 deletions app/scripts/components/user/user.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module ngApp.components.user.services {
import IFile = ngApp.files.models.IFile;
import ILocationService = ngApp.components.location.ILocationService;
import IGDCConfig = ngApp.IGDCConfig;
import INotifyService = ng.cgNotify.INotifyService;

export interface IUserService {
login(): void;
Expand All @@ -26,6 +27,7 @@ module ngApp.components.user.services {
private $cookies: ng.cookies.ICookiesService,
private $window: ng.IWindowService,
private $uibModal: any,
private notify: INotifyService,
private config: IGDCConfig,
private $log: ng.ILogService) {
if (config.fake_auth) {
Expand Down Expand Up @@ -56,7 +58,15 @@ module ngApp.components.user.services {
this.setUser(data);
}, (response) => {
if(response.status === 401) {
return;
if (this.currentUser) {
this.currentUser = undefined;
this.notify({
message: "",
messageTemplate: "<span data-translate>Session expired or unauthorized.</span>",
container: "#notification",
classes: "alert-warning"
});
}
} else {
this.$log.error("Error logging in, response status " + response.status);
}
Expand All @@ -80,16 +90,7 @@ module ngApp.components.user.services {
this.$window.saveAs(file.data, "gdc-user-token." + this.$window.moment().format() + ".txt");
}, (response) => {
if(response.status === 401) {
var loginWarningModal = this.$uibModal.open({
templateUrl: "core/templates/request-access-to-download-single.html",
controller: "LoginToDownloadController",
controllerAs: "wc",
backdrop: "static",
keyboard: false,
backdropClass: "warning-backdrop",
animation: false,
size: "lg"
});
this.login();
} else {
this.$log.error("Error logging in, response status " + response.status);
}
Expand Down
6 changes: 6 additions & 0 deletions app/scripts/core/core.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ngApp.core.controllers {
import ICartService = ngApp.cart.services.ICartService;
import INotifyService = ng.cgNotify.INotifyService;
import IUserService = ngApp.components.user.services.IUserService;

export interface ICoreController {
showWarning: boolean;
Expand All @@ -16,8 +17,13 @@ module ngApp.core.controllers {
private notify: INotifyService,
$location: ng.ILocationService,
private $cookies: ng.cookies.ICookiesService,
UserService: IUserService,
private $uibModal: any) {

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams, options) {
UserService.login();
});

// display login failed warning
if(_.get($location.search(), 'error') === 'You are not authorized to gdc services') {
var loginWarningModal = this.$uibModal.open({
Expand Down
1 change: 1 addition & 0 deletions app/scripts/files/tests/files.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('Files:', function () {

beforeEach(module(function ($provide) {
$provide.value('RestFullResponse', {});
$provide.value('notify', {});
}));

beforeEach(module(function ($provide) {
Expand Down
1 change: 1 addition & 0 deletions app/scripts/participant/tests/participants.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('Participants:', function () {

beforeEach(module(function ($provide) {
$provide.value('AuthRestangular', {});
$provide.value('notify', {});
$provide.value('config', {});
}));

Expand Down
1 change: 1 addition & 0 deletions app/scripts/projects/tests/projects.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('Projects:', function () {

beforeEach(module(function ($provide) {
$provide.value('AuthRestangular', {});
$provide.value('notify', {});
$provide.value('config', {});
}));

Expand Down

0 comments on commit 657e11c

Please sign in to comment.