Remove auth tokens from db on logout & after end of validity
Currently the auth tokens just linger in the database forever. This should be changed in two ways:
- If a user clicks "logout", an api call should be made that removes this login token from the database
- A periodic task should be added that removes all expired auth tokens from the database
Hi. I would like to contribute and fix the listed issue
Hey, great to hear that. Some pointers that might be useful for implementing this. Feel free to implement it in some other way if you think that would work better. If you have any more questions, don't hesitate to ask anything here.
For the first point you need to implement a new endpoint in this file: https://github.com/bugbakery/transcribee/blob/bffc574a2730f72e87c254e9a18d0351cffd4dca/backend/transcribee_backend/routers/user.py I'd suggest to write one that takes a UserToken as a dependency (like https://github.com/bugbakery/transcribee/blob/bffc574a2730f72e87c254e9a18d0351cffd4dca/backend/transcribee_backend/routers/user.py#L46 does) and then just deletes this token from the database. To call it from the frontend, you need to regenerate the openapi docs, then add the endpoint here https://github.com/bugbakery/transcribee/blob/main/frontend/src/api/user.ts and then call it when clicking on the logout button.
For the second point, you need to add a new function that simply removes all expired auth tokens, similar to https://github.com/bugbakery/transcribee/blob/bffc574a2730f72e87c254e9a18d0351cffd4dca/backend/transcribee_backend/helpers/tasks.py#L56C1-L56C1 and then register it in the main.py: https://github.com/bugbakery/transcribee/blob/bffc574a2730f72e87c254e9a18d0351cffd4dca/backend/transcribee_backend/main.py#L41
Thanks. Will let you know when ready or if have question/comments
hi, I implemented this according to a template
// Assuming you have a function that sends an API request to the server
// Function to logout and remove the auth token from the database
function logout() {
let token = getAuthToken();
sendApiRequest("/logout", {token: token}, (response) => {
// Handle the response from the server
if (response.success) {
localStorage.removeItem("authToken");
// Redirect to the login page or show a message
window.location.href = "/login";
} else {
alert(response.message);
}
});
}
function checkTokenValidity() {
let token = getAuthToken();
sendApiRequest("/checkTokenValidity", {token: token}, (response) => {
if (response.success) {
// If the token is valid, do nothing
} else {
// If the token is invalid, logout and remove it from the database
logout();
}
});
}
// Set an interval to run the checkTokenValidity function every minute (or any other time period)
setInterval(checkTokenValidity, 60000); // 1 minute
@MJAlaeddini I've hidden your response in this thread as the code you posted solves none of the problems named in this issue, it's not even in the right area
I have created a draft MR https://github.com/bugbakery/transcribee/pull/285
Still need to add a periodic task. How to regenerate the openapi docs?
You can regenerate them by running
pdm run -p backend/ generate_openapi --file openapi-schema.yml
pnpm --prefix frontend/ run generate-openapi
or by running our pre commit hook: pre-commit run --all-files
Thanks. Before finalizing my MR I want to share some feedback:
- I was not able to run the project locally: not with docker not with nix.
With nix: Retry failed jobs ✖ Update click None -> 8.1.3 failed ✖ Update pyyaml None -> 6.0 failed ✖ Update typing-extensions None -> 4.5.0 failed ✖ Install automerge 0.0.1 failed
With Docker was error that app/dev.sh not found. I fixed it by copying dev sh in docker file COPY packaging/dev.sh /app/
I have updated the MR. I am not that strong with the FE so would like you to review and comment on how to properly make a request to the BE logout.
@pajowu I have left some comments above. I have another one: Why did you add a task 2 times? https://github.com/bugbakery/transcribee/blob/bffc574a2730f72e87c254e9a18d0351cffd4dca/backend/transcribee_backend/helpers/tasks.py#L39
Very nice PR, thanks a lot. I just added a few small review comments, but it looks good in general
The fronend part looks good to me (but also isn't my biggest strength, so maybe the other will have an opinion as well).
The double session.add is a nice find. I don't think this was on purpose, but just an artifact from updating some code. It should be safe to delete that
@pajowu I have updated MR following your comments. Please review the MR