Understanding HEADLESS flow #4123
-
This is probably my fault for misunderstanding as I'm new to allauth. My goal is to provide an API with endpoints authenticated by allauth using Google OAuth2. At the moment I'm doing the following in my client:-
{
"provider": "google",
"process": "login",
"token": {
"id_token": "<token provided by Google>",
"client_id": "< client_id provided by config>"
}
{
"meta" : {
"is_authenticated" : true,
"session_token" : "<Session token>"
}
} Which is great, my user is logged in and has a session_token. So now I'd like them to call an API endpoint that only authenticated users can access. This is where I'm confused, how should I set my headers to use this token to authenticate the call? I tried setting X-Session-Token to but this did not work. Is it that I need to do some additional processing with the Token before processing the api call. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You've covered exactly the part that allauth considers in scope: authentication of the user. How to invoke your own APIs is something allauth puts no constraints/assumptions on, as that depends on what framework (e.g. django rest framework / ninja) you use, and also what authentication method you desire there.
engine = import_module(settings.SESSION_ENGINE)
session = engine.SessionStore(session_key=key)
user_id_str = session.get(SESSION_KEY)
if user_id_str:
user_id = get_user_model()._meta.pk.to_python(user_id_str)
user = get_user_model().objects.filter(pk=user_id).first()
if user and user.is_active:
return user The above code needs to be setup here https://www.django-rest-framework.org/api-guide/authentication/#custom-authentication or https://django-ninja.dev/guides/authentication/ |
Beta Was this translation helpful? Give feedback.
You've covered exactly the part that allauth considers in scope: authentication of the user. How to invoke your own APIs is something allauth puts no constraints/assumptions on, as that depends on what framework (e.g. django rest framework / ninja) you use, and also what authentication method you desire there.
settings.HEADLESS_TOKEN_STRATEGY
to point to a token strategy that creates such a token. Once you set that up, the token will appear in themeta
next to thesession_token
.