[SOLVED] getLoggedInUser of Google OAuth SSR gives null once logged in success
- 0
- Auth
- Web
- Cloud
I'm really sure there is bug. Step to reproduce:
- download this project https://github.com/appwrite/demos-for-react/tree/master/nextjs/server-side-rendering
- Change OAuth sign in, from Github with Google
"use server";
import { createAdminClient } from "./appwrite";
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { OAuthProvider } from "node-appwrite";
export async function signInWithGoogle() {
const { account } = await createAdminClient();
const origin = headers().get("origin");
const successUrl = `${origin}/oauth`;
const failureUrl = `${origin}/signin`;
const redirectUrl = await account.createOAuth2Token(
OAuthProvider.Google,
successUrl,
failureUrl
);
redirect(redirectUrl);
}
- npm run dev
- Sign in with google
- You will redirect to sign in page because of below lines. Means, getLoggedInUser is null.
// app/account/page.tsx
const user = await getLoggedInUser();
if (!user) redirect("/signin");
- Refresh the page, then you will be redirected to /account page.
Maybe there is some delay, with the session, i'm not sure, but I have checked my local browser, and session in Appwrite console exist.
I think I ran into a similar problem the other day. It's how Next handles redirects from what I can tell. The way I fixed it was by adding the cookie to the NextResponse as well at the end before returning it in /app/oauth/route.ts
const response = NextResponse.redirect(`${request.nextUrl.origin}/account`);
response.cookies.set(SESSION_COOKIE, session.secret);
return response;
Wahh, thank you for your help. It works like you said. need to add the cookie. Thanks a lot @Ryan
[SOLVED] getLoggedInUser of Google OAuth SSR gives null once logged in success
No worries, I'll see if I can put in a PR/issue to update the repo
Recommended threads
- Invalid document structure: missing requ...
I just pick up my code that's working a week ago, and now I got this error: ``` code: 400, type: 'document_invalid_structure', response: { message: 'Inv...
- Error: User (role: guests) missing scope...
I want to send a verification code to the user and the given phone number and check it and create a session right after the user entered the secret. For me that...
- Apple OAuth Scopes
Hi Hi, I've configured sign in with apple and this is the response i'm getting from apple once i've signed in. I cant find anywhere I set scopes. I remember se...