Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement Role Based Authentication with JWT in Python

I am looking to implement a role based authentication in a Vue/FastApi application. I come from a background of using Web Forms in asp.net and it was fairly simple to hide and show certain forms depending on if the user is an Admin, or a Manager, or Employee etc. Is there a way to do this with Vue/FastApi with JWT?

like image 996
austin_wilcox21 Avatar asked Nov 18 '25 09:11

austin_wilcox21


1 Answers

You will have to split the authentication in two:

  • Authentication via Vuejs. This is independent from fastapi. The only shared thing will be the fact that upon login, vue will authenticate to fastapi (like a man in the middle that forwards information). Vue will then cache the received JWT token (or whatever token received) and keep it in a session (so that in case of page reload the user does not have to login again). As soon as the user logs out, erase the token from the vue app.

  • Authentication via FastAPI. Here, you simply authenticate with username and password, get a JWT token as response (or any other token you want) and use such token for the following requests.

Basically, once vue receives user - password, it will authenticate to the fastapi api, get the token and store it somewhere (this depends on how you implement it and on vuejs, I haven't used so I can't say anything).

Connecting to your background, the .net form equivalent would be the vue login form, but I recommend you to first follow some tutorials on vuejs in order to first understand how it works and have a proper understanding of how it works and which are some possibilities for achieving something.

like image 185
lsabi Avatar answered Nov 21 '25 07:11

lsabi