Cant see cookies
Unanswered
Arinji posted this in #help-forum
ArinjiOP
import { RequestCookies } from "next/dist/compiled/@edge-runtime/cookies";
import Pocketbase from "pocketbase";
export async function initPocketbaseServer(cookies: RequestCookies) {
const pb = new Pocketbase(process.env.NEXT_PUBLIC_POCKETBASE_URL);
const authCookie = cookies.get("pb_auth");
pb.authStore.loadFromCookie(authCookie?.value || "");
pb.authStore.onChange(() => {
console.log("authStore changed");
cookies.set("pb_auth", pb.authStore.exportToCookie());
console.log(pb.authStore.model);
});
try {
pb.authStore.isValid && (await pb.collection("users").authRefresh());
} catch (_) {
pb.authStore.clear();
}
return pb;
}
export async function initPocketbaseClient() {
const pb = new Pocketbase(process.env.NEXT_PUBLIC_POCKETBASE_URL);
return pb;
}1 Reply
ArinjiOP
I am calling it from this route handler
However i dont see the cookies being added even though i get a success message|
import { initPocketbaseServer } from "@/utils/pocketbaseInit";
import { RegisterFormSchema } from "@/utils/validations/auth/schema";
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: NextRequest) {
const body = await req.json().catch(() => {
return NextResponse.json(
{ success: false, message: "Body not provided" },
{ status: 400, headers: { "content-type": "application/json" } },
);
});
const { email, username, password, confirmPassword } = body;
const parsedForm = RegisterFormSchema.safeParse({
email,
username,
password,
confirmPassword,
});
if (!parsedForm.success) {
return NextResponse.json(
{ success: false, message: parsedForm.error.issues[0].message },
{ status: 400, headers: { "content-type": "application/json" } },
);
}
const pb = await initPocketbaseServer(req.cookies);
const user = await pb.collection("users").create({
email,
username,
password,
passwordConfirm: confirmPassword,
});
await pb.collection("users").authWithPassword(email, password);
return NextResponse.json(
{ success: true, message: "User created successfully", user },
{ status: 200, headers: { "content-type": "application/json" } },
);
}However i dont see the cookies being added even though i get a success message|