#Server-side sessions VS. Client-side sessions

15 messages · Page 1 of 1 (latest)

naive stone
#

I am confused about creating a session on the server side vs. creating a session on the client side. I successfully create a session on the server side via my web interface, I get code 200, and log the relevant info in both browser console and Appwrite's Exevution logs. But when I refresh the Sessions tab for my user, the tab is empty. Where is the session being created and why is it not listed in the Session's tab?

acoustic atlas
#

Testing this myself with NextJS and creating a server side session works just fine and logs it in the sessions tab for the user. What does your code look like that is creating the session?

naive stone
#

this was the code:

import { Client, Users, Query } from 'node-appwrite';

// This Appwrite function will be executed every time your function is triggered
export default async ({ req, res, log, error }) => {
  // You can use the Appwrite SDK to interact with other services
  // For this example, we're using the Users service
  const client = new Client()
    .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT)
    .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
    .setKey(process.env.VITE_MY_API_KEYS);


  const users = new Users(client);
  let userSessions = null;

  try {

    if (!req.body) {
      throw new Error('Request body is missing.');
    }

    const data = JSON.parse(req.body);

    if (!data.email) {
      throw new Error('Email not provided.');
    }

    const response = await users.list([Query.equal('email', data.email)]);

    userSessions = await users.listSessions(response.users[0].$id);

    // Log messages and errors to the Appwrite Console
    // These logs won't be seen by your end users

     if (userSessions.total === 0) {
       log('Creating session');
       userSessions = await users.createSession(response.users[0].$id);
     } else {
       log(`ALREADY EXISTS: userSessions for ${response.users[0].email}: ${JSON.stringify(userSessions)}`);
     }

     log(`userSessions for ${response.users[0].email}: ${JSON.stringify(userSessions)}`);


  } catch (err) {
    error("Could not process: " + err.message);
  }


  return res.json(userSessions);
};

This used to be my code. But since the sessions would not show up in the Sessions tab, I modified it so now it only lists the active sessions sends the result to the client, and I start the session on the client-side.

Please let me know if my original approach was problematice.
@acoustic atlas

acoustic atlas
#

What are you trying to accomplish?

#

Are you trying to authenticate a user?

naive stone
#

When the user is a returnee and have not ended their session manually, I wanted to check if they have an active session. if not, start a new one.

#

@acoustic atlas

acoustic atlas
naive stone
#

account.get() always gives me this error:

    at _Client```
I do not know how to work with `account.get()`.
#

@acoustic atlas

naive stone
#

I mean it gives me that error when I do not have an active session going. The function that I created on the server side, checks for the active session before attempting to get details.

proper coral
#

@naive stone I am wondering if you got solution for this. I would love to be helped.

acoustic atlas
naive stone
naive stone