transcribee icon indicating copy to clipboard operation
transcribee copied to clipboard

Remove auth tokens from db on logout & after end of validity

Open pajowu opened this issue 3 years ago • 13 comments

Currently the auth tokens just linger in the database forever. This should be changed in two ways:

  1. If a user clicks "logout", an api call should be made that removes this login token from the database
  2. A periodic task should be added that removes all expired auth tokens from the database

pajowu avatar Jun 26 '23 22:06 pajowu

Hi. I would like to contribute and fix the listed issue

voronov007 avatar Jun 27 '23 09:06 voronov007

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

pajowu avatar Jun 27 '23 10:06 pajowu

Thanks. Will let you know when ready or if have question/comments

voronov007 avatar Jun 27 '23 11:06 voronov007

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 avatar Jun 27 '23 11:06 MJAlaeddini

@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

pajowu avatar Jun 27 '23 11:06 pajowu

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?

voronov007 avatar Jun 27 '23 12:06 voronov007

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

pajowu avatar Jun 27 '23 12:06 pajowu

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/

voronov007 avatar Jun 27 '23 13:06 voronov007

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.

voronov007 avatar Jun 27 '23 14:06 voronov007

@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

voronov007 avatar Jun 27 '23 14:06 voronov007

Very nice PR, thanks a lot. I just added a few small review comments, but it looks good in general

pajowu avatar Jun 27 '23 17:06 pajowu

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 avatar Jun 27 '23 17:06 pajowu

@pajowu I have updated MR following your comments. Please review the MR

voronov007 avatar Jun 28 '23 08:06 voronov007