From 7988a804b815bad912f671600765f445d9d83342 Mon Sep 17 00:00:00 2001 From: Cristian Schuszter <cristian.schuszter@cern.ch> Date: Tue, 13 Oct 2020 16:04:53 +0200 Subject: [PATCH] Add user info --- src/App.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 8f1f861..9ea5315 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect, useState } from "react"; import { connect } from "react-redux"; import { keycloakAuthenticated, @@ -12,6 +12,21 @@ const App = ({ authenticated, userToken, kcInstance }) => { kcInstance.logout(); }; + const [userInfo, setUserInfo] = useState({}); + + useEffect(() => { + const getData = async () => { + const userInfoResponse = await fetch( + `${userToken.iss}/protocol/openid-connect/userinfo`, + { + headers: { Authorization: `Bearer ${kcInstance.token}` }, + } + ); + setUserInfo(await userInfoResponse.json()); + }; + getData(); + }, [userToken, kcInstance.token]); + return ( <div className="App" style={{ margin: "20px" }}> {!authenticated && <p>Loading...</p>} @@ -30,6 +45,13 @@ const App = ({ authenticated, userToken, kcInstance }) => { {JSON.stringify(userToken, null, 2)} </code> </div> + <hr /> + <p>My user info: </p> + <div> + <code style={{ whiteSpace: "pre-wrap" }}> + {JSON.stringify(userInfo, null, 2)} + </code> + </div> </> )} </div> -- GitLab