Skip to content

Commit

Permalink
user type and id stored in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfreire committed Dec 11, 2018
1 parent 272f61a commit 174ae28
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 10 deletions.
1 change: 1 addition & 0 deletions home/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@

REST_AUTH_SERIALIZERS = {
'USER_DETAILS_SERIALIZER': 'users.serializers.UserSerializer',
'TOKEN_SERIALIZER': 'users.serializers.TokenSerializer'
}

REST_AUTH_REGISTER_SERIALIZERS = {
Expand Down
1 change: 1 addition & 0 deletions home/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
]
10 changes: 8 additions & 2 deletions src/containers/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CustomLayout extends React.Component {
<Link to="/">Home</Link>
</Breadcrumb.Item>
<Breadcrumb.Item>
<Link to="/">List</Link>
<Link to={`/profiles/${this.props.userId}`}>Profile</Link>
</Breadcrumb.Item>
</Breadcrumb>
<div style={{ background: "#fff", padding: 24, minHeight: 280 }}>
Expand All @@ -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())
Expand All @@ -58,7 +64,7 @@ const mapDispatchToProps = dispatch => {

export default withRouter(
connect(
null,
mapStateToProps,
mapDispatchToProps
)(CustomLayout)
);
23 changes: 23 additions & 0 deletions src/containers/Profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from "react";
import { connect } from "react-redux";

class Profile extends React.PureComponent {
render() {
return <div>Hi {this.props.username}</div>;
}
}

const mapStateToProps = state => {
return {
token: state.auth.token,
username: state.auth.username
};
};

const mapDispatchToProps = dispatch => {
return {
//
};
};

export default connect(mapStateToProps)(Profile);
2 changes: 2 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => (
<Hoc>
<Route exact path="/login/" component={Login} />
<Route exact path="/signup/" component={Signup} />
<Route exact path="/profile/:id" component={Profile} />
</Hoc>
);

Expand Down
16 changes: 11 additions & 5 deletions src/store/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/store/reducers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const initialState = {
username: null,
is_student: null,
is_teacher: null,
userId: null,
error: null,
loading: false
};
Expand All @@ -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
});
Expand Down
26 changes: 25 additions & 1 deletion users/admin.py
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 20 additions & 1 deletion users/serializers.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions users/urls.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion users/views.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 174ae28

Please sign in to comment.