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?
#Server-side sessions VS. Client-side sessions
15 messages · Page 1 of 1 (latest)
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?
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
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
I don't know if I'd really do this tbh, I would use Appwrites built in account.get() function to check for an active session, if it's not active prompt them to reauthenticate.
account.get() always gives me this error:
at _Client```
I do not know how to work with `account.get()`.
@acoustic atlas
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.
@naive stone I am wondering if you got solution for this. I would love to be helped.
Use the create sessions functions to create a session, set it in the cookies on the server, then create a session that utilizes the session token you set in the cookies so it acts on behalf of the user. Then you can use account.get() to see if the user is logged in, if that method throws then the user is not logged in
I just resorted to creating the session on the client side. I no longer use the serve-side to session creation. Thank you very much for asking. 🙏
Now, I create the session on the client-side and check and use .get() to perform the check. It works as I intended.