From 1c9748fcc601408462cb4c8a0bb1e4b77a5c90f7 Mon Sep 17 00:00:00 2001
From: Nico Vermaas <vermaas@astron.nl>
Date: Tue, 23 Feb 2021 11:59:18 +0100
Subject: [PATCH] adding username

---
 src/contexts/GlobalContext.js | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/contexts/GlobalContext.js b/src/contexts/GlobalContext.js
index 2ebfc7d..291a690 100644
--- a/src/contexts/GlobalContext.js
+++ b/src/contexts/GlobalContext.js
@@ -5,6 +5,15 @@ import getCookie from "../utils/getCookie";
 
 export const GlobalContext = createContext();
 
+function getUserName(api_host, setLoggedInUserName){
+  const profileUrl = api_host + "accounts/user-profiles/";
+  axios
+    .get(profileUrl, {withCredentials: true})
+    .then((response) => {
+      setLoggedInUserName(response.data.results[0].user_name);
+    })
+}
+
 export function GlobalContextProvider({ children }) {
   
   const api_host =
@@ -16,6 +25,8 @@ export function GlobalContextProvider({ children }) {
 
   const [archives, setArchives] = useState();
   const [navbar, setNavbar] = useState();
+  const [loggedInUserName, setLoggedInUserName] = useState();
+
   useEffect(() => {
     axios
       .get(api_host + "query/archives-uri")
@@ -42,6 +53,7 @@ export function GlobalContextProvider({ children }) {
     setIsAuthenticated(true);
     setSessionid(getCookie("sessionid"));
     history.replace("/");
+    getUserName(api_host, setLoggedInUserName);
     return null;
   };
 
@@ -49,6 +61,7 @@ export function GlobalContextProvider({ children }) {
     setIsAuthenticated(false);
     setSessionid(null);
     history.replace("/");
+    setLoggedInUserName("nobody");
     return null;
   };
 
@@ -76,6 +89,7 @@ export function GlobalContextProvider({ children }) {
         handleLogin,
         handleLogout,
         handleError,
+        loggedInUserName,
       }}
     >
       {children}
-- 
GitLab