How do I authenticate users with Django REST framework and React.js frontend?

别说谁变了你拦得住时间么 提交于 2020-06-27 15:09:11

问题


I am making a website with django rest in the backend and react.js in the frontend. This will be my first time using a front end framework with django. I am using django-allauth for social auth on the backend (facebook/twitter/google).

Once the user is signed in through a web session (using allauth) via a 3rd party provider (facebook) how do I pass that data to my react app?

Do I grab the facebook token, authenticate it, then generate an oauth2/jwt token and store that somehow? Is there some boilerplate/code that will save me time on this. I don't want to reinvent the wheel here.

Many thanks!


回答1:


After you get the token, you can store it in cookies(or keychain if you developing an IOS app) than put the token in your request header then make a request to your server like:

curl -H "Authorization: bearer your_access_token" -A "ChangeMeClient/0.1 by YourUsername" http://your_server/your_api

This may help Authorizing a request




回答2:


This is an old question, but I thought I'd chime in for anyone reading this now...

As long as you set up rest_framework.authtoken you should be able to authenticate with DRF with a variety of backends.

If you're using Redux, you might consider a simple npm package that does all the groundwork already: drf-redux-auth. At the very least, it might provide the "boilerplate" you're looking for.



来源:https://stackoverflow.com/questions/39401787/how-do-i-authenticate-users-with-django-rest-framework-and-react-js-frontend

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