https://github.com/jamstooks/drf-redux-auth
Async React/Redux Authentication library for Django Rest Framework Token Authentication
https://github.com/jamstooks/drf-redux-auth
auth django react redux
Last synced: 28 days ago
JSON representation
Async React/Redux Authentication library for Django Rest Framework Token Authentication
- Host: GitHub
- URL: https://github.com/jamstooks/drf-redux-auth
- Owner: jamstooks
- Created: 2018-07-04T02:00:01.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-08-14T13:53:09.000Z (over 6 years ago)
- Last Synced: 2025-03-24T11:45:13.395Z (about 1 month ago)
- Topics: auth, django, react, redux
- Language: JavaScript
- Homepage:
- Size: 76.2 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Django Rest Framework Authentication for React and Redux
[](https://travis-ci.org/jamstooks/drf-redux-auth)
Provides actions, reducers, and components to authenticate with
django using Django Rest Framework's
[Token Authentication](http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication)[Demo Available](https://github.com/jamstooks/drf-redux-auth-demo)
## Install
npm install --save drf-redux-auth
## Setup
Setup should be simple. Just add the reducers where you `combineReducers`:
import { authReducer } from "drf-redux-auth";
const rootReducer = combineReducers({
...
auth: authReducer,
...
});
and provide your actions to your components in your containers:import { loginUser, logoutUser } from "drf-redux-auth";
...
const mapDispatchToProps = dispatch => ({
...
logout: () => dispatch(logoutUser()),
login: creds => dispatch(loginUser(creds)),
...
});
Note: this currently relies on an environment variable,
`REACT_APP_AUTH_URL` for the DRF URL for now.
## State ShapeThe state provided by this libary is the following:
{
username,
token,
isAuthenticated: ,
isFetching: ,
errorMessage
}
The token and username are also stored in `localStorage`
to persist auth status past reload.
## ComponentsTwo extremely basic components are available:
import { AuthStatus, Login } from "drf-redux-auth";
You'll want to write your own, but they're useful for reference.
Another reference component can be seen in the
[demo](https://github.com/jamstooks/drf-redux-auth-demo).## Django Rest Framework
Enabling token auth is easy. Just add to `INSTALLED_APPS`:
'rest_framework',
'rest_framework.authtoken',and update your `urls.py`:
from rest_framework.authtoken.views import obtain_auth_token
urlpatterns = [
...
path('api/token-auth/', obtain_auth_token),
]
## References### Implementation Design
- [Redux Middleware](https://redux.js.org/advanced/middleware)
- [Auth0 JWT Authentication](https://auth0.com/blog/secure-your-react-and-redux-app-with-jwt-authentication/)
- [django-react-auth](https://github.com/geezhawk/django-react-auth)### Packaging Decisions
- [bookercodes](https://github.com/bookercodes/articles/blob/master/how-to-build-and-publish-es6-npm-modules-today-with-babel.md)