Fix OSM auth not being loaded correctly on launch
This commit is contained in:
@@ -7,7 +7,15 @@ class MarcoOsmAuthStorage {
|
||||
localStorage.setItem('marco:osm_auth_state', serializedState);
|
||||
}
|
||||
loadState() {
|
||||
return localStorage.getItem('marco:osm_auth_state');
|
||||
const state = localStorage.getItem('marco:osm_auth_state');
|
||||
if (!state) return false;
|
||||
try {
|
||||
JSON.parse(state);
|
||||
return state;
|
||||
} catch (e) {
|
||||
console.warn('Failed to parse OSM auth state', e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +53,11 @@ export default class OsmAuthService extends Service {
|
||||
}
|
||||
|
||||
async restoreSession() {
|
||||
try {
|
||||
await this.oauthClient.ready;
|
||||
} catch (e) {
|
||||
console.warn('oauthClient.ready failed', e);
|
||||
}
|
||||
const isAuthorized = await this.oauthClient.isAuthorized();
|
||||
if (isAuthorized) {
|
||||
this.isConnected = true;
|
||||
|
||||
@@ -52,6 +52,7 @@ module('Unit | Service | osm-auth', function (hooks) {
|
||||
// Because restoreSession runs in the constructor, we might need to overwrite it after, but it's async.
|
||||
// Let's just create it, let the original restoreSession fail or do nothing, and then we stub and re-call it.
|
||||
|
||||
service.oauthClient.ready = Promise.resolve();
|
||||
service.oauthClient.isAuthorized = async () => true;
|
||||
window.localStorage.setItem('marco:osm_user_display_name', 'CachedName');
|
||||
|
||||
@@ -68,6 +69,7 @@ module('Unit | Service | osm-auth', function (hooks) {
|
||||
test('it fetches user info when logged in but no cached name', async function (assert) {
|
||||
let service = this.owner.factoryFor('service:osm-auth').create();
|
||||
|
||||
service.oauthClient.ready = Promise.resolve();
|
||||
service.oauthClient.isAuthorized = async () => true;
|
||||
service.oauthClient.getTokens = async () => ({ accessToken: 'fake-token' });
|
||||
// Ensure localStorage is empty for this key
|
||||
|
||||
Reference in New Issue
Block a user