|
| 1 | +import type { IconButtonProps } from "@chakra-ui/react"; |
| 2 | +import { |
| 3 | + AbsoluteCenter, |
| 4 | + Button, |
| 5 | + CloseButton, |
| 6 | + Dialog, |
| 7 | + Heading, |
| 8 | + HStack, |
| 9 | + IconButton, |
| 10 | + Portal, |
| 11 | + Spinner, |
| 12 | + Stack, |
| 13 | + Text, |
| 14 | + VStack, |
| 15 | +} from "@chakra-ui/react"; |
| 16 | +import * as React from "react"; |
| 17 | +import { LuLock, LuUser } from "react-icons/lu"; |
| 18 | +import { useAuth } from "react-oidc-context"; |
| 19 | + |
| 20 | +const authority = import.meta.env.VITE_AUTH_AUTHORITY as string | undefined; |
| 21 | +const clientId = import.meta.env.VITE_AUTH_CLIENT_ID as string | undefined; |
| 22 | + |
| 23 | +export const authConfig = |
| 24 | + authority && clientId |
| 25 | + ? { |
| 26 | + authority, |
| 27 | + client_id: clientId, |
| 28 | + redirect_uri: |
| 29 | + window.location.origin + |
| 30 | + (import.meta.env.BASE_URL ?? "/").replace(/\/$/, "") + |
| 31 | + "/", |
| 32 | + onSigninCallback: () => { |
| 33 | + window.history.replaceState( |
| 34 | + {}, |
| 35 | + document.title, |
| 36 | + window.location.pathname + window.location.search |
| 37 | + ); |
| 38 | + }, |
| 39 | + } |
| 40 | + : null; |
| 41 | + |
| 42 | +export function LoginSplash({ children }: { children: React.ReactNode }) { |
| 43 | + const { isLoading, isAuthenticated, signinRedirect, error } = useAuth(); |
| 44 | + |
| 45 | + if (isLoading) { |
| 46 | + return ( |
| 47 | + <AbsoluteCenter> |
| 48 | + <Spinner size="xl" /> |
| 49 | + </AbsoluteCenter> |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + if (!isAuthenticated) { |
| 54 | + return ( |
| 55 | + <AbsoluteCenter> |
| 56 | + <VStack gap={6}> |
| 57 | + <Heading size="2xl"> |
| 58 | + <HStack> |
| 59 | + stac-map with auth <LuLock /> |
| 60 | + </HStack> |
| 61 | + </Heading> |
| 62 | + {error && ( |
| 63 | + <Text color="fg.error" fontSize="sm"> |
| 64 | + {error.message} |
| 65 | + </Text> |
| 66 | + )} |
| 67 | + <Button size="lg" onClick={() => signinRedirect()}> |
| 68 | + Sign in |
| 69 | + </Button> |
| 70 | + </VStack> |
| 71 | + </AbsoluteCenter> |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + return <>{children}</>; |
| 76 | +} |
| 77 | + |
| 78 | +interface UserButtonProps extends Omit<IconButtonProps, "aria-label"> {} |
| 79 | + |
| 80 | +export const UserButton = React.forwardRef<HTMLButtonElement, UserButtonProps>( |
| 81 | + function UserButton(props, ref) { |
| 82 | + const { user, removeUser } = useAuth(); |
| 83 | + |
| 84 | + return ( |
| 85 | + <Dialog.Root> |
| 86 | + <Dialog.Trigger asChild> |
| 87 | + <IconButton |
| 88 | + variant="ghost" |
| 89 | + aria-label="User info" |
| 90 | + ref={ref} |
| 91 | + {...props} |
| 92 | + > |
| 93 | + <LuUser /> |
| 94 | + </IconButton> |
| 95 | + </Dialog.Trigger> |
| 96 | + <Portal> |
| 97 | + <Dialog.Backdrop /> |
| 98 | + <Dialog.Positioner> |
| 99 | + <Dialog.Content> |
| 100 | + <Dialog.Header> |
| 101 | + <Dialog.Title>User Info</Dialog.Title> |
| 102 | + </Dialog.Header> |
| 103 | + <Dialog.Body> |
| 104 | + <Stack gap={2}> |
| 105 | + {user?.profile?.name && ( |
| 106 | + <Text> |
| 107 | + <Text as="span" fontWeight="bold"> |
| 108 | + Name: |
| 109 | + </Text>{" "} |
| 110 | + {user.profile.name} |
| 111 | + </Text> |
| 112 | + )} |
| 113 | + {user?.profile?.email && ( |
| 114 | + <Text> |
| 115 | + <Text as="span" fontWeight="bold"> |
| 116 | + Email: |
| 117 | + </Text>{" "} |
| 118 | + {user.profile.email} |
| 119 | + </Text> |
| 120 | + )} |
| 121 | + </Stack> |
| 122 | + </Dialog.Body> |
| 123 | + <Dialog.Footer> |
| 124 | + <Button |
| 125 | + variant="outline" |
| 126 | + onClick={() => removeUser()} |
| 127 | + colorPalette="red" |
| 128 | + > |
| 129 | + Sign out |
| 130 | + </Button> |
| 131 | + </Dialog.Footer> |
| 132 | + <Dialog.CloseTrigger asChild> |
| 133 | + <CloseButton size="sm" /> |
| 134 | + </Dialog.CloseTrigger> |
| 135 | + </Dialog.Content> |
| 136 | + </Dialog.Positioner> |
| 137 | + </Portal> |
| 138 | + </Dialog.Root> |
| 139 | + ); |
| 140 | + } |
| 141 | +); |
0 commit comments