Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion source/infrastructure/lambda/aws-exports-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface CloudFormationEvent {
UserFilesBucketRegion: string;
IoTEndpoint: string;
IoTPolicy: string;
OAuthDomain?: string;
OAuthRedirectUrl?: string;
};
PhysicalResourceId?: string;
}
Expand All @@ -44,11 +46,13 @@ export const handler = async (event: CloudFormationEvent): Promise<CloudFormatio
UserFilesBucketRegion,
IoTEndpoint,
IoTPolicy,
OAuthDomain,
OAuthRedirectUrl,
} = ResourceProperties;

try {
if (RequestType === "Create" || RequestType === "Update") {
const awsExports = {
const awsExports: Record<string, string> = {
UserPoolId,
PoolClientId,
IdentityPoolId,
Expand All @@ -59,6 +63,9 @@ export const handler = async (event: CloudFormationEvent): Promise<CloudFormatio
IoTPolicy,
};

if (OAuthDomain) awsExports.OAuthDomain = OAuthDomain;
if (OAuthRedirectUrl) awsExports.OAuthRedirectUrl = OAuthRedirectUrl;

await s3Client.send(
new PutObjectCommand({
Bucket: BucketName,
Expand Down
16 changes: 15 additions & 1 deletion source/webui/src/components/navigation/TopNavigationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@

import { TopNavigation, TopNavigationProps } from "@cloudscape-design/components";
import { useAuthenticator } from "@aws-amplify/ui-react";
import { fetchAuthSession } from "aws-amplify/auth";
import { useState, useEffect } from "react";

export default function TopNavigationBar() {
const { user, signOut } = useAuthenticator();
const [displayName, setDisplayName] = useState<string | null>(null);

useEffect(() => {
fetchAuthSession()
.then((session) => {
const payload = session.tokens?.idToken?.payload;
if (payload) {
setDisplayName((payload.name as string) || (payload.email as string) || null);
}
})
.catch(() => {});
}, [user]);

const solutionIdentity: TopNavigationProps.Identity = {
href: "/",
Expand All @@ -20,7 +34,7 @@ export default function TopNavigationBar() {
const utilities: TopNavigationProps.Utility[] = [
{
type: "menu-dropdown",
text: user.username ?? "User",
text: displayName ?? user.username ?? "User",
iconName: "user-profile",
items: [
{
Expand Down
13 changes: 9 additions & 4 deletions source/webui/src/contexts/UserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,20 @@ export const UserContextProvider = (props: { children: ReactNode }) => {
...responseUser,
});
try {
const userAttributesOutput = await fetchUserAttributes();
setEmail(userAttributesOutput.email ?? null);

// Attach IoT policy when user is authenticated
// Attach IoT policy first — fetchUserAttributes may fail for federated users
// and must not block IoT setup needed for real-time metrics
const response = await fetch("/aws-exports.json");
const config = await response.json();
if (config.IoTPolicy) {
await attachIoTPolicy(config.IoTPolicy);
}

try {
const userAttributesOutput = await fetchUserAttributes();
setEmail(userAttributesOutput.email ?? null);
} catch (attrError) {
console.log("fetchUserAttributes failed (expected for federated users):", attrError);
}
} catch (e) {
console.log(e);
}
Expand Down
11 changes: 11 additions & 0 deletions source/webui/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ getRuntimeConfig().then((json) => {
userPoolId: json.UserPoolId,
userPoolClientId: json.PoolClientId,
identityPoolId: json.IdentityPoolId,
...(json.OAuthDomain && {
loginWith: {
oauth: {
domain: json.OAuthDomain,
scopes: ["email", "openid", "profile"],
redirectSignIn: [json.OAuthRedirectUrl],
redirectSignOut: [json.OAuthRedirectUrl],
responseType: "code",
},
},
}),
},
},
API: {
Expand Down