Authentication and Authorization Middleware

Batman added middleware to the Robyn application to verify the JWT tokens and to restrict access to certain endpoints based on the user's role.

Setting up Authentication Middlewares

from robyn.authentication import AuthenticationHandler, BearerGetter, Identity

class BasicAuthHandler(AuthenticationHandler):
    def authenticate(self, request: Request):
        token = self.token_getter.get_token(request)

            payload = crud.decode_access_token(token)
            username = payload["sub"]
        except Exception:

        with SessionLocal() as db:
            user = crud.get_user_by_username(db, username=username)

        return Identity(claims={"user": f"{ user }"})


@app.get("/users/me", auth_required=True)
async def get_current_user(request):
    user =["user"]
    return user

With the web application in place, the Gotham City Police Department could now efficiently manage crime data and track criminal activities in real-time. Batman had successfully used the Robyn web framework to build a real-world application to help fight crime in Gotham City.