Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate jwt when signing in with allauth

How would you generate a token with django-rest-framework-jwt and pass it to a template that can store the token in localstorage, when signing in with allauth?

I know django-rest-framework-jwt lets you generate tokens via POST:

$ curl -X POST -d "username=admin&password=abc123" http://localhost:8000/api-token-auth/

But how would you implement this in the login/signup flow of allauth?

like image 304
2083 Avatar asked Nov 10 '14 19:11

2083


2 Answers

allauth is basically for multi page Apps, for API login i recommend relying on rest_auth Rest Auth Docs

It has built in Login/Register etc built on top of all-auth but for Single page apps.

then update settings, to

REST_USE_JWT = True

'DEFAULT_AUTHENTICATION_CLASSES': [
            'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
            'rest_framework.authentication.BasicAuthentication',

    ],

Now You should receive jwt token in response to login calls.

like image 22
Deepak Sharma Avatar answered Oct 28 '22 21:10

Deepak Sharma


(I have not used JWT but I don't believe there is anything special about JWT compared to regular tokens, other than the extra security and more importantly, not having to keep a database table of tokens. So, my answer is for regular tokens, assuming/hoping you can adjust to JWT)

I am assuming you are trying to write stand-alone client, in which case, the problem is that django-allauth is not really intended for use with cleints/APIs, so a lot of the magic cannot be used through an API. See this some how old issue, which I believe is still valid: 3rd party REST/JSON APIs.

If you scroll to the end, you will see somebody recommending the use of django-rest-auth to handle the social login for the API, while keeping the main django-allauth handing the native django web site side of things.

I have not yet used them both together (I am currently not supporting social login on the API side, so haven't had to deal with it).

This post shows an excellent example for developing an Angular Client using django-rest-framework. You will see how it creates its own APIs to registering and logging in. You should be able to replace that part with django-rest-auth, but the point is that django-allauth won't really play a big role on anything that comes via the API (unfortunately).

Finally, you may also want to check my own implementation here. Look at the 'authentication' app, and look at the tests for how is used, which is my version of link 3

like image 122
dkarchmer Avatar answered Oct 28 '22 19:10

dkarchmer