
Hi,
export async function GET(request: NextRequest) {
const userId = request.nextUrl.searchParams.get("userId");
const secret = request.nextUrl.searchParams.get("secret");
if (!userId || !secret) {
return NextResponse.json({ error: "Invalid request" }, { status: 400 });
}
const { account } = await createAdminClient();
const session = await account.createSession(userId, secret);
cookies().set("my-custom-session", session.secret, {
path: "/",
httpOnly: true,
sameSite: "strict",
secure: true,
});
return NextResponse.redirect(`${request.nextUrl.origin}/`);
}
this code redirectes user to home page, but actually when redirected at the same time i am calling
export async function getLoggedInUser() {
try {
const { account } = await createSessionClient();
return await account.get();
} catch (error) {
console.log(error);
return null;
}
this function and this function return null, after refresh its still returns null. But when i enter url to browser and login again it works

user is updated

i dont understand, i followed docs and its not working

cookies().set("my-custom-session", session.secret, {
path: "/",
httpOnly: true,
sameSite: isProduction ? "none" : "lax",
secure: isProduction,
});
would that be good option?

@Moderator please, any idea, we are stuck. It is working but we have no idea if it is the most secure implementation

i meant in nextjs strict not working when redirected and cookie was added browser still is in old state and is not consistent to new cookie that was successfuly added thats why i changed strict to lax and it was working actually, i didnot have time and not searched about that attributes i copied everything from docs, maybe docs need to update

in docs 'strict' was written, dunno if u tested this before u wrote that in docs

Does it work if you refresh the page after logging in?

nope

in 'strict' mode

refreshing not working

Do you have the code for createSessionClient()
?

but if i enter url from address bar

export async function createSessionClient() {
const client = new Client()
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!)
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT!);
const session = cookies().get("my-custom-session");
if (!session || !session.value) {
throw new Error("No session");
}
client.setSession(session.value);
return {
get account() {
return new Account(client);
},
};
}```

It works if you enter the URL from the address bar?

yep

Try this change after you set the cookie
const response = NextResponse.redirect(`${request.nextUrl.origin}/`);
response.cookies.set("my-custom-session", session.secret);
return response;
Recommended threads
- Need help setting up this error is showi...
You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy. If you're the app developer, register the redirect URI in the Google Cl...
- Appwrite stopped working, I can't authen...
I'm having an issue with Appwrite. It was working fine just a while ago, but suddenly it stopped working for me and can't authenticate accounts. I even went bac...
- Fail to receive the verification email a...
I added my email address to prevent it from showing "appwrite," but now I'm not receiving emails for verification or password resets. The function appears to be...
