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
- Invalid `userId` param: Parameter must c...
Error: Invalid `userId` param: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can't start with a speci...
- "The document data is missing. Try again...
Hello, I am getting this error suddenly. I have been using this service for a year now, and I haven't made any changes to my code since then. My app was workin...
- Error: The document data is missing. Try...
I am not able to create any document on some of the collection/ DBs. As I can see many of us facing the same issue, need a quick resolution from the Appwrite Te...
