Back

Is there a way to get a JWT token to test with?

  • 0
  • Self Hosted
  • GraphQL
  • Web
caro
3 Sep, 2023, 00:49

Hello! I have some experience with auth0, but I'm trying out appwrite for a personal project. I'm creating a graphql API in nest and currently using the server SDK. What I'm trying to do is have my API create a user in appwrite while at the same time create a user in my postgres db. One thing I'm wondering is...how can I get a token to test this logic out with? In auth0, I know I'm able to go into their website and get a token to play around with. Is there a way to do this with appwrite?

TL;DR
The user wants to know if there is a way to get a JWT token to test with in Appwrite. They have tried using the `createJWT` function but it is returning an error. The solution is to first create a session using `createEmailSession` before calling `createJWT`. Additionally, an API Key may be needed for authentication.
Drake
3 Sep, 2023, 01:39
caro
3 Sep, 2023, 01:43

I’m trying to replicate the user experience though Apollo Studio. In Apollo Studio, I can set headers, so I’m attempting to use Bearer <token>

Drake
3 Sep, 2023, 01:58

you mean you want to make requests as an end user? You'd have to generate that JWT token client side using https://appwrite.io/docs/client/account?sdk=web-default#accountCreateJWT

caro
3 Sep, 2023, 02:03

ah when I do that, I get the following error: 'User (role: guests) missing scope (account). I’ve also tried using createEmailSession with a user that exists before running the createJWT() logic, but same error

Drake
3 Sep, 2023, 02:08

yes you definitely need a session before creating the JWT...where did you run create email session?

caro
3 Sep, 2023, 02:11

I have the following code:

TypeScript
import { Account, Client } from "appwrite";

const client = new Client()
  .setEndpoint("https://cloud.appwrite.io/v1")
  .setProject("<project_id>");

const account = new Account(client);

await account.createEmailSession("<email>", "<password>");

account.createJWT().then(
  function (response) {
    console.log(response);
  },
  function (error) {
    console.log(error);
  }
);
Drake
3 Sep, 2023, 02:26

where is this running?

caro
3 Sep, 2023, 02:26

this is just a script i created to run locally

Drake
3 Sep, 2023, 02:27

so this is a node script? this SDK is meant to be run in a browser because the session gets persisted in local storage or cookies. since that's not avaiable, the createJWT() call is unauthenticated

caro
3 Sep, 2023, 02:30

Yes, it’s a node script. Since this is meant to run in a browser, and I don’t yet have that built out yet (focusing on building the API first), is there another way for me to test out my logic?

Drake
3 Sep, 2023, 02:31

curl with cookies.

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