Back

Share session between client and server when using OTP auth

  • 0
  • Self Hosted
  • Auth
  • Web
Zoker
27 Jul, 2024, 22:38

Hi there,

I'm using the client sdk to authenticate the user within my nextjs frontend. I also need to access the user object, so I also use the server sdk. I followed these two guides to set it up:

I use the OTP function for authentication (the one, where the user gets a one time password send via mail). I'm still having trouble to create a server session next to the client session.

I use this code:

TypeScript
app/page/login/page.tsx

const session = await account.createSession(userId, otp);
await fetch('/api/login', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({userId, otp}),
});

And I do this on the server side:

TypeScript
app/api/login/route.ts

export async function POST(request: NextRequest){
  const { account } = await createAdminClient();

  const { userId, otp } = await request.json();
  const session = await account.createSession(userId, otp);

I always get the error: Invalid token passed in the request.

So now my question is: How can I share the session between client and server when using the OTP for auth? Thanks!

TL;DR
Developers having trouble sharing a session between client and server when using OTP for authentication. The issue arises from invalid token being passed in the request. Solution: Check the token generation and compare it between client and server. Make sure they match in order to successfully share the session.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more