Skip to content

Commit

Permalink
Ensure user service is called with userToken when present. Remove old…
Browse files Browse the repository at this point in the history
… transition route munging which is no longer needed.
  • Loading branch information
markpatton committed Oct 13, 2023
1 parent 86f3fc3 commit 5018405
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 35 deletions.
2 changes: 0 additions & 2 deletions app/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ export default class ApplicationController extends Controller {
@alias('model.staticConfig')
staticConfig;

params = ['userToken'];
rootURL = config.rootURL;

@tracked userToken = null;
@tracked wideRoutes = ['grants.index', 'grants.detail', 'submissions.index'];
@tracked brand = get(this, 'staticConfig.branding');
@tracked currentRouteName = this.router.currentRouteName;
Expand Down
40 changes: 7 additions & 33 deletions app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { action } from '@ember/object';
export default class ApplicationRoute extends CheckSessionRoute {
@service('app-static-config') staticConfig;

queryParams = ['userToken'];

/* Used as route-action in templates */
@action
back() {
Expand All @@ -18,42 +20,14 @@ export default class ApplicationRoute extends CheckSessionRoute {
}

/**
* It is possible for unfortunate things to happen somewhere in the backend stack
* that will result in route ids not being encoded.
* Therefore we specially handle the /submissions/id and /grants/id routes. In
* the event that unencoded ID is encountered (it will include slashes), replace
* the current history with the encoded version.
* If there is a userToken query parameter call the user service with that parameter
* to ensure objects are updated in the backend before any queries are done.
*/
beforeModel(transition) {
let intent = transition.intent.url;

if (!intent) {
return;
}
let userToken = transition.to.queryParams.userToken;

let prefix = null;

if (intent.startsWith('/grants/')) {
prefix = '/grants/';
} else if (intent.startsWith('/submissions/')) {
prefix = '/submissions/';
} else {
return;
}

// Work around ember collapsing // into /
if (intent.includes('https:/') && !intent.includes('https://')) {
intent = intent.replace('https:/', 'https://');
}

// Ensure that route parameter is encoded
if (intent.includes('https://')) {
let q = intent.indexOf('?');
if (q == -1) {
q = intent.length;
}
const targetId = intent.substring(prefix.length, q);
this.replaceWith(intent.replace(targetId, `${encodeURIComponent(targetId)}`));
if (userToken) {
return fetch(`/user/whoami?userToken=${encodeURIComponent(userToken)}`);
}
}

Expand Down

0 comments on commit 5018405

Please sign in to comment.