Back

REST API AUTH

  • 0
  • REST API
Chris Nowicki
7 Jun, 2024, 18:27

We are playing around with REST API Authentication. I can successfully register a user and login a user. However when it comes time to get user account details (using REST) it asks for a JWT.

I tried the create JWT REST path and that gives me errors.

Not sure the correct flow for authenticating a user and storing session in browser.

Any help appreciated!

TL;DR
Developers are having trouble retrieving user account details with REST API Authentication. They are struggling to get the JWT for authentication. They need assistance in setting up the correct flow for authenticating a user and storing the session in the browser. Solution: Ensure the authentication flow is correctly implemented with functions like `getJWT` and `getAccount`. Double-check the request headers and endpoint paths for JWT retrieval and account details to ensure proper authentication.
Chris Nowicki
7 Jun, 2024, 18:27
TypeScript
export const getJWT = async () => {
  const jwt = await fetch(`https://cloud.appwrite.io/v1/account/jwt`, {
    method: 'POST',
    headers: {
      Host: 'cloud.appwrite.io',
      'Content-Type': 'application/json',
      'X-Appwrite-Response-Format': '1.5.0',
      'X-Appwrite-Project': projectId,
      'X-Appwrite-Key': apiKey,
    },
  })
    .then((response) => {
      console.log(response)
      return response.json();
    })
    .then((data) => {
      console.log('jwt: ', data.jwt);
      return data.jwt;
    })
    .catch((error) => {
      console.error('Error:', error);
    });

  return jwt;
};

export const getAccount = async () => {
  const jwt = await getJWT();

  const response = await fetch(`${url}/account`, {
    method: 'GET',
    headers: {
      Host: host,
      'Content-Type': 'application/json',
      'X-Appwrite-Response-Format': '1.5.0',
      'X-Appwrite-Project': projectId,
      'X-Appwrite-JWT': jwt,
    },
  })
    .then((response) => {
      return response.json();
    })
    .catch((error) => {
      console.error('Error:', error);
    });

  return response;
};
Steven
7 Jun, 2024, 18:29
Chris Nowicki
7 Jun, 2024, 18:36

@Steven ah, thanks! okay ... this is a really stupid question. How do I get the set-cookie from the header?

Chris Nowicki
7 Jun, 2024, 18:42

this is all that is returned in the response:

TypeScript
{
  '$id': '66635473eaa5e1f66046',
  '$createdAt': '2024-06-07T18:41:55.971+00:00',
  '$updatedAt': '2024-06-07T18:41:55.971+00:00',
  userId: '66634059b7c39f742502',
  expire: '2024-06-07T22:41:55.961+00:00',
  provider: 'email',
  providerUid: 'test@me.com',
  providerAccessToken: '',
  providerAccessTokenExpiry: '',
  providerRefreshToken: '',
  ip: '108.70.33.101',
  osCode: '',
  osName: '',
  osVersion: '',
  clientType: '',
  clientCode: '',
  clientName: '',
  clientVersion: '',
  clientEngine: '',
  clientEngineVersion: '',
  deviceName: '',
  deviceBrand: '',
  deviceModel: '',
  countryCode: 'us',
  countryName: 'United States',
  current: true,
  factors: [ 'password' ],
  secret: '',
  mfaUpdatedAt: ''
}
Chris Nowicki
7 Jun, 2024, 18:44

nm

Chris Nowicki
7 Jun, 2024, 18:44

found it

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