-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove terra functionality from authprovider into its own provi…
…der (#178)
- Loading branch information
Fran McDade
authored and
Fran McDade
committed
Oct 24, 2024
1 parent
a78ce2a
commit dd975b7
Showing
128 changed files
with
2,489 additions
and
1,079 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/components/Authentication/components/SessionController/SessionController.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useSession } from "next-auth/react"; | ||
import React, { Fragment, useEffect, useMemo } from "react"; | ||
import { updateAuthentication } from "../../../../providers/authentication/authentication/dispatch"; | ||
import { useAuthentication } from "../../../../providers/authentication/authentication/hook"; | ||
import { updateAuthorization } from "../../../../providers/authentication/authorization/dispatch"; | ||
import { useAuthorization } from "../../../../providers/authentication/authorization/hook"; | ||
import { SessionControllerProps } from "./types"; | ||
import { mapAuthentication, mapAuthorization } from "./utils"; | ||
|
||
export function SessionController({ | ||
children, | ||
}: SessionControllerProps): JSX.Element { | ||
const { authenticationDispatch } = useAuthentication(); | ||
const { authorizationDispatch } = useAuthorization(); | ||
const session = useSession(); | ||
const authentication = useMemo(() => mapAuthentication(session), [session]); | ||
const authorization = useMemo(() => mapAuthorization(session), [session]); | ||
|
||
useEffect(() => { | ||
authenticationDispatch?.(updateAuthentication(authentication)); | ||
authorizationDispatch?.(updateAuthorization(authorization)); | ||
}, [ | ||
authentication, | ||
authenticationDispatch, | ||
authorization, | ||
authorizationDispatch, | ||
]); | ||
|
||
return <Fragment>{children}</Fragment>; | ||
} |
5 changes: 5 additions & 0 deletions
5
src/components/Authentication/components/SessionController/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ReactNode } from "react"; | ||
|
||
export interface SessionControllerProps { | ||
children: ReactNode | ReactNode[]; | ||
} |
78 changes: 78 additions & 0 deletions
78
src/components/Authentication/components/SessionController/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Session } from "next-auth"; | ||
import { SessionContextValue } from "next-auth/react"; | ||
import { | ||
AUTHENTICATION_STATUS, | ||
UpdateAuthenticationPayload, | ||
UserProfile, | ||
} from "../../../../providers/authentication/authentication/types"; | ||
import { | ||
AUTHORIZATION_STATUS, | ||
UpdateAuthorizationStatusPayload, | ||
} from "../../../../providers/authentication/authorization/types"; | ||
|
||
/** | ||
* Returns the authentication profile and status from the session context. | ||
* @param session - Session context value. | ||
* @returns authentication profile and status. | ||
*/ | ||
export function mapAuthentication( | ||
session: SessionContextValue | ||
): UpdateAuthenticationPayload { | ||
return { | ||
profile: mapProfile(session.data), | ||
status: mapAuthenticationStatus(session.status), | ||
}; | ||
} | ||
|
||
/** | ||
* Maps the session data to a user profile. | ||
* @param sessionData - Session data. | ||
* @returns user profile. | ||
*/ | ||
function mapProfile(sessionData: Session | null): UserProfile | undefined { | ||
if (!sessionData) return; | ||
const { user } = sessionData; | ||
if (!user) return; | ||
const { email, image, name } = user; | ||
return { | ||
email: email || "", | ||
image: image || "", | ||
name: name || "", | ||
}; | ||
} | ||
|
||
/** | ||
* Returns the authentication status based on the session status. | ||
* @param status - Session status. | ||
* @returns authentication status. | ||
*/ | ||
function mapAuthenticationStatus( | ||
status: SessionContextValue["status"] | ||
): AUTHENTICATION_STATUS { | ||
switch (status) { | ||
case "authenticated": | ||
return AUTHENTICATION_STATUS.AUTHENTICATED; | ||
case "loading": | ||
return AUTHENTICATION_STATUS.PENDING; | ||
default: | ||
return AUTHENTICATION_STATUS.UNAUTHENTICATED; | ||
} | ||
} | ||
|
||
/** | ||
* Maps the session context value to an authorization status. | ||
* @param session - Session context value. | ||
* @returns authorization status. | ||
*/ | ||
export function mapAuthorization( | ||
session: SessionContextValue | ||
): UpdateAuthorizationStatusPayload { | ||
switch (session.status) { | ||
case "authenticated": | ||
return AUTHORIZATION_STATUS.AUTHORIZED; | ||
case "loading": | ||
return AUTHORIZATION_STATUS.PENDING; | ||
default: | ||
return AUTHORIZATION_STATUS.UNAUTHORIZED; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
...ns/components/Authentication/components/AuthenticationMenu/authenticationMenu.stories.tsx
This file was deleted.
Oops, something went wrong.
58 changes: 17 additions & 41 deletions
58
...ts/Actions/components/Authentication/components/AuthenticationMenu/authenticationMenu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,45 @@ | ||
import { MenuItem } from "@mui/material"; | ||
import React, { MouseEvent, useState } from "react"; | ||
import { UserProfile } from "../../../../../../../../../../../../hooks/useAuthentication/useFetchGoogleProfile"; | ||
import React, { Fragment } from "react"; | ||
import { useAuth } from "../../../../../../../../../../../../providers/authentication/auth/hook"; | ||
import { UserProfile } from "../../../../../../../../../../../../providers/authentication/authentication/types"; | ||
import { useMenu } from "../../../../../../../../../../../common/Menu/hooks/useMenu"; | ||
import { | ||
Avatar, | ||
AuthenticationMenu as Menu, | ||
UserIcon, | ||
UserNames, | ||
UserSummary, | ||
} from "./authenticationMenu.styles"; | ||
import { MENU_PROPS } from "./constants"; | ||
|
||
export interface AuthenticationMenuProps { | ||
onLogout: () => void; | ||
userProfile: UserProfile; | ||
profile: UserProfile; | ||
} | ||
|
||
export const AuthenticationMenu = ({ | ||
onLogout, | ||
userProfile, | ||
profile, | ||
}: AuthenticationMenuProps): JSX.Element => { | ||
const [anchorEl, setAnchorEl] = useState<null | HTMLButtonElement>(null); | ||
const open = Boolean(anchorEl); | ||
|
||
const onOpenMenu = (event: MouseEvent<HTMLButtonElement>): void => { | ||
setAnchorEl(event.currentTarget); | ||
}; | ||
|
||
const onCloseMenu = (): void => { | ||
setAnchorEl(null); | ||
}; | ||
|
||
const { service: { requestLogout } = {} } = useAuth(); | ||
const { anchorEl, onClose, onOpen, open } = useMenu<HTMLElement>(); | ||
return ( | ||
<> | ||
<UserIcon onClick={onOpenMenu}> | ||
<Avatar | ||
alt={`${userProfile.given_name} ${userProfile.family_name}`} | ||
src={userProfile.picture} | ||
/> | ||
<Fragment> | ||
<UserIcon onClick={onOpen}> | ||
<Avatar alt={profile.name} src={profile.image} /> | ||
</UserIcon> | ||
<Menu | ||
anchorEl={anchorEl} | ||
anchorOrigin={{ horizontal: "right", vertical: "bottom" }} | ||
autoFocus={false} | ||
onClose={onCloseMenu} | ||
open={open} | ||
slotProps={{ paper: { variant: "menu" } }} | ||
transformOrigin={{ | ||
horizontal: "right", | ||
vertical: "top", | ||
}} | ||
> | ||
<Menu {...MENU_PROPS} anchorEl={anchorEl} onClose={onClose} open={open}> | ||
<UserSummary> | ||
You are signed in as: | ||
<UserNames noWrap> | ||
{userProfile.given_name} {userProfile.family_name} | ||
</UserNames> | ||
<UserNames noWrap>{profile.name}</UserNames> | ||
</UserSummary> | ||
<MenuItem | ||
onClick={(): void => { | ||
onCloseMenu(); | ||
onLogout(); | ||
requestLogout?.(); | ||
onClose(); | ||
}} | ||
> | ||
Logout | ||
</MenuItem> | ||
</Menu> | ||
</> | ||
</Fragment> | ||
); | ||
}; |
Oops, something went wrong.