Skip to content

Commit

Permalink
Merge pull request #122 from kinde-oss/peter/remove-trailing-slashes
Browse files Browse the repository at this point in the history
Peter/remove trailing slashes
  • Loading branch information
peterphanouvong authored Feb 8, 2024
2 parents bc20c1f + 2fea4d0 commit 8c5c6ba
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {GrantType} from '@kinde-oss/kinde-typescript-sdk';
import {version} from '../utils/version';
import {removeTrailingSlash} from '../utils/removeTrailingSlash';

/**
* @type {import('../../types').KindeState}
Expand Down Expand Up @@ -30,25 +31,28 @@ const initialState = {

const SESSION_PREFIX = 'pkce-verifier';

const KINDE_SITE_URL = process.env.KINDE_SITE_URL;
const KINDE_SITE_URL = removeTrailingSlash(process.env.KINDE_SITE_URL);

// We need to use NEXT_PUBLIC for frontend vars
const KINDE_AUTH_API_PATH =
process.env.NEXT_PUBLIC_KINDE_AUTH_API_PATH ||
process.env.KINDE_AUTH_API_PATH ||
removeTrailingSlash(process.env.NEXT_PUBLIC_KINDE_AUTH_API_PATH) ||
removeTrailingSlash(process.env.KINDE_AUTH_API_PATH) ||
'/api/auth';

const KINDE_POST_LOGIN_REDIRECT_URL =
process.env.KINDE_POST_LOGIN_REDIRECT_URL ||
process.env.KINDE_POST_LOGIN_URL_REDIRECT_URL;
const KINDE_POST_LOGOUT_REDIRECT_URL =
process.env.KINDE_POST_LOGOUT_REDIRECT_URL;
removeTrailingSlash(process.env.KINDE_POST_LOGIN_REDIRECT_URL) ||
removeTrailingSlash(process.env.KINDE_POST_LOGIN_URL_REDIRECT_URL);
const KINDE_POST_LOGOUT_REDIRECT_URL = removeTrailingSlash(
process.env.KINDE_POST_LOGOUT_REDIRECT_URL
);

const KINDE_ISSUER_URL = process.env.KINDE_ISSUER_URL;
const KINDE_ISSUER_URL = removeTrailingSlash(process.env.KINDE_ISSUER_URL);
const KINDE_CLIENT_ID = process.env.KINDE_CLIENT_ID;
const KINDE_CLIENT_SECRET = process.env.KINDE_CLIENT_SECRET;
const KINDE_AUDIENCE = process.env.KINDE_AUDIENCE;
const KINDE_COOKIE_DOMAIN = process.env.KINDE_COOKIE_DOMAIN;
const KINDE_COOKIE_DOMAIN = removeTrailingSlash(
process.env.KINDE_COOKIE_DOMAIN
);

export const config = {
apiPath: KINDE_AUTH_API_PATH,
Expand Down
11 changes: 11 additions & 0 deletions src/utils/removeTrailingSlash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function removeTrailingSlash(url) {
if (url === undefined) return url;

url = url.trim();

if (url.endsWith('/')) {
url = url.slice(0, -1);
}

return url;
}
23 changes: 23 additions & 0 deletions tests/frontend/utils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {removeTrailingSlash} from '../../src/utils/removeTrailingSlash'; // Assuming the function is exported from utils file

describe('removeTrailingSlash', () => {
test('should remove trailing slash', () => {
const url = 'http://example.com/';
expect(removeTrailingSlash(url)).toBe('http://example.com');
});

test('should trim whitespace', () => {
const url = ' http://example.com/ ';
expect(removeTrailingSlash(url)).toBe('http://example.com');
});

test('should handle url without trailing slash', () => {
const url = 'http://example.com';
expect(removeTrailingSlash(url)).toBe('http://example.com');
});

test('should handle empty string', () => {
const url = '';
expect(removeTrailingSlash(url)).toBe('');
});
});

0 comments on commit 8c5c6ba

Please sign in to comment.