bugfixing
This commit is contained in:
parent
bc8c5cd7f0
commit
1041597f97
@ -4,7 +4,6 @@ import { serializeNonPOJOs } from "$lib/utils";
|
|||||||
|
|
||||||
export const handle = sequence(async ({ event, resolve }) => {
|
export const handle = sequence(async ({ event, resolve }) => {
|
||||||
|
|
||||||
|
|
||||||
// Use a ternary operator instead of the logical OR for better compatibility
|
// 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 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;
|
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") || "";
|
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) {
|
if (event?.locals?.pb?.authStore?.isValid) {
|
||||||
try {
|
try {
|
||||||
await event.locals.pb.collection("users").authRefresh();
|
await event?.locals?.pb?.collection("users")?.authRefresh();
|
||||||
event.locals.user = serializeNonPOJOs(event.locals.pb.authStore.model);
|
event.locals.user = serializeNonPOJOs(event?.locals?.pb?.authStore?.model);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
event.locals.pb.authStore.clear();
|
event.locals.pb.authStore.clear();
|
||||||
event.locals.user = undefined;
|
event.locals.user = undefined;
|
||||||
@ -36,7 +36,7 @@ export const handle = sequence(async ({ event, resolve }) => {
|
|||||||
const response = await resolve(event);
|
const response = await resolve(event);
|
||||||
|
|
||||||
// Use a more compatible way to set the cookie
|
// 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,
|
httpOnly: true,
|
||||||
path: "/",
|
path: "/",
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
|
|||||||
@ -41,10 +41,10 @@
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (
|
if (
|
||||||
["redirect", "success"]?.includes(result.type) &&
|
["redirect", "success"]?.includes(result.type) &&
|
||||||
$page?.url?.pathname === "/pricing"
|
$page?.url?.pathname
|
||||||
) {
|
) {
|
||||||
const anchor = document.createElement("a");
|
const anchor = document.createElement("a");
|
||||||
anchor.href = "/pricing";
|
anchor.href = $page?.url?.pathname;
|
||||||
anchor.dataset.sveltekitReload = true;
|
anchor.dataset.sveltekitReload = true;
|
||||||
document.body.appendChild(anchor);
|
document.body.appendChild(anchor);
|
||||||
anchor.dispatchEvent(new MouseEvent("click"));
|
anchor.dispatchEvent(new MouseEvent("click"));
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
export const load = ({ locals, cookies }) => {
|
export const load = ({ locals, cookies }) => {
|
||||||
const { user, isUSRegion, wsURL } = locals;
|
const { user, isUSRegion, wsURL } = locals;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
user: user || undefined,
|
user: user || undefined,
|
||||||
isUSRegion,
|
isUSRegion,
|
||||||
|
|||||||
@ -57,9 +57,7 @@ export const actions = {
|
|||||||
const targetItem = authMethods?.providers?.findIndex(
|
const targetItem = authMethods?.providers?.findIndex(
|
||||||
(item) => item?.name === providerSelected,
|
(item) => item?.name === providerSelected,
|
||||||
);
|
);
|
||||||
//console.log("==================")
|
|
||||||
//console.log(authMethods.authProviders)
|
|
||||||
//console.log('target item is: ', targetItem)
|
|
||||||
|
|
||||||
const provider = authMethods.providers[targetItem];
|
const provider = authMethods.providers[targetItem];
|
||||||
const authProviderRedirect = `${provider.authUrl}${redirectURL}`;
|
const authProviderRedirect = `${provider.authUrl}${redirectURL}`;
|
||||||
@ -92,7 +90,7 @@ export const actions = {
|
|||||||
maxAge: 60 * 60,
|
maxAge: 60 * 60,
|
||||||
});
|
});
|
||||||
|
|
||||||
cookies.set("path", "/", {
|
cookies.set("path", "/profile", {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
secure: true,
|
secure: true,
|
||||||
@ -100,7 +98,7 @@ export const actions = {
|
|||||||
maxAge: 60,
|
maxAge: 60,
|
||||||
});
|
});
|
||||||
|
|
||||||
redirect(303, authProviderRedirect);
|
redirect(301, authProviderRedirect);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,11 @@
|
|||||||
import { redirect } from "@sveltejs/kit";
|
import { redirect } from "@sveltejs/kit";
|
||||||
import { serializeNonPOJOs } from "$lib/utils";
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const GET = async ({ locals, url, cookies }) => {
|
export const GET = async ({ locals, url, cookies }) => {
|
||||||
//console.log(url.searchParams);
|
//console.log(url.searchParams);
|
||||||
const redirectURL = `${url.origin}/oauth`;
|
const redirectURL = `${url.origin}/oauth`;
|
||||||
//const expectedState = cookies.get('state');
|
|
||||||
//const expectedVerifier = cookies.get('verifier');
|
|
||||||
let newUser;
|
|
||||||
|
|
||||||
let expectedState = cookies?.get("state");
|
let expectedState = cookies?.get("state");
|
||||||
let expectedVerifier = cookies?.get("verifier");
|
let expectedVerifier = cookies?.get("verifier");
|
||||||
@ -17,8 +14,6 @@ export const GET = async ({ locals, url, cookies }) => {
|
|||||||
const state = await url.searchParams.get("state");
|
const state = await url.searchParams.get("state");
|
||||||
const code = await url.searchParams.get("code");
|
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
|
//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;
|
const authMethods = (await locals.pb?.collection("users")?.listAuthMethods())?.oauth2;
|
||||||
@ -69,10 +64,11 @@ export const GET = async ({ locals, url, cookies }) => {
|
|||||||
redirect(302, "/register");
|
redirect(302, "/register");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(cookies?.get("path"))
|
||||||
if (cookies?.get("path")) {
|
if (cookies?.get("path")) {
|
||||||
redirect(301, cookies?.get("path"));
|
redirect(301, cookies?.get("path"));
|
||||||
} else {
|
} else {
|
||||||
redirect(303, "/");
|
redirect(302, "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -105,6 +105,8 @@ await locals.pb?.collection('users').update(
|
|||||||
},
|
},
|
||||||
|
|
||||||
oauth2: async ({ url, locals, request, cookies }) => {
|
oauth2: async ({ url, locals, request, cookies }) => {
|
||||||
|
|
||||||
|
const path = url?.href?.replace("/oauth2","")
|
||||||
const authMethods = (await locals?.pb
|
const authMethods = (await locals?.pb
|
||||||
?.collection("users")
|
?.collection("users")
|
||||||
?.listAuthMethods())?.oauth2;
|
?.listAuthMethods())?.oauth2;
|
||||||
@ -159,7 +161,7 @@ await locals.pb?.collection('users').update(
|
|||||||
maxAge: 60 * 60,
|
maxAge: 60 * 60,
|
||||||
});
|
});
|
||||||
|
|
||||||
cookies.set("path", "/", {
|
cookies.set("path", path, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
secure: true,
|
secure: true,
|
||||||
|
|||||||
@ -91,9 +91,7 @@ export const actions = {
|
|||||||
const targetItem = authMethods?.providers?.findIndex(
|
const targetItem = authMethods?.providers?.findIndex(
|
||||||
(item) => item?.name === providerSelected,
|
(item) => item?.name === providerSelected,
|
||||||
);
|
);
|
||||||
//console.log("==================")
|
|
||||||
//console.log(authMethods.authProviders)
|
|
||||||
//console.log('target item is: ', targetItem)
|
|
||||||
|
|
||||||
const provider = authMethods.providers[targetItem];
|
const provider = authMethods.providers[targetItem];
|
||||||
const authProviderRedirect = `${provider.authUrl}${redirectURL}`;
|
const authProviderRedirect = `${provider.authUrl}${redirectURL}`;
|
||||||
@ -126,7 +124,7 @@ export const actions = {
|
|||||||
maxAge: 60 * 60,
|
maxAge: 60 * 60,
|
||||||
});
|
});
|
||||||
|
|
||||||
cookies.set("path", "/", {
|
cookies.set("path", "/profile", {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
secure: true,
|
secure: true,
|
||||||
@ -134,6 +132,6 @@ export const actions = {
|
|||||||
maxAge: 60,
|
maxAge: 60,
|
||||||
});
|
});
|
||||||
|
|
||||||
redirect(303, authProviderRedirect);
|
redirect(301, authProviderRedirect);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -79,6 +79,8 @@ await locals.pb?.collection('users').update(
|
|||||||
},
|
},
|
||||||
|
|
||||||
oauth2: async ({ url, locals, request, cookies }) => {
|
oauth2: async ({ url, locals, request, cookies }) => {
|
||||||
|
|
||||||
|
const path = url?.href?.replace("/oauth2","")
|
||||||
const authMethods = (await locals?.pb
|
const authMethods = (await locals?.pb
|
||||||
?.collection("users")
|
?.collection("users")
|
||||||
?.listAuthMethods())?.oauth2;
|
?.listAuthMethods())?.oauth2;
|
||||||
@ -133,7 +135,7 @@ await locals.pb?.collection('users').update(
|
|||||||
maxAge: 60 * 60,
|
maxAge: 60 * 60,
|
||||||
});
|
});
|
||||||
|
|
||||||
cookies.set("path", "/", {
|
cookies.set("path", path, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
secure: true,
|
secure: true,
|
||||||
|
|||||||
@ -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 }) => {
|
export const load = async ({ locals, params }) => {
|
||||||
const { apiKey, apiURL } = locals;
|
const { apiKey, apiURL } = locals;
|
||||||
|
|
||||||
@ -108,3 +113,155 @@ export const load = async ({ locals, params }) => {
|
|||||||
getOptionsGexData: await getOptionsGexData(),
|
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);
|
||||||
|
},
|
||||||
|
|
||||||
|
};
|
||||||
|
|||||||
@ -32,7 +32,10 @@ export const load = async ({ locals, params }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
login: async ({ request, locals }) => {
|
login: async ({ url, request, locals }) => {
|
||||||
|
|
||||||
|
const path = url?.href?.replace("/oauth2","")
|
||||||
|
|
||||||
const { formData, errors } = await validateData(
|
const { formData, errors } = await validateData(
|
||||||
await request.formData(),
|
await request.formData(),
|
||||||
loginUserSchema,
|
loginUserSchema,
|
||||||
@ -63,10 +66,12 @@ export const actions = {
|
|||||||
error(err.status, err.message);
|
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(
|
const { formData, errors } = await validateData(
|
||||||
await request.formData(),
|
await request.formData(),
|
||||||
registerUserSchema,
|
registerUserSchema,
|
||||||
@ -88,7 +93,7 @@ await locals.pb?.collection('users').update(
|
|||||||
'tier': 'Pro', //Give new users a free trial for the Pro Subscription
|
'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) {
|
} catch (err) {
|
||||||
console.log("Error: ", err);
|
console.log("Error: ", err);
|
||||||
error(err.status, err.message);
|
error(err.status, err.message);
|
||||||
@ -103,10 +108,12 @@ await locals.pb?.collection('users').update(
|
|||||||
error(err.status, err.message);
|
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
|
const authMethods = (await locals?.pb
|
||||||
?.collection("users")
|
?.collection("users")
|
||||||
?.listAuthMethods())?.oauth2;
|
?.listAuthMethods())?.oauth2;
|
||||||
@ -161,7 +168,7 @@ await locals.pb?.collection('users').update(
|
|||||||
maxAge: 60 * 60,
|
maxAge: 60 * 60,
|
||||||
});
|
});
|
||||||
|
|
||||||
cookies.set("path", "/", {
|
cookies.set("path", path, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
secure: true,
|
secure: true,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user