Commit e2691fd0 authored by David Haynes's avatar David Haynes 🙆

More sane handling of getting session information

- /auth/status to get all the juicy deets
parent 04796779
Pipeline #3456 passed with stage
in 1 minute and 10 seconds
......@@ -7,6 +7,7 @@ name = "pypi"
pylint = "*"
pylint-django = "*"
coverage = "*"
black = "*"
[packages]
django = "<2.1,>=2.0"
......
This diff is collapsed.
......@@ -4,29 +4,45 @@ import { Button } from "reactstrap";
class AuthButton extends React.Component {
constructor(props) {
super(props);
this.state = { is_auth: false };
this.state = { error: null, is_auth: false };
}
componentDidMount() {
this.setState(() => {
return { is_auth: window.django.user.is_authenticated == "True" };
});
fetch("/auth/status")
.then(res => res.json())
.then(
result => {
this.setState({
is_auth: result.is_authenticated
});
},
error => {
this.setState({
error
});
}
);
}
render() {
return (
<div>
{this.state.is_auth ? (
<Button color="info" href="/auth/logout">
Logout
</Button>
) : (
<Button color="info" href="/auth/login">
Login
</Button>
)}
</div>
);
const { is_auth, error } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else {
return (
<div>
{is_auth ? (
<Button color="info" href="/auth/logout">
Logout
</Button>
) : (
<Button color="info" href="/auth/login">
Login
</Button>
)}
</div>
);
}
}
}
......
{% load static %}
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#006633" />
<meta name="description" content="University-branded URL shortening" />
<title>Welcome &bull; SRCT Go</title>
</head>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#006633" />
<meta name="description" content="University-branded URL shortening" />
<title>Welcome &bull; SRCT Go</title>
</head>
<body>
<!-- React injects itself here -->
<div id="root"></div>
</body>
<body>
<!-- React injects itself here -->
<div id="root"></div>
</body>
<script>
window.django = {
logout: "{% url "cas_logout" %}",
user: {
username: "{{ request.user.username }}",
full_name: "{{ request.user.get_full_name }}",
last_login: "{{ request.user.last_login }}",
is_authenticated: "{{ request.user.is_authenticated }}"
}
};
</script>
<script src="static/main.js"></script>
</html>
\ No newline at end of file
<script src="static/main.js"></script>
</html>
......@@ -4,17 +4,15 @@ settings/urls.py
The URLs of the project and their associated view that requests are routed to.
"""
# Django Imports
from django.urls import path, re_path, include
from django.urls import path, include
from django.contrib import admin
from django.views.decorators.cache import cache_page
from django.views.generic import TemplateView
# App Imports
from . import views
from cas import views as cas_views
# Third Party
from rest_framework import routers
from cas import views as cas_views
# App Imports
from . import views
router = routers.DefaultRouter()
router.register(r"golinks", views.URLViewSet, base_name="golinks")
......@@ -32,7 +30,8 @@ urlpatterns = [
# /admin - Administrator interface.
path("admin/", admin.site.urls, name="go_admin"),
path("auth/", include("rest_framework.urls")),
path("auth/token/", views.CustomAuthToken.as_view())
path("auth/token/", views.CustomAuthToken.as_view()),
path("auth/status/", views.GetSessionInfo.as_view())
# # /view/<short> - View URL data. Cached for 15 minutes
# re_path(r'^view/(?P<short>([\U00010000-\U0010ffff][\U0000200D]?)+)$',
# cache_page(60 * 15)(go.views.view), name='view'),
......
......@@ -4,15 +4,16 @@ go/views.py
The functions that handle a request to a given URL. Get some data, manipulate
it, and return a rendered template.
"""
from rest_framework import viewsets
from rest_framework import permissions
from rest_framework import viewsets, permissions
from rest_framework.authentication import TokenAuthentication, SessionAuthentication
from .serializers import URLSerializer
from .models import URL
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authtoken.models import Token
from rest_framework.permissions import IsAuthenticated
from rest_framework.authtoken.views import ObtainAuthToken
from .serializers import URLSerializer
from .models import URL
class URLPermission(permissions.BasePermission):
......@@ -46,11 +47,6 @@ class URLViewSet(viewsets.ModelViewSet):
serializer.save(owner=self.request.user.registereduser)
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
class CustomAuthToken(ObtainAuthToken):
authentication_classes = (SessionAuthentication,)
permission_classes = (IsAuthenticated,)
......@@ -59,3 +55,20 @@ class CustomAuthToken(ObtainAuthToken):
token, created = Token.objects.get_or_create(user=request.user)
return Response({"token": token.key})
class GetSessionInfo(APIView):
"""Handy endpoint to return current user session status & information to the frontend."""
authentication_classes = (SessionAuthentication,)
permission_classes = (IsAuthenticated,)
def get(self, request, *args, **kwargs):
token, created = Token.objects.get_or_create(user=request.user)
session_info = {
"username": request.user.username,
# "full_name": f"{request.user.get_full_name}",
"last_login": request.user.last_login,
"is_authenticated": request.user.is_authenticated,
"token": token.key,
}
return Response(session_info)
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment