Skip to content
Open
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
11 changes: 9 additions & 2 deletions src/app/core/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { inject, Injectable, PLATFORM_ID } from '@angular/core';
import { SignUpModel } from '@core/models/sign-up.model';
import { ENVIRONMENT } from '@core/provider/environment.provider';
import { ClearCurrentUser } from '@osf/core/store/user';
import { urlParam } from '@osf/shared/helpers/url-param.helper';
import { localUrlParam, urlParam } from '@osf/shared/helpers/url-param.helper';
import { JsonApiService } from '@osf/shared/services/json-api.service';
import { LoaderService } from '@osf/shared/services/loader.service';

Expand Down Expand Up @@ -41,7 +41,14 @@ export class AuthService {
}

this.loaderService.show();
const loginUrl = `${this.casUrl}/login?${urlParam({ service: `${this.webUrl}/login`, next: window.location.href })}`;
let loginUrl = null;
if (this.environment.webUrl.includes('localhost')) {
// CAS should handle auth instead of angular, so we need to pass the next param
// in the service param to ensure the user is redirected back to the correct page after login
loginUrl = `${this.casUrl}/login?${localUrlParam({ service: `${this.webUrl.replace('4200', '5000')}/login`, next: window.location.href })}`;
} else {
loginUrl = `${this.casUrl}/login?${urlParam({ service: `${this.webUrl}/login`, next: window.location.href })}`;
}
window.location.href = loginUrl;
}

Expand Down
11 changes: 11 additions & 0 deletions src/app/shared/helpers/url-param.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ export const urlParam = (params: Record<string, string>) => {
.map((entry) => entry.map((comp) => encodeURIComponent(comp)).join('='))
.join('&');
};

export const localUrlParam = (params: { service: string; next?: string }) => {
const { service, next } = params;

// encode "next" separately because it must be encoded twice
const encodedNext = next ? encodeURIComponent(next) : undefined;

const valueAfterService = encodedNext ? `${service}?next=${encodedNext}` : service;

return `service=${encodeURIComponent(valueAfterService)}`;
};
Comment on lines +7 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
export const localUrlParam = (params: { service: string; next?: string }) => {
const { service, next } = params;
// encode "next" separately because it must be encoded twice
const encodedNext = next ? encodeURIComponent(next) : undefined;
const valueAfterService = encodedNext ? `${service}?next=${encodedNext}` : service;
return `service=${encodeURIComponent(valueAfterService)}`;
};
export const localUrlParam = (params: { service: string; next?: string }): string => {
const { service, next } = params;
if (!next) {
return `service=${encodeURIComponent(service)}`;
}
const encodedNext = encodeURIComponent(next);
const valueAfterService = `${service}?next=${encodedNext}`;
return `service=${encodeURIComponent(valueAfterService)}`;
};