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: [email protected] (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
- can someone explain the custom domain is...
- Running Appwrite + several clients in a ...
Hi all 👋 My next project is a bit of a special one. Before I start diving into development, I need to make sure that I will be able to achieve this with Appwr...
- New user onboarding page
I am having a react vite web app (CSR), I want to show a welcome card or interface for the first time login only for a user. I am using a local flag for the fir...