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
- Function permissions
In my app I have a CreateUser flow which makes several documents and at the end uses the functions.createExecution() to call a server-side function to create a ...
- The file size is either not valid or exc...
Hello, I am receiving the following error when I am trying to deploy a function from my local. ``` > appwrite push functions --function-id xxxxxxxxx ℹ Info: Va...
- How does sending email from the Appwrite...
I noticed that the pricing page mentions “Messages – 1000 per month” for the Free plan. Is this different from sending emails? When I try to send an email usin...