significant refactor

This commit is contained in:
2025-08-30 01:42:23 -05:00
parent 7136f646a3
commit 052f53444e
106 changed files with 1994 additions and 1701 deletions

View File

@@ -1,8 +1,8 @@
import SuperTokens from 'supertokens-web-js';
import Session from 'supertokens-web-js/recipe/session';
import Passwordless from 'supertokens-web-js/recipe/passwordless';
import { appInfo } from './config';
import { logger } from './';
import SuperTokens from "supertokens-web-js";
import Session from "supertokens-web-js/recipe/session";
import Passwordless from "supertokens-web-js/recipe/passwordless";
import { appInfo } from "./config";
import { logger } from "./";
export const frontendConfig = () => {
return {
@@ -12,27 +12,27 @@ export const frontendConfig = () => {
Session.init({
tokenTransferMethod: "cookie",
sessionTokenBackendDomain: undefined,
preAPIHook: async (context) => {
context.requestInit.credentials = "include";
return context;
},
})
]
}),
],
};
}
};
let initialized = false;
export function ensureSuperTokensFrontend() {
if (typeof window === 'undefined') return;
if (typeof window === "undefined") return;
if (!initialized) {
SuperTokens.init(frontendConfig());
initialized = true;
logger.info("Initialized");
Session.doesSessionExist().then(exists => {
logger.info(`Session does${exists ? '' : 'NOT'} exist on load!`);
Session.doesSessionExist().then((exists) => {
logger.info(`Session does${exists ? "" : "NOT"} exist on load!`);
});
}
}
}

View File

@@ -1,7 +1,7 @@
import Passwordless from "supertokens-node/recipe/passwordless";
import { logger } from "../";
const init = () => (
const init = () =>
Passwordless.init({
flowType: "USER_INPUT_CODE",
contactMethod: "PHONE",
@@ -14,17 +14,17 @@ const init = () => (
throw new Error("No user input code provided to sendSms");
}
logger.info('Sending Code',
'######################',
'## SuperTokens Code ##',
logger.info(
"Sending Code",
"######################",
"## SuperTokens Code ##",
`## ${userInputCode} ##`,
'######################'
"######################"
);
}
}
}
}
})
)
},
};
},
},
});
export default { init };

View File

@@ -2,7 +2,7 @@ import { useSession } from "@tanstack/react-start/server";
import Passwordless from "supertokens-node/recipe/passwordless";
import { sendVerifyCode, updateVerify } from "@/lib/twilio";
const init = () => (
const init = () =>
Passwordless.init({
flowType: "USER_INPUT_CODE",
contactMethod: "PHONE",
@@ -18,22 +18,24 @@ const init = () => (
const sid = await sendVerifyCode(phoneNumber, userInputCode);
const session = await useSession({
password: preAuthSessionId
password: preAuthSessionId,
});
await session.update({
twilioSid: sid
twilioSid: sid,
});
}
}
}
},
};
},
},
override: {
functions: (originalImplementation) => {
return {
...originalImplementation,
consumeCode: async (input) => {
const session = await useSession({ password: input.preAuthSessionId });
const session = await useSession({
password: input.preAuthSessionId,
});
const twilioSid = session?.data.twilioSid;
if (!twilioSid) {
@@ -46,20 +48,22 @@ const init = () => (
await updateVerify(twilioSid);
await session.update({
twilioSid: undefined,
userId: response?.user.id
})
userId: response?.user.id,
});
} else if (response.status === "INCORRECT_USER_INPUT_CODE_ERROR") {
if (response.failedCodeInputAttemptCount !== response.maximumCodeInputAttempts) {
if (
response.failedCodeInputAttemptCount !==
response.maximumCodeInputAttempts
) {
await updateVerify(twilioSid);
}
}
return response;
}
}
}
}
})
)
},
};
},
},
});
export default { init };

View File

@@ -9,6 +9,14 @@ export async function getSessionForStart(request: Request, options?: { sessionRe
const session = await getSessionForSSR(request);
if (session.hasToken) {
if (session.accessTokenPayload?.sub === undefined || session.accessTokenPayload?.sessionHandle === undefined) {
return {
hasToken: true,
needsRefresh: true,
error: 'TRY_REFRESH_TOKEN'
}
}
return {
hasToken: true,
accessTokenPayload: session.accessTokenPayload,
@@ -36,13 +44,3 @@ export async function getSessionForStart(request: Request, options?: { sessionRe
throw error;
}
}
export async function verifySession(request: Request, options?: { sessionRequired?: boolean }) {
const session = await getSessionForStart(request, options);
if (!session && options?.sessionRequired !== false) {
throw new Response("Unauthorized", { status: 401 });
}
return session;
}

View File

@@ -9,28 +9,30 @@ import { logger } from "./";
export const backendConfig = (): TypeInput => {
return {
framework: 'custom',
framework: "custom",
supertokens: {
connectionURI: import.meta.env.VITE_SUPERTOKENS_URI || "https://try.supertokens.io",
connectionURI:
import.meta.env.VITE_SUPERTOKENS_URI || "https://try.supertokens.io",
},
appInfo,
recipeList: [
PasswordlessDevelopmentMode.init(),
Session.init({
cookieSameSite: "lax",
cookieSecure: process.env.NODE_ENV === 'production',
cookieDomain: process.env.NODE_ENV === 'production' ? ".example.com" : undefined,
antiCsrf: process.env.NODE_ENV === 'production' ? "VIA_TOKEN" : "NONE",
cookieSecure: process.env.NODE_ENV === "production",
cookieDomain:
process.env.NODE_ENV === "production" ? ".example.com" : undefined,
antiCsrf: process.env.NODE_ENV === "production" ? "VIA_TOKEN" : "NONE",
// Debug only
exposeAccessTokenToFrontendInCookieBasedAuth: true,
}),
Dashboard.init(),
UserRoles.init()
UserRoles.init(),
],
telemetry: process.env.NODE_ENV !== 'production',
telemetry: process.env.NODE_ENV !== "production",
};
}
};
let initialized = false;
export function ensureSuperTokensBackend() {