Skip to content

Commit 265ca44

Browse files
committed
user login workflow
1 parent 775dbac commit 265ca44

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/frontend_react/src/api/config.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// src/config.js
22

3+
import { UserInfo } from "@/models";
4+
35
declare global {
46
interface Window {
57
appConfig?: Record<string, any>;
68
activeUserId?: string;
9+
userInfo?: UserInfo[];
710
}
811
}
912

@@ -38,7 +41,21 @@ export function getConfigData() {
3841

3942
return { ...config };
4043
}
41-
44+
export async function getUserInfo(): Promise<UserInfo[]> {
45+
try {
46+
const response = await fetch("/.auth/me");
47+
if (!response.ok) {
48+
console.log(
49+
"No identity provider found. Access to chat will be blocked."
50+
);
51+
return [];
52+
}
53+
const payload = await response.json();
54+
return payload;
55+
} catch (e) {
56+
return [];
57+
}
58+
}
4259
export function getApiUrl() {
4360
if (!API_URL) {
4461
// Check if window.appConfig exists
@@ -56,7 +73,7 @@ export function getApiUrl() {
5673
}
5774

5875
export function getUserId(): string {
59-
USER_ID = window.activeUserId ?? null;
76+
USER_ID = window.userInfo ? window.userInfo[0].user_id : null;
6077
const userId = USER_ID ?? "00000000-0000-0000-0000-000000000000";
6178
return userId;
6279
}

src/frontend_react/src/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import './index.css';
44
import App from './App';
55
import reportWebVitals from './reportWebVitals';
66
import { FluentProvider, teamsLightTheme, teamsDarkTheme } from "@fluentui/react-components";
7-
import { setEnvData, setApiUrl, config as defaultConfig, toBoolean } from './api/config';
7+
import { setEnvData, setApiUrl, config as defaultConfig, toBoolean, getUserInfo } from './api/config';
8+
import { UserInfo } from './models';
89
const root = ReactDOM.createRoot(document.getElementById("root") as HTMLElement);
910

1011
const AppWrapper = () => {
1112
// State to store the current theme
1213
const [isConfigLoaded, setIsConfigLoaded] = useState(false);
14+
const [isUserInfoLoaded, setIsUserInfoLoaded] = useState(false);
15+
const [userInfo, setUserInfo] = useState<UserInfo[] | null>(null);
1316
const [isDarkMode, setIsDarkMode] = useState(
1417
window.matchMedia("(prefers-color-scheme: dark)").matches
1518
);
@@ -33,11 +36,15 @@ const AppWrapper = () => {
3336
setEnvData(config);
3437
setApiUrl(config.API_URL);
3538
setConfig(config);
39+
let defaultUserInfo = config.ENABLE_AUTH ? await getUserInfo() : [] as UserInfo[];
40+
window.userInfo = defaultUserInfo;
41+
setUserInfo(defaultUserInfo);
3642

3743
} catch (error) {
3844
console.info("frontend config did not load from python", error);
3945
} finally {
4046
setIsConfigLoaded(true);
47+
setIsUserInfoLoaded(true);
4148
}
4249
};
4350

@@ -58,7 +65,7 @@ const AppWrapper = () => {
5865
mediaQuery.addEventListener("change", handleThemeChange);
5966
return () => mediaQuery.removeEventListener("change", handleThemeChange);
6067
}, []);
61-
if (!isConfigLoaded) return <div>Loading...</div>;
68+
if (!isConfigLoaded || !isUserInfoLoaded) return <div>Loading...</div>;
6269
return (
6370
<StrictMode>
6471
<FluentProvider theme={isDarkMode ? teamsDarkTheme : teamsLightTheme} style={{ height: "100vh" }}>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type UserInfo = {
2+
access_token: string;
3+
expires_on: string;
4+
id_token: string;
5+
provider_name: string;
6+
user_claims: any[];
7+
user_id: string;
8+
};

src/frontend_react/src/models/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ export * from './taskDetails';
1414
export * from './taskList';
1515
export * from './planPanelLeft';
1616
export * from './homeInput';
17+
export * from './auth'
1718

1819
// Add other model exports as needed

0 commit comments

Comments
 (0)