Back

Nitro middleware for accessing logged user information

  • 0
  • Accounts
  • Web
andrei
7 Jul, 2023, 10:09

First I am not sure how to send the JWT to the nitro api, secondly I am not sure how to actually create the client, is it good practice to create it in a middleware? wouldn't that slow everything down?

TypeScript
export default defineEventHandler((event) => {
  event.context.appwrite = new sdk.Client()    
    .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setJWT(jwt); // Your secret JSON Web 
  event.context.account = new sdk.Account(event.context.appwrite);
  
})
TL;DR
The user is looking for guidance on using Nitro middleware to access logged user information. The best practice is to have a provider that holds all Appwrite services. To get a JWT (JSON Web Token) with Nitro, use the Appwrite client-side JavaScript SDK. Here's how to get the JWT on the server-side: [link]. If not using the client-side SDK, the JWT needs to be sent from the client to the backend on each request. The user is unsure of how to send the JWT to the Nitro API and how to create the client. They're asking if it's good practice to create it in a
Binyamin
7 Jul, 2023, 13:57

Best practice would be to have some provider that will hold all of your Appwrite services.

As for nitro, the only way to get JWT is with Appwrite client side js SDK. https://appwrite.io/docs/client/account?sdk=web-default#accountCreateJWT

Check this to see how to get JWT in server side - https://discord.com/channels/564160730845151244/1123704308966379552/1123972307057119273

Otherwise you'll need to send the JWT from the client side to backend one each request.

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