Hey ya
I’ve been implementing sanic-jwt around my api end points and have the initial authentication working against the user records in my mongodb. I think i’ve probably set something up wrong though (I’m a python amateur and reasonably new to sanic), as if i try auth/me
i’m not able to retrieve the user info as there is no user_id
in the payload. Its value is just None.
I’m initialising here:
initialize_jwt(app, authenticate=authenticate, retrieve_user=retrieve_user, access_token_name='token')
Authentication:
async def authenticate(request, *args, **kwargs):
username = request.json.get("username", None)
password = request.json.get("password", None)
if not username or not password:
raise exceptions.AuthenticationFailed("Missing username or password.")
user_doc = await request.app.db['users'].find(
{"contact_details.email": username}).to_list(None)
if len(user_doc) == 0:
raise exceptions.AuthenticationFailed("User not found.")
if not validate_password(password, user_doc[0]['password']):
raise exceptions.AuthenticationFailed("Password is incorrect.")
return user_doc[0]
Retrieve user:
async def retrieve_user(request, payload, *args, **kwargs):
if payload:
user = await request.app.db['users'].find(
{"_id": payload['user_id']}).to_list(None)
return user[0]
else:
return None
Once i get this working i want to add scopes (which i currently think will suffer the same issue i have here currently).
Thanks in advance for any guidance/pointers