deffemttg
deffemttg
Explore posts from servers
TTCTheo's Typesafe Cult
Created by Hitori on 5/19/2023 in #questions
Database Recommedation
I think that PlanetScale+Prisma is the perfect beginner friendly combo to get you started with relational databases
7 replies
TTCTheo's Typesafe Cult
Created by deffemttg on 5/19/2023 in #questions
Set cookies to a different domain
Here are some more info on my middleware.ts:
export default authMiddleware({
beforeAuth(req) {
// bla bla bla
// a bunch of NextResponse.rewrite(...)
},
publicRoutes(req) {
// everything on the root domain (marketing) is public
// and the /signin and /signup pages in the "accounts" subdomain
},
afterAuth(auth, req) {
const { protocol, domain } = parseUrl(req);

// handle users who aren't authenticated
// (redirect to https://account.example.com/signin)
if (!auth.userId && !auth.isPublicRoute) {
const signInUrl = new URL(`${protocol}account.${domain}/signin`);
const redirectUrl = new URL(`${protocol}account.${domain}/`);
signInUrl.searchParams.set("redirect_url", redirectUrl.toString());
return NextResponse.redirect(signInUrl);
}


//*********************************************//
// THIS IS THE THING I TRIED, BUT DOESN'T WORK, ANY SUGGESTION?

// get the clerk auth cookie and set it on the root domain
if (auth.userId) {
const clerkAuthCookie = req.cookies.get("__session")?.value || "";
const response = NextResponse.next();
response.cookies.set({
name: "__session",
value: clerkAuthCookie,
domain: `${domain}`,
});
console.log(
`setting __session cookie on the domain ${domain}`,
clerkAuthCookie
);
return response;
}
//*********************************************//
},
});
export default authMiddleware({
beforeAuth(req) {
// bla bla bla
// a bunch of NextResponse.rewrite(...)
},
publicRoutes(req) {
// everything on the root domain (marketing) is public
// and the /signin and /signup pages in the "accounts" subdomain
},
afterAuth(auth, req) {
const { protocol, domain } = parseUrl(req);

// handle users who aren't authenticated
// (redirect to https://account.example.com/signin)
if (!auth.userId && !auth.isPublicRoute) {
const signInUrl = new URL(`${protocol}account.${domain}/signin`);
const redirectUrl = new URL(`${protocol}account.${domain}/`);
signInUrl.searchParams.set("redirect_url", redirectUrl.toString());
return NextResponse.redirect(signInUrl);
}


//*********************************************//
// THIS IS THE THING I TRIED, BUT DOESN'T WORK, ANY SUGGESTION?

// get the clerk auth cookie and set it on the root domain
if (auth.userId) {
const clerkAuthCookie = req.cookies.get("__session")?.value || "";
const response = NextResponse.next();
response.cookies.set({
name: "__session",
value: clerkAuthCookie,
domain: `${domain}`,
});
console.log(
`setting __session cookie on the domain ${domain}`,
clerkAuthCookie
);
return response;
}
//*********************************************//
},
});
2 replies