bugfixing

This commit is contained in:
MuslemRahimi 2024-12-11 11:37:15 +01:00
parent bc8c5cd7f0
commit 1041597f97
10 changed files with 195 additions and 36 deletions

View File

@ -4,7 +4,6 @@ import { serializeNonPOJOs } from "$lib/utils";
export const handle = sequence(async ({ event, resolve }) => {
// Use a ternary operator instead of the logical OR for better compatibility
const pbURL = import.meta.env.VITE_USEAST_POCKETBASE_URL; //isUS ? import.meta.env.VITE_USEAST_POCKETBASE_URL : import.meta.env.VITE_EU_POCKETBASE_URL;
const apiURL = import.meta.env.VITE_USEAST_API_URL; //isUS ? import.meta.env.VITE_USEAST_API_URL : import.meta.env.VITE_EU_API_URL;
@ -20,12 +19,13 @@ export const handle = sequence(async ({ event, resolve }) => {
};
const authCookie = event?.request?.headers?.get("cookie") || "";
event.locals.pb.authStore.loadFromCookie(authCookie);
event.locals.pb.authStore?.loadFromCookie(authCookie);
if (event?.locals?.pb?.authStore?.isValid) {
try {
await event.locals.pb.collection("users").authRefresh();
event.locals.user = serializeNonPOJOs(event.locals.pb.authStore.model);
await event?.locals?.pb?.collection("users")?.authRefresh();
event.locals.user = serializeNonPOJOs(event?.locals?.pb?.authStore?.model);
} catch (e) {
event.locals.pb.authStore.clear();
event.locals.user = undefined;
@ -36,7 +36,7 @@ export const handle = sequence(async ({ event, resolve }) => {
const response = await resolve(event);
// Use a more compatible way to set the cookie
const cookieString = event.locals.pb.authStore.exportToCookie({
const cookieString = event?.locals?.pb?.authStore?.exportToCookie({
httpOnly: true,
path: "/",
sameSite: "lax",

View File

@ -41,10 +41,10 @@
setTimeout(() => {
if (
["redirect", "success"]?.includes(result.type) &&
$page?.url?.pathname === "/pricing"
$page?.url?.pathname
) {
const anchor = document.createElement("a");
anchor.href = "/pricing";
anchor.href = $page?.url?.pathname;
anchor.dataset.sveltekitReload = true;
document.body.appendChild(anchor);
anchor.dispatchEvent(new MouseEvent("click"));

View File

@ -1,6 +1,5 @@
export const load = ({ locals, cookies }) => {
const { user, isUSRegion, wsURL } = locals;
return {
user: user || undefined,
isUSRegion,

View File

@ -57,9 +57,7 @@ export const actions = {
const targetItem = authMethods?.providers?.findIndex(
(item) => item?.name === providerSelected,
);
//console.log("==================")
//console.log(authMethods.authProviders)
//console.log('target item is: ', targetItem)
const provider = authMethods.providers[targetItem];
const authProviderRedirect = `${provider.authUrl}${redirectURL}`;
@ -92,7 +90,7 @@ export const actions = {
maxAge: 60 * 60,
});
cookies.set("path", "/", {
cookies.set("path", "/profile", {
httpOnly: true,
sameSite: "lax",
secure: true,
@ -100,7 +98,7 @@ export const actions = {
maxAge: 60,
});
redirect(303, authProviderRedirect);
redirect(301, authProviderRedirect);
},
};

View File

@ -1,14 +1,11 @@
import { redirect } from "@sveltejs/kit";
import { serializeNonPOJOs } from "$lib/utils";
export const GET = async ({ locals, url, cookies }) => {
//console.log(url.searchParams);
const redirectURL = `${url.origin}/oauth`;
//const expectedState = cookies.get('state');
//const expectedVerifier = cookies.get('verifier');
let newUser;
let expectedState = cookies?.get("state");
let expectedVerifier = cookies?.get("verifier");
@ -17,8 +14,6 @@ export const GET = async ({ locals, url, cookies }) => {
const state = await url.searchParams.get("state");
const code = await url.searchParams.get("code");
//console.log('returned state',state)
//console.log('returned code',code)
//as a side effect this will generate a new code verifier, hence why we need to pass the verifier back in through the cookie
const authMethods = (await locals.pb?.collection("users")?.listAuthMethods())?.oauth2;
@ -69,10 +64,11 @@ export const GET = async ({ locals, url, cookies }) => {
redirect(302, "/register");
}
console.log(cookies?.get("path"))
if (cookies?.get("path")) {
redirect(301, cookies?.get("path"));
} else {
redirect(303, "/");
redirect(302, "/");
}

View File

@ -104,7 +104,9 @@ await locals.pb?.collection('users').update(
redirect(302, "/pricing");
},
oauth2: async ({ url, locals, request, cookies }) => {
oauth2: async ({ url, locals, request, cookies }) => {
const path = url?.href?.replace("/oauth2","")
const authMethods = (await locals?.pb
?.collection("users")
?.listAuthMethods())?.oauth2;
@ -159,7 +161,7 @@ await locals.pb?.collection('users').update(
maxAge: 60 * 60,
});
cookies.set("path", "/", {
cookies.set("path", path, {
httpOnly: true,
sameSite: "lax",
secure: true,

View File

@ -71,7 +71,7 @@ export const actions = {
redirect(301, "/");
},
oauth2: async ({ url, locals, request, cookies }) => {
oauth2: async ({ url, locals, request, cookies }) => {
const authMethods = (await locals?.pb
?.collection("users")
?.listAuthMethods())?.oauth2;
@ -91,9 +91,7 @@ export const actions = {
const targetItem = authMethods?.providers?.findIndex(
(item) => item?.name === providerSelected,
);
//console.log("==================")
//console.log(authMethods.authProviders)
//console.log('target item is: ', targetItem)
const provider = authMethods.providers[targetItem];
const authProviderRedirect = `${provider.authUrl}${redirectURL}`;
@ -126,7 +124,7 @@ export const actions = {
maxAge: 60 * 60,
});
cookies.set("path", "/", {
cookies.set("path", "/profile", {
httpOnly: true,
sameSite: "lax",
secure: true,
@ -134,6 +132,6 @@ export const actions = {
maxAge: 60,
});
redirect(303, authProviderRedirect);
redirect(301, authProviderRedirect);
},
};

View File

@ -79,6 +79,8 @@ await locals.pb?.collection('users').update(
},
oauth2: async ({ url, locals, request, cookies }) => {
const path = url?.href?.replace("/oauth2","")
const authMethods = (await locals?.pb
?.collection("users")
?.listAuthMethods())?.oauth2;
@ -133,7 +135,7 @@ await locals.pb?.collection('users').update(
maxAge: 60 * 60,
});
cookies.set("path", "/", {
cookies.set("path", path, {
httpOnly: true,
sameSite: "lax",
secure: true,

View File

@ -1,3 +1,8 @@
import { error, fail, redirect } from "@sveltejs/kit";
import { validateData } from "$lib/utils";
import { loginUserSchema, registerUserSchema } from "$lib/schemas";
export const load = async ({ locals, params }) => {
const { apiKey, apiURL } = locals;
@ -108,3 +113,155 @@ export const load = async ({ locals, params }) => {
getOptionsGexData: await getOptionsGexData(),
};
};
export const actions = {
login: async ({ url, request, locals }) => {
const path = url?.href?.replace("/oauth2","")
const { formData, errors } = await validateData(
await request.formData(),
loginUserSchema,
);
if (errors) {
return fail(400, {
data: formData,
errors: errors.fieldErrors,
});
}
try {
await locals.pb
.collection("users")
.authWithPassword(formData.email, formData.password);
/*
if (!locals.pb?.authStore?.model?.verified) {
locals.pb.authStore.clear();
return {
notVerified: true,
};
}
*/
} catch (err) {
console.log("Error: ", err);
error(err.status, err.message);
}
redirect(302, path);
},
register: async ({ url, locals, request }) => {
const path = url?.href?.replace("/oauth2","")
const { formData, errors } = await validateData(
await request.formData(),
registerUserSchema,
);
if (errors) {
return fail(400, {
data: formData,
errors: errors.fieldErrors,
});
}
try {
let newUser = await locals.pb.collection("users").create(formData);
/*
await locals.pb?.collection('users').update(
newUser?.id, {
'freeTrial' : true,
'tier': 'Pro', //Give new users a free trial for the Pro Subscription
});
*/
await locals.pb.collection("users")?.requestVerification(formData.email);
} catch (err) {
console.log("Error: ", err);
error(err.status, err.message);
}
try {
await locals.pb
.collection("users")
.authWithPassword(formData.email, formData.password);
} catch (err) {
console.log("Error: ", err);
error(err.status, err.message);
}
redirect(303, path);
},
oauth2: async ({ url, locals, request, cookies }) => {
const path = url?.href?.replace("/oauth2","")
const authMethods = (await locals?.pb
?.collection("users")
?.listAuthMethods())?.oauth2;
const data = await request?.formData();
const providerSelected = data?.get("provider");
if (!authMethods) {
return {
authProviderRedirect: "",
authProviderState: "",
};
}
const redirectURL = `${url.origin}/oauth`;
const targetItem = authMethods?.providers?.findIndex(
(item) => item?.name === providerSelected,
);
//console.log("==================")
//console.log(authMethods.authProviders)
//console.log('target item is: ', targetItem)
const provider = authMethods.providers[targetItem];
const authProviderRedirect = `${provider.authUrl}${redirectURL}`;
const state = provider.state;
const verifier = provider.codeVerifier;
cookies.set("state", state, {
httpOnly: true,
sameSite: "lax",
secure: true,
path: "/",
maxAge: 60 * 60,
});
cookies.set("verifier", verifier, {
httpOnly: true,
sameSite: "lax",
secure: true,
path: "/",
maxAge: 60 * 60,
});
cookies.set("provider", providerSelected, {
httpOnly: true,
sameSite: "lax",
secure: true,
path: "/",
maxAge: 60 * 60,
});
cookies.set("path", path, {
httpOnly: true,
sameSite: "lax",
secure: true,
path: "/",
maxAge: 60,
});
redirect(302, authProviderRedirect);
},
};

View File

@ -32,7 +32,10 @@ export const load = async ({ locals, params }) => {
};
export const actions = {
login: async ({ request, locals }) => {
login: async ({ url, request, locals }) => {
const path = url?.href?.replace("/oauth2","")
const { formData, errors } = await validateData(
await request.formData(),
loginUserSchema,
@ -63,10 +66,12 @@ export const actions = {
error(err.status, err.message);
}
redirect(302, "/");
redirect(301, path);
},
register: async ({ locals, request }) => {
register: async ({ url, locals, request }) => {
const path = url?.href?.replace("/oauth2","")
const { formData, errors } = await validateData(
await request.formData(),
registerUserSchema,
@ -88,7 +93,7 @@ await locals.pb?.collection('users').update(
'tier': 'Pro', //Give new users a free trial for the Pro Subscription
});
*/
await locals.pb.collection("users").requestVerification(formData.email);
await locals.pb.collection("users")?.requestVerification(formData.email);
} catch (err) {
console.log("Error: ", err);
error(err.status, err.message);
@ -103,10 +108,12 @@ await locals.pb?.collection('users').update(
error(err.status, err.message);
}
redirect(303, "/");
redirect(301, path);
},
oauth2: async ({ url, locals, request, cookies }) => {
oauth2: async ({ url, locals, request, cookies }) => {
const path = url?.href?.replace("/oauth2","")
const authMethods = (await locals?.pb
?.collection("users")
?.listAuthMethods())?.oauth2;
@ -161,7 +168,7 @@ await locals.pb?.collection('users').update(
maxAge: 60 * 60,
});
cookies.set("path", "/", {
cookies.set("path", path, {
httpOnly: true,
sameSite: "lax",
secure: true,