Skip to content
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
1 change: 1 addition & 0 deletions js/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const TLD: Record<string, string> = {
};

export const NEETO_URL_COMPONENT_REGEX = /neeto(\w+)/;
export const NEETO_URL_PREFIX_REGEX = /^(https?:\/\/)?(www\.)?[\w-]+\./;

export const CLIENT_APPS = {
cal: "Cal",
Expand Down
9 changes: 7 additions & 2 deletions js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import jwt from "jsonwebtoken";
import { getClientAppName, getLoginUri, SearchParams } from "./utils.js";
import {
getClientAppName,
getLoginUri,
getRedirectUri,
SearchParams,
} from "./utils.js";

interface Options {
email: string;
Expand Down Expand Up @@ -48,7 +53,7 @@ class NeetoJWT {

const searchParams: SearchParams = {
jwt: this.generateJWT(),
redirect_uri: encodeURI(redirectUri),
redirect_uri: getRedirectUri(redirectUri),
client_app_name: getClientAppName(redirectUri),
};

Expand Down
17 changes: 16 additions & 1 deletion js/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CLIENT_APPS, NEETO_URL_COMPONENT_REGEX, TLD } from "./constants.js";
import {
CLIENT_APPS,
NEETO_URL_COMPONENT_REGEX,
NEETO_URL_PREFIX_REGEX,
TLD,
} from "./constants.js";

export type SearchParams = {
jwt: string;
Expand Down Expand Up @@ -29,3 +34,13 @@ export const getClientAppName = (redirectUri: string) => {

return "Cal";
};

export const getRedirectUri = (redirectUri: string) => {
const match = redirectUri.match(NEETO_URL_PREFIX_REGEX);

if (match) {
return encodeURI(redirectUri.replace(NEETO_URL_PREFIX_REGEX, ""));
}

return encodeURI(getTopLevelDomain());
};
2 changes: 1 addition & 1 deletion js/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("NeetoJWT", () => {
expect(loginUrl).toContain("https://spinkart.neetoauth.com/users/auth/jwt");
expect(loginUrl).toContain(`jwt=`);
expect(loginUrl).toContain(
`redirect_uri=${encodeURIComponent(redirectUri)}`
`redirect_uri=${encodeURIComponent("neetocal.com/admin")}`
);
expect(loginUrl).toContain(`client_app_name=Cal`);
});
Expand Down
Loading