Skip to content

Commit

Permalink
Don't use the login_required decorator
Browse files Browse the repository at this point in the history
This wasn't working well with when used over the API
  • Loading branch information
rolandgeider committed Aug 14, 2024
1 parent 5e2bd2b commit 5f71782
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
37 changes: 27 additions & 10 deletions wger/core/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import JsonResponse
from django.http import (
HttpResponseForbidden,
JsonResponse,
)
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page

Expand Down Expand Up @@ -82,6 +85,7 @@
from wger.utils.api_token import create_token
from wger.utils.permissions import WgerPermission


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -410,28 +414,41 @@ class RoutineWeightUnitViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = ('name',)


@login_required
@api_view(['GET'])
@api_view()
def get_token_for_user(request):
if not request.user.is_authenticated:
return HttpResponseForbidden()

token = AccessToken.for_user(request.user)

return JsonResponse(
data={
'token': str(token),
'type': str(token.token_type),
'user': request.user.username,
'powersync_url': 'http://powersync:8080',
}
)


@login_required
@api_view(['GET'])
@api_view()
def get_powersync_keys(request):
if not request.user.is_authenticated:
return HttpResponseForbidden()

return JsonResponse(
{
'keys': [
settings.POWERSYNC_JWKS_PUBLIC_KEY,
]
},
{'keys': [settings.POWERSYNC_JWKS_PUBLIC_KEY]},
status=200,
)


@api_view()
def upload_powersync_data(request):
if not request.user.is_authenticated:
return HttpResponseForbidden()

logger.debug(request.POST)
return JsonResponse(
{'ok!'},
status=200,
)
7 changes: 6 additions & 1 deletion wger/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
name='min_app_version',
),
path(
'api/v2/get-token',
'api/v2/powersync-token',
core_api_views.get_token_for_user,
name='get_token',
),
Expand All @@ -287,6 +287,11 @@
core_api_views.get_powersync_keys,
name='powersync-keys',
),
path(
'api/v2/upload-powersync-data',
core_api_views.upload_powersync_data,
name='powersync-keys',
),
# Api documentation
path(
'api/v2/schema',
Expand Down

0 comments on commit 5f71782

Please sign in to comment.