Hey all, in my project I needed to have different session limits per user. So with the advice of Matej, I created a collection that stores attributes userId and limit.
Then I created a function that fired on users..sessions..create
It checks if a user's session has gone over their limit. Right now if that is true, it will delete the session. The issue is I need to send the response to the client with the error message, with the idea that I can deny the login request.
I was looking through the documentation, I'm not sure how to send a message back to the client manually. I think as an API this the things like res.json({}) are supposed to be returned?
I think a workaround, if I can send any type of message to the client I could have them listen and then fire the logout() that way.
The client is using appwrite web sdk and svelte
Here is my function:
module.exports = async function (req, res) {
if (req.headers != 'website') {
const client = new sdk.Client();
const database = new sdk.Databases(client);
client
.setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
.setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
.setKey(req.variables['APPWRITE_FUNCTION_API_KEY']);
const users = new sdk.Users(client);
const docs = database.listDocuments('640c0c2b8e955c6e66ae', '640c0c315d02c25dc798'); // database ID, collection ID
let data = null;
let userId = null;
try {
data = JSON.parse(req.variables.APPWRITE_FUNCTION_EVENT_DATA);
userId = data.userId;
} catch (err) {
res.json({'error': err.toString()});
}
docs.then(function (documents) {
// let id = result.documents[0].userId
// let limit = result.documents[0].limit
let doc = null;
for (let i = 0; i < documents.documents.length; i++) {
if (documents.documents[i].userId == userId) {
doc = documents.documents[i];
break;
}
}
if (doc) {
const sessions = users.listSessions(userId);
sessions.then(function (userSessions) {
// res.json({documents, userSessions}); // Success
if (userSessions.total > 1) {
if (userSessions.total > doc.limit) {
res.send("user sessions over document limit");
// delete session
}
} else {
res.send("1 session");
}
}, function (err) {
res.json({'error': err.toString()})
});
}
}, function (error) {
res.json({'error': error.toString()})
});
}
};```
I would try with realtime and listen to the account event
There is no way to send information out
But, you do have few options to tackle this challenge
you can try to use AppWrite realtime like so (in case your function id is aaa111bbb
.
client.subscribe('functions.aaa111bbb.executions.*.update', response => {
// check the user login status for the session
});
But it's sound like to much resources for this task
oh like I would do that client-side?
This is one way, yes
Or you can give the user just reading access for the document length and listen to it in realtime So in that case the only the realtime will fire a new event is when is session got deleted
oh okay that is awesome, theoretically I could subscribe to the account or session client side? and if that changes, i can logout
Exactly
thanks so much guys 🙂
appwrite is amazing
Indeed
Recommended threads
- HTTP POST to function returning "No Appw...
Hi everyone, I’m running into an issue with my self-hosted Appwrite instance. I’ve set up my environment variables (APPWRITE_FUNCTION_PROJECT_ID, APPWRITE_FUNC...
- Can't add dart 3.5 runtime
Modified the `.env` to enable dart 3.5 runtime on my self-hosted instance but still can't find the runtime when creating a new function. I manually pulled the i...
- How to verify an user using AppWrite Fun...
I have seen similar questions but none whose solutions serve me. I have a function to verify a user with their secret and their id: https://blahblah.appwrite.gl...