-
Notifications
You must be signed in to change notification settings - Fork 5
Add token refresh flow. #369
Changes from 5 commits
4e08716
0a603ee
03e6cec
42d06c9
2caa387
29b0a21
d770466
834a8da
f3c4e28
3ca3b8c
090cb17
b156158
2a6d6b6
0f925ad
9469307
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,20 @@ require('./zooapi.module.js') | |
.factory('zooAPI', zooAPI); | ||
|
||
var ApiClient = require('panoptes-client/lib/api-client'); | ||
|
||
var oauth = require('panoptes-client/lib/oauth') | ||
// @ngInject | ||
function zooAPI(zooAPIConfig) { | ||
|
||
function zooAPI(AnnotationsFactory, $location) { | ||
ApiClient.beforeEveryRequest = function() { | ||
return oauth.checkBearerToken() | ||
.then(function (token) { | ||
console.log('Token refreshed: ', token); | ||
}) | ||
.catch(function (error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know what's best practice with promises, but couldn't we reject the promise and throw an error when it resolves to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't that break zooniverse/anti-slavery-manuscripts#251? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way, that function does reject with an error if no token is found. https://github.com/zooniverse/panoptes-javascript-client/blob/f26d810767ce210bc8b773efab431f46991a8d72/lib/oauth.js#L204-L208 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Errors are caught, and null is returned, here: https://github.com/zooniverse/panoptes-javascript-client/blob/595e694c71dd877ddbae4a13e670fc466314258e/lib/oauth.js#L279-L284 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. re: When a login token could not be refreshed when it's supposed to, should it reject a promise or return null (as it does in PJC 2.9.2 onwards)? I'm not sure what the best practice is either, since I haven't worked with many other login/session libraries - but whatever it is, we'll need to document it clearly. Thoughts:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eatyourgreens do you know why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because that's a catch block, so the original promise has already rejected at this point. Have a look at the sections about chaining promises, and handling errors, at MDN. The return value of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This discussion seems to be diverting away from SW, and more into how the client should work. Would it be better off having that discussion on zooniverse/panoptes-javascript-client? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As to why |
||
console.log('Failed to refresh token: ', error); | ||
AnnotationsFactory.updateCache(); | ||
oauth.signIn($location.absUrl()); | ||
}) | ||
} | ||
// There's only a version of this project on production, so rather than | ||
// defer to the client we manually override the API root. | ||
// Panoptes.apiClient.root = 'https://www.zooniverse.org/api'; | ||
|
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' | ||
]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
))) | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...