Help in Implementing OAuth Authentication with Appwrite in Next.js 15 + Types with SSR and Cookies
- 1
- Web

I'm working on implementing OAuth authentication in my Next.js application using Appwrite as the backend
Technical Environment: Next.js (lastest) Appwrite (lastest) Authentication Type: OAuth (provider: Google/GitHub/etc.) Node-appwrite/appwrite The problem is that node-appwrite doest have oauth logun and I want to store the cookies as well I want to use react-query to store user data

node-appwrite does allow for oauth login.
https://appwrite.io/docs/references/cloud/server-nodejs/account#createOAuth2Token

You can check out this tutorial for oauth in nextjs ssr

Thanks!!

but How to sotre the cookies of the OAuth?

TYhis its a reference ``` app.get('/oauth/success', async (req, res) => { const account = new Account(adminClient);
// Get the userId and secret from the URL parameters
const { userId, secret } = req.query;
try {
// Create the session using the Appwrite client
const session = await account.createSession(userId, secret);
// Set the session cookie
res.cookie('session', session.secret, { // Use the session secret as the cookie value
httpOnly: true,
secure: true,
sameSite: 'strict',
maxAge: sesion.expire
path: '/',
});
res.status(200).json({ success: true });
} catch (e) {
res.status(400).json({ success: false, error: e.message });
}
});``` but the main problem its how to get the Token if Im using Appwrite Cloud and Im a Pro user

Go ahead and checkout the tutorial I linked you.
You'll need to setup a callback for the oauth redirect to get the session and store it in the cookie.
https://appwrite.io/docs/tutorials/nextjs-ssr-auth/step-7#oauth-callback

Cool, Thanks

Hmm, This its unexpected Error fetching user: AppwriteException: app.6723dcc0001423226a2f@service.cloud.appwrite.io (role: applications) missing scope (account)
at _Client.call (turbopack://[project]/node_modules/node-appwrite/src/client.ts:347:8)

to use account.get you need to create a new client that sets the users session on the client

try {
const { account } = await createAdminClient();
// @ts-expect-error - Next.js 15 types issue
const sessionId = await cookies().get("auth_session")?.value;
if (!sessionId) {
return null;
}
const user = await account.get();
return user;
} catch (error) {
console.error('Error fetching user:', error);
return null;
}
} ``` This its my code its good?

I need to create a new client

?

Yes, you'll need an admin client and a session client.

The session client acts on behalf of a user.

I highly recommend looking at this tutorial Guille and I have provided.

Thanks so much!!, Im still new in appwrite
Recommended threads
- Cannot access my Appwrite database on th...
Hello guys, I am currently developing a web app and using appwrite for authentication, database, storage and messaging. It was working well but since yesterday...
- Nuxt Middleware Support
So I'm trying to protect some routes so that the user is redirected back to the login screen should they not have an active session. However, I'm stuck in a lo...
- Sites Problem
Hi, I keep getting this problem when deploying my site. I have already made sure the site is active which it is. It even shows the preview of my site but when ...
