django rest framework social-auth with jwt

时间:2018-02-03 08:36:11

标签: django-rest-framework single-page-application django-authentication django-socialauth

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 个答案:

答案 0 :(得分:2)

OAuth 2.0工作流程采用rest-social-auth

  1. 前端需要了解每个社交提供商的以下参数:

    • client_id#仅适用于OAuth 2.0,社交服务提供商的注册应用程序ID
    • redirect_uri#到此网址社交提供商将重定向代码
    • scope = your_scope#例如电子邮件
    • response_type =所有oauth2.0提供商的代码#相同
  2. 用户确认。

  3. 社交提供商使用param代码重定向回redirect_uri。

  4. 前端现已准备好登录用户。为此,请使用提供者名称和代码发送POST请求:

  5. README

    这意味着,您应该在facebook developers中设置一个应用程序(让我们以Facebook为例),这里是教程How to Add Social Login to Django,您可以在其中找到如何设置Facebook应用并配置django_social。

    之后你会有facebook_id,redirect_uri为你的Facebook应用程序。 下一步是获取参数代码。您可以通过向网址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发送GET请求来获取参数代码(范围=电子邮件,如果您想获取用户电子邮件)

    如果一切正确,它会将您重定向到带有代码参数的redirect_uri(例如:http://YOUR_REDIRECT_URL/?code=90159c983d3bdc28c0PPzfSQB8LI49BYlPA6Vs

    所以,现在你有了代码,并准备好登录(注册)用户。只需将提供商名称和代码的POST请求发送到url / api / login / social / jwt /

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

    多数情况下,你会得到jwt令牌作为回应。