Skip to content

Commit

Permalink
User serializers/views added, including registration view
Browse files Browse the repository at this point in the history
  • Loading branch information
william-herring committed Jan 4, 2022
1 parent 8834251 commit 1368a15
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
13 changes: 2 additions & 11 deletions client/lib/forms/login_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ class _LoginFormState extends State<LoginForm> {
autocorrect: false,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter a valid username";
return "Required field.";
}

if (value.isNotEmpty) {
setState(() {
userInput = value;
});
}

return "Please enter a valid username";
},
textAlign: TextAlign.center,
decoration: InputDecoration(
Expand All @@ -63,21 +61,14 @@ class _LoginFormState extends State<LoginForm> {
),

),
Text(
"",
style: GoogleFonts.ubuntu(
fontSize: 11.0,
fontWeight: FontWeight.w500
)
),

TextFormField(
enableSuggestions: false,
autocorrect: false,
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter a valid password";
return "Required field.";
}

if (value.isNotEmpty) {
Expand Down
20 changes: 19 additions & 1 deletion server/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
from rest_framework.serializers import ModelSerializer
from rest_framework.serializers import ModelSerializer, CharField
from .models import Team
from django.contrib.auth.models import User


class UserSerializer(ModelSerializer):
password = CharField(write_only=True)

def create(self, validated_data):

user = User.objects.create_user(
username=validated_data['username'],
password=validated_data['password'],
)

return user

class Meta:
model = User
fields = ('id', 'username', 'password')


class TeamSerializer(ModelSerializer):
Expand Down
4 changes: 3 additions & 1 deletion server/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
path('create-team', CreateTeamView.as_view()),
path('get-team', GetTeamView.as_view()),
path('delete-team', DeleteTeamView.as_view()),
path('update-team', UpdateTeamView.as_view())
path('update-team', UpdateTeamView.as_view()),
path('user', UserView.as_view()),
path('create-user', CreateUserView.as_view())
]
18 changes: 16 additions & 2 deletions server/api/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
from rest_framework.generics import ListAPIView
from rest_framework.generics import ListAPIView, CreateAPIView
from rest_framework.views import APIView
from rest_framework import permissions
from .models import Team
from rest_framework.status import *
from rest_framework.response import Response

from django.contrib.auth.models import User
from .serializers import *


class UserView(ListAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer


class CreateUserView(CreateAPIView):
model = User
serializer_class = UserSerializer
permission_classes = [
permissions.AllowAny # This allows unauthenticated users to register an account
]


class TeamView(ListAPIView):
queryset = Team.objects.all()
serializer_class = TeamSerializer
Expand Down
Binary file modified server/db.sqlite3
Binary file not shown.
9 changes: 9 additions & 0 deletions server/server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'corsheaders',
'api.apps.ApiConfig',
'rest_framework',
'rest_framework.authtoken',
]


Expand All @@ -54,6 +55,14 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
]
}

CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOWED_ORIGINS = [
Expand Down

0 comments on commit 1368a15

Please sign in to comment.