Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call user service with a userToken parameter #1226

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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