diff --git a/home/settings/base.py b/home/settings/base.py
index 3403e8f9..bd00f16d 100644
--- a/home/settings/base.py
+++ b/home/settings/base.py
@@ -88,6 +88,7 @@
REST_AUTH_SERIALIZERS = {
'USER_DETAILS_SERIALIZER': 'users.serializers.UserSerializer',
+ 'TOKEN_SERIALIZER': 'users.serializers.TokenSerializer'
}
REST_AUTH_REGISTER_SERIALIZERS = {
diff --git a/home/urls.py b/home/urls.py
index 52573fb9..b66bf05d 100644
--- a/home/urls.py
+++ b/home/urls.py
@@ -7,5 +7,6 @@
path('rest-auth/', include('rest_auth.urls')),
path('rest-auth/registration/', include('rest_auth.registration.urls')),
path('admin/', admin.site.urls),
+ path('users/', include('users.urls')),
re_path(r'^.*', TemplateView.as_view(template_name='index.html')),
]
diff --git a/src/containers/Layout.js b/src/containers/Layout.js
index 52f1a097..8b418747 100644
--- a/src/containers/Layout.js
+++ b/src/containers/Layout.js
@@ -35,7 +35,7 @@ class CustomLayout extends React.Component {
Home
- List
+ Profile
@@ -50,6 +50,12 @@ class CustomLayout extends React.Component {
}
}
+const mapStateToProps = state => {
+ return {
+ userId: state.auth.userId
+ };
+};
+
const mapDispatchToProps = dispatch => {
return {
logout: () => dispatch(actions.logout())
@@ -58,7 +64,7 @@ const mapDispatchToProps = dispatch => {
export default withRouter(
connect(
- null,
+ mapStateToProps,
mapDispatchToProps
)(CustomLayout)
);
diff --git a/src/containers/Profile.js b/src/containers/Profile.js
new file mode 100644
index 00000000..6acc52d4
--- /dev/null
+++ b/src/containers/Profile.js
@@ -0,0 +1,23 @@
+import React from "react";
+import { connect } from "react-redux";
+
+class Profile extends React.PureComponent {
+ render() {
+ return
Hi {this.props.username}
;
+ }
+}
+
+const mapStateToProps = state => {
+ return {
+ token: state.auth.token,
+ username: state.auth.username
+ };
+};
+
+const mapDispatchToProps = dispatch => {
+ return {
+ //
+ };
+};
+
+export default connect(mapStateToProps)(Profile);
diff --git a/src/routes.js b/src/routes.js
index d17c7c7c..8cefb811 100644
--- a/src/routes.js
+++ b/src/routes.js
@@ -4,11 +4,13 @@ import Hoc from "./hoc/hoc";
import Login from "./containers/Login";
import Signup from "./containers/Signup";
+import Profile from "./containers/Profile";
const BaseRouter = () => (
+
);
diff --git a/src/store/actions/auth.js b/src/store/actions/auth.js
index be9e702e..0705f975 100644
--- a/src/store/actions/auth.js
+++ b/src/store/actions/auth.js
@@ -45,11 +45,16 @@ export const authLogin = (username, password) => {
password: password
})
.then(res => {
- const token = res.data.key;
- const expirationDate = new Date(new Date().getTime() + 3600 * 1000);
- localStorage.setItem("token", token);
- localStorage.setItem("expirationDate", expirationDate);
- dispatch(authSuccess(token));
+ const user = {
+ token: res.data.key,
+ username,
+ userId: res.data.user,
+ is_student: res.data.user_type.is_student,
+ is_teacher: res.data.user_type.is_teacher,
+ expirationDate: new Date(new Date().getTime() + 3600 * 1000)
+ };
+ localStorage.setItem("user", JSON.stringify(user));
+ dispatch(authSuccess(user));
dispatch(checkAuthTimeout(3600));
})
.catch(err => {
@@ -81,6 +86,7 @@ export const authSignup = (
const user = {
token: res.data.key,
username,
+ userId: res.data.user,
is_student,
is_teacher: !is_student,
expirationDate: new Date(new Date().getTime() + 3600 * 1000)
diff --git a/src/store/reducers/auth.js b/src/store/reducers/auth.js
index 7ad35d04..14725507 100644
--- a/src/store/reducers/auth.js
+++ b/src/store/reducers/auth.js
@@ -6,6 +6,7 @@ const initialState = {
username: null,
is_student: null,
is_teacher: null,
+ userId: null,
error: null,
loading: false
};
@@ -23,6 +24,7 @@ const authSuccess = (state, action) => {
username: action.user.username,
is_student: action.user.is_student,
is_teacher: action.user.is_teacher,
+ userId: action.user.userId,
error: null,
loading: false
});
diff --git a/users/admin.py b/users/admin.py
index c021d5b6..02838027 100644
--- a/users/admin.py
+++ b/users/admin.py
@@ -1,7 +1,31 @@
from django.contrib import admin
+from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import Group
from .models import User
-admin.site.register(User)
+
+class UserAdmin(BaseUserAdmin):
+ add_fieldsets = (
+ (None, {
+ 'fields': ('email', 'username', 'is_student', 'is_teacher', 'password1', 'password2')
+ }),
+ ('Permissions', {
+ 'fields': ('is_superuser', 'is_staff')
+ })
+ )
+ fieldsets = (
+ (None, {
+ 'fields': ('email', 'username', 'is_student', 'is_teacher', 'password')
+ }),
+ ('Permissions', {
+ 'fields': ('is_superuser', 'is_staff')
+ })
+ )
+ list_display = ['email', 'username', 'is_student', 'is_teacher']
+ search_fields = ('email', 'username')
+ ordering = ('email',)
+
+
+admin.site.register(User, UserAdmin)
admin.site.unregister(Group)
diff --git a/users/serializers.py b/users/serializers.py
index 2fb26fbb..1c9e91a3 100644
--- a/users/serializers.py
+++ b/users/serializers.py
@@ -1,7 +1,7 @@
from allauth.account.adapter import get_adapter
from rest_auth.registration.serializers import RegisterSerializer
from rest_framework import serializers
-
+from rest_framework.authtoken.models import Token
from .models import User
@@ -39,3 +39,22 @@ def save(self, request):
user.save()
adapter.save_user(request, user, self)
return user
+
+
+class TokenSerializer(serializers.ModelSerializer):
+ user_type = serializers.SerializerMethodField()
+
+ class Meta:
+ model = Token
+ fields = ('key', 'user', 'user_type')
+
+ def get_user_type(self, obj):
+ serializer_data = UserSerializer(
+ obj.user
+ ).data
+ is_student = serializer_data.get('is_student')
+ is_teacher = serializer_data.get('is_teacher')
+ return {
+ 'is_student': is_student,
+ 'is_teacher': is_teacher
+ }
diff --git a/users/urls.py b/users/urls.py
new file mode 100644
index 00000000..01591d8b
--- /dev/null
+++ b/users/urls.py
@@ -0,0 +1,6 @@
+from rest_framework.routers import DefaultRouter
+from .views import UserViewSet
+
+router = DefaultRouter()
+router.register(r'', UserViewSet, base_name='users')
+urlpatterns = router.urls
diff --git a/users/views.py b/users/views.py
index 91ea44a2..72d4615a 100644
--- a/users/views.py
+++ b/users/views.py
@@ -1,3 +1,10 @@
+from rest_framework import viewsets
from django.shortcuts import render
-# Create your views here.
+from .models import User
+from .serializers import UserSerializer
+
+
+class UserViewSet(viewsets.ModelViewSet):
+ serializer_class = UserSerializer
+ queryset = User.objects.all()