Skip to content

Commit

Permalink
setState for islogged in
Browse files Browse the repository at this point in the history
  • Loading branch information
timizuoebideri1 committed Feb 23, 2023
1 parent d41b77c commit 596d9c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
34 changes: 27 additions & 7 deletions nautobot/ui/src/components/common/BSNavBar.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { Container, Nav, Navbar, NavDropdown } from "react-bootstrap"
import { faRightToBracket } from "@fortawesome/free-solid-svg-icons";
import { faSignIn, faSignOut } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Link, NavLink } from "react-router-dom"
import {useState, useEffect} from "react"
import useSWR from "swr"

import { nautobot_url } from "src/index"


const fetcher = (url) => fetch(url, { credentials: "include" }).then((res) => res.json());
const fetcher = (url) => fetch(url, { credentials: "include" }).then((res) => {
if(res.status !== 200){
throw new Error(res.json());
}
return res.json();
});

export default function BSNavBar() {
const { data, error } = useSWR(nautobot_url + "/api/get-menu/", fetcher)
const { data, error } = useSWR([nautobot_url + "/api/get-menu/"], fetcher)
const { data: profileData, error: profileError } = useSWR(nautobot_url + "/api/users/users/my-profile/", fetcher)
const [ isLoggedIn, setIsLoggedIn] = useState(false)
useEffect(() => {
setIsLoggedIn(profileData ? true : false)
}, [profileData, profileError, isLoggedIn]);

if (error) return <div>Failed to load menu</div>
if (!data) return <></>

Expand Down Expand Up @@ -50,10 +62,18 @@ export default function BSNavBar() {
</Navbar.Collapse>
<Navbar.Collapse className="justify-content-end">
<Navbar.Text>
<Link to="/login/">
<FontAwesomeIcon icon={faRightToBracket} />
{" Login"}
</Link>
{
isLoggedIn ?
<Link to="/logout/" onClick={() => setIsLoggedIn(false)}>
<FontAwesomeIcon icon={faSignOut} />
{" Logout"}
</Link>
:
<Link to="/login/">
<FontAwesomeIcon icon={faSignIn} />
{" Login"}
</Link>
}
</Navbar.Text>
</Navbar.Collapse>
</Container>
Expand Down
6 changes: 6 additions & 0 deletions nautobot/users/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db.models import Count
from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiTypes
from rest_framework.authentication import BasicAuthentication
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.routers import APIRootView
Expand Down Expand Up @@ -36,6 +37,11 @@ class UserViewSet(ModelViewSet):
serializer_class = serializers.UserSerializer
filterset_class = filters.UserFilterSet

@action(methods=["GET"], detail=False, url_path="my-profile")
def my_profile(self, request):
serializer = self.serializer_class(instance=request.user, context={"request": request})
return Response(serializer.data)


@extend_schema_view(
bulk_destroy=extend_schema(request=BulkOperationIntegerIDSerializer(many=True)),
Expand Down

0 comments on commit 596d9c1

Please sign in to comment.