Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Add token refresh flow. #369

Closed
wants to merge 15 commits into from
30 changes: 24 additions & 6 deletions app/modules/auth/auth.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,36 @@ require('./auth.module.js')
var OAuth = require('panoptes-client/lib/oauth');

// @ngInject
function authFactory($interval, $timeout, $location, $window, localStorageService, ModalsFactory, zooAPI, zooAPIConfig, CribsheetFactory, $rootScope) {
function authFactory($location, $rootScope, $state, $transitions, $window, AnnotationsFactory, zooAPI, CribsheetFactory) {

var factory;

var _user = {};

OAuth.checkCurrent()
.then(function (user) {
if (user) {
_setUserData();
}
});
.then(function (user) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to keep these whitespace changes only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the indenting at 4 spaces?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it was like that when I started working on this app and it was too late when I realised I could change it. It annoys me every time I work on it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok - this PR has the change in whitespace here it seems unrelated and that's why i'm confused by it. It's not blocking for sure, just this PR is adding spacing to a function for some reason...

if (user) {
_setUserData();
}
})
.then(function() {
zooAPI.beforeEveryRequest = function() {
return OAuth.checkBearerToken()
.then(function (token) {
// The Panoptes client doesn't return an error but just null when it can't refresh the token.
// So we check for null, instead of using a catch block.
if (_user.id && token === null) {
// We're logged in but don't have a token any more.
alert('Your session is expired. Press OK to save your work and start a new one.')
// Save any unsaved work and redirect to Panoptes for a new token.
AnnotationsFactory.updateCache();
OAuth.signIn($location.absUrl());
// Abort ui-router state transition
return Promise.reject(new Error('ui-router transition aborted'));
}
})
}
});

factory = {
signIn: signIn,
Expand Down
5 changes: 1 addition & 4 deletions app/modules/transcribe/subjects.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ function SubjectsFactory($q, AnnotationsFactory, localStorageService, zooAPI, zo
return zooAPI.type('workflows').get(zooAPIConfig.workflow_id)
.then(function(wf) {
var randomSet = _.sample(wf.links.subject_sets)
return randomSet
})
.catch(function(error) {
console.log('Error fetching active subject sets', error)
return randomSet;
})
}

Expand Down
13 changes: 6 additions & 7 deletions app/modules/transcribe/transcribe.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ function TranscribeController($stateParams, $modal, $scope, $window, Annotations

function loadSubject() {
return SubjectsFactory.$getData($stateParams.subjectSet)
.then(subjectLoaded, subjectLoadError)
.then(loadAggregations);
.then(subjectLoaded)
.then(loadAggregations)
.catch(handleErrors);
}

function openTutorial() {
Expand All @@ -93,12 +94,10 @@ function TranscribeController($stateParams, $modal, $scope, $window, Annotations
vm.subject = SubjectsFactory.current;
}

function subjectLoadError(result) {
if (result === 'outOfData') {
function handleErrors(error) {
if (error === 'outOfData') {
$scope.$broadcast('subject:outOfData');
} else {
console.error('Error loading subject', result);
}
console.log(error);
}

}
2 changes: 1 addition & 1 deletion app/modules/zoo-api/zooapi.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require('./zooapi.module.js')
var ApiClient = require('panoptes-client/lib/api-client');

// @ngInject
function zooAPI(zooAPIConfig) {
function zooAPI() {

// There's only a version of this project on production, so rather than
// defer to the client we manually override the API root.
Expand Down
4 changes: 3 additions & 1 deletion app/modules/zoo-api/zooapi.module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict';

module.exports = require('angular')
.module('app.zooapi', []);
.module('app.zooapi', [
'app.transcribe.annotations'
]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also need to be reverted if token check is done in AuthFactory.

2 changes: 1 addition & 1 deletion gulp/tasks/browserify.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function buildScript(file) {
.pipe(gulpif(createSourcemap, sourcemaps.init()))
.pipe(gulpif(global.isProd, streamify(
uglify({
compress: { drop_console: true }
compress: { } // leave console.log in prod, for testing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should be reverted before this is merged to master.

})
.on('error', handleErrors)
)))
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"nib": "^1.1.0",
"node-dir": "0.1.16",
"npm": "^5.3.0",
"panoptes-client": "^2.9.4",
"panoptes-client": "^2.9.6",
"pretty-hrtime": "^1.0.3",
"q": "^1.2.0",
"run-sequence": "^2.1.0",
Expand Down