Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#327 show login messages from server, remove old logo from login #329

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2023, PKPDApp
Copyright (c) 2024, PKPDApp
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
15 changes: 13 additions & 2 deletions frontend-v2/src/features/login/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,20 @@ const Login: React.FC<LoginProps> = ({ onLogin, isLoading, errorMessage }) => {
<form onSubmit={handleSubmit(onSubmit)}>
<Stack spacing={2} sx={{ marginTop: 10 }}>
<Box display="flex" justifyContent="center" alignItems="center">
<PkpdAppIcon style={{ width: 250 }} />
<Typography
variant="h3"
component="div"
sx={{
color: "#1976d2",
fontWeight: "bold",
paddingLeft: "1rem",
fontFamily: "Comfortaa",
}}
>
pkpd explorer
</Typography>
</Box>
<Typography variant="h5">Login</Typography>
<Typography variant="h6">Login</Typography>
<TextField
label="Username"
name="username"
Expand Down
20 changes: 18 additions & 2 deletions frontend-v2/src/features/login/loginSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,27 @@ export const fetchSession = createAsyncThunk<
return sessionResponse;
});

class ServerError extends Error {
constructor(message: string) {
super(message)
this.name = "ServerError";
}
}
function isResponseOk(response: Response) {
if (response.status >= 200 && response.status <= 299) {
return response.json();
} else {
throw Error(response.statusText);
return response.json()
.then((data) => {
throw new ServerError(data.detail);
})
.catch((err) => {
if (err instanceof ServerError) {
throw err;
} else {
throw Error(response.statusText);
}
});
}
}

Expand Down Expand Up @@ -80,7 +96,7 @@ export const login = createAsyncThunk<
return { isAuthenticated: true, user: data.user };
})
.catch((err) => {
return rejectWithValue({ error: err.message });
return rejectWithValue({ error: err.message});
});
return response;
},
Expand Down
46 changes: 24 additions & 22 deletions pkpdapp/pkpdapp/api/views/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,53 @@
from django.middleware.csrf import get_token
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.decorators.http import require_POST
from rest_framework.authentication import (
SessionAuthentication, BasicAuthentication
)
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from pkpdapp.api.serializers import UserSerializer


def get_csrf(request):
response = JsonResponse({
'X-CSRFToken': get_token(request),
'detail': 'CSRF cookie set'
})
response = JsonResponse(
{"X-CSRFToken": get_token(request), "detail": "CSRF cookie set"}
)
return response


@ensure_csrf_cookie
@require_POST
def login_view(request):
data = json.loads(request.body)
username = data.get('username')
password = data.get('password')
username = data.get("username")
password = data.get("password")
if username is None or password is None:
return JsonResponse({
'detail': 'Please provide username and password.'
}, status=400)
return JsonResponse(
{"detail": "Please provide username and password."}, status=400
)

user = authenticate(username=username, password=password)
if user is None:
return JsonResponse({'detail': 'Invalid credentials.'}, status=400)
return JsonResponse(
{
"detail": "Invalid credentials. Either you have supplied an incorrect username/password combination, or you do not have sufficient access" # noqa E501
},
status=400,
)

login(request, user)

return JsonResponse({
'user': UserSerializer(user).data,
'detail': 'Successfully logged in.'
})
return JsonResponse(
{"user": UserSerializer(user).data, "detail": "Successfully logged in."}
)


@ensure_csrf_cookie
def logout_view(request):
if not request.user.is_authenticated:
return JsonResponse({'detail': 'You\'re not logged in.'}, status=400)
return JsonResponse({"detail": "You're not logged in."}, status=400)

logout(request)
return JsonResponse({'detail': 'Successfully logged out.'})
return JsonResponse({"detail": "Successfully logged out."})


class SessionView(APIView):
Expand All @@ -64,8 +65,9 @@ class SessionView(APIView):

@staticmethod
def get(request, format=None):
return JsonResponse({'isAuthenticated': True, 'user':
UserSerializer(request.user).data})
return JsonResponse(
{"isAuthenticated": True, "user": UserSerializer(request.user).data}
)


class WhoAmIView(APIView):
Expand All @@ -74,4 +76,4 @@ class WhoAmIView(APIView):

@staticmethod
def get(request, format=None):
return JsonResponse({'user': request.user})
return JsonResponse({"user": request.user})
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ django-extensions>=3.1.1
jsonfield>=3.1.0
djangorestframework>=3.12.4
djoser>=2.1.0
python-memcached>=1.16.0
python-memcached>=1.16,<=1.59
psycopg2-binary>=2.9.1
dj-database-url>=0.5.0
django-polymorphic>=3.1.0
Expand Down
Loading