django rest framework social-auth with jwt

最后都变了- 提交于 2020-07-05 04:52:29

问题


hi im working on SPA django rest framework with jwt and angular 5 . i built login and sign up section. now im looking for a way to add social login or register to my app and have jwt support after deep search i found some module that have very unclear document(atleast to me).. this module https://github.com/st4lk/django-rest-social-auth looks ok he said use:

/api/login/social/jwt_user/

and:

/api/login/social/jwt/

are end point for jwt but this get me nothing when i'm using them..

we had some chat in slack but that was inconclusive anybody can explain solution to me and many developer like me that are confused about this issue ? which module should we use or if mentioned module is ok how should we use it


回答1:


There is OAuth 2.0 workflow with rest-social-auth

  1. Front-end need to know following params for each social provider:

    • client_id # only in case of OAuth 2.0, id of registered application on social service provider
    • redirect_uri # to this url social provider will redirect with code
    • scope=your_scope # for example email
    • response_type=code # same for all oauth2.0 providers
  2. User confirms.

  3. Social provider redirects back to redirect_uri with param code.

  4. Front-end now ready to login the user. To do it, send POST request with provider name and code:

README

it means, that you should set up an application (let's get a facebook as an example) in facebook developers, here is tutorial How to Add Social Login to Django where you can find how to set up facebook app and configurate django_social.

After that you will have client_id, redirect_uri for your facebook app. Next step is to get param code. You can get param code by making GET request to url https://www.facebook.com/v2.5/dialog/oauth?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&display=popup&scope=email (scope=email, if you want to get user email)

If everything is correct it will redirect you to your redirect_uri with code param (for example: http://YOUR_REDIRECT_URL/?code=90159c983d3bdc28c0PPzfSQB8LI49BYlPA6Vs)

So, now you have code, and you are ready to login (sign up) the user. Just send POST request with provider name and code to url /api/login/social/jwt/

{"code":"YOUR_CODE", "provider":"facebook"}

Thats all, you will get jwt token in response.



来源:https://stackoverflow.com/questions/48595507/django-rest-framework-social-auth-with-jwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!