#Could not find user session: app.<project_id>@service.cloud.appwrite.io (role: applications)

41 messages · Page 1 of 1 (latest)

icy fjord
#

Hello,

Error : Could not find user session: app.66e034f7001f1afeb163@service.cloud.appwrite.io (role: applications) missing scope (account)

I have this error when i'm trying to check if i'm logged.

I have 2 functions : one to create my session and another to check if my user is logged.
I already tried to change roles in my appwrite.json and my session is created.

If someone has some tips or whatever, i'm free to speak about it.

Thanks.

slate grotto
left obsidian
#

Do not pass an API key in this case when you want to check an users session

icy fjord
#

My code :

#

try {
const client = new Client()
.setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT)
.setProject(process.env.APPWRITE_PROJECT_ID_KEY)
.setKey(process.env.APPWRITE_API_KEY);

const account = new Account(client);
await account.get();
left obsidian
#

You only authenticated as the API key here

#

You should either pass the users JWT or its session to the client

icy fjord
#

but without my API key i have an another error :

#

Could not find user session: User (role: guests) missing scope (account)

left obsidian
#

Because you are not authenticated

#

You should either be using setJWT() or setSession()

icy fjord
#

My code to create session :

#
      const client = new Client()
        .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT)
        .setProject(process.env.APPWRITE_PROJECT_ID_KEY)
        .setKey(process.env.APPWRITE_API_KEY);
      const account = new Account(client);
      const secretCode = req.bodyJson.secretCode.trim();
      const userId = req.bodyJson.userId.trim();
      const secret = secretCode;

      const sessionCookies = req.bodyJson.cookies.session; // Get the session cookie from the request
      if (sessionCookies) {
        client.setSession(sessionCookies);
      }

      await account.createSession(userId, secret);```
#

I logged my user by phone

left obsidian
#

Please use backticks to format your code

icy fjord
#

My bad

left obsidian
#

Three backticks at the start and end

left obsidian
icy fjord
#

The whole code :

if (
    req.bodyJson.path.trim() === '/auth/phone/code' &&
    req.bodyJson.isNewCode === false
  ) {
    try {
      const client = new Client()
        .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT)
        .setProject(process.env.APPWRITE_PROJECT_ID_KEY)
        .setKey(process.env.APPWRITE_API_KEY);
      const account = new Account(client);
      const secretCode = req.bodyJson.secretCode.trim();
      const userId = req.bodyJson.userId.trim();
      const secret = secretCode;

      const sessionCookies = req.bodyJson.cookies.session;
      if (sessionCookies) {
        client.setSession(sessionCookies);
      }

      await account.createSession(userId, secret);
    } catch (err) {
      return res.json(
        {
          path: '/auth/phone/code',
          error: err.message,
          statusCode: 401,
        },
        401,
        formattedAccess
      );
    }

    return res.json(
      {
        path: '/',
        statusCode: 200,
      },
      200,
      formattedAccess
    );
  }
#

This is my code to logged my user

#

This is my code to check if he's logged :

try {
    const client = new Client()
      .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT)
      .setProject(process.env.APPWRITE_PROJECT_ID_KEY);

    const account = new Account(client);
    await account.get();

    return res.json(
      {
        account: account.get(),
        statusCode: 200,
      },
      200,
      formattedAccess
    );
  } catch (err) {
    error('Could not find user session: ' + err.message);
    return res.json(
      {
        path: '/signin',
        statusCode: 401,
        message: 'Unauthorized',
      },
      401,
      formattedAccess
    );
  }
};
left obsidian
#

First code block looks fine but the second code block is missing setJWT() or setSession()

icy fjord
#

The setSession() is necessary ? Because if i already create the session, i don't need that, no ?

left obsidian
#

Because you are communicating with the appwrite server without passing any user credentials, so the server won't know who you are. In your previous attempt you passed the API key and that was also wrong

#

Can you explain in detail how your whole flow looks like? What frameworks you use and whether you use SSR or not

icy fjord
#

Okay, to explain my goal :

#

I have a web app with Vue 3, TS.

I logged my user by a phone number, if the user exists, he will be redirect on a page to enter a security code, which received on his phone, and if the code is right, he will be redirected on the dashboard.

Actually, i want to know if my user is logged and if he's not, he can't access to specials pages like dashboard, settings etc...

#

I have 2 functions in JS which are deploy on my AW console too

left obsidian
#

Actually, I just saw that you are never passing the session to the user.

#

You just do await account.createSession(userId, secret);

icy fjord
#

This is not enough with that ?

#

Because i saw that, on the phone auth doc

left obsidian
#

Are you ever passing a session token to the user so the user can authenticate from there on?

icy fjord
#

I pass a cookie which is his id

#
 const sessionCookies = req.bodyJson.cookies.session;

      if (sessionCookies) {
        client.setSession(sessionCookies);
      }
left obsidian
#

What id and what session? I thought that was an appwrite function to login?

icy fjord
#

User id and user session
Yep, this is my function pushed on my AW console

left obsidian
#

I gotta admit I'm terribly confused

#

I'll get back to this later, because I'm not sure if this approach really makes sense.