-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: remove terra functionality from authprovider into its own provider (#178) #240
Open
frano-m
wants to merge
18
commits into
main
Choose a base branch
from
fran/178-auth-providers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
dd975b7
feat: remove terra functionality from authprovider into its own provi…
72014b6
refactor: google sign in and next auth directory structure (#178)
a3e52df
refactor: google sign in with active terra profile status (#178)
c1669ae
feat: update profile hook (#178)
ae467ab
feat: updated authentication pending status (#178)
df97865
feat: updated route history hook (#178)
d603646
feat: enable more reliable timeout handling with nextauth (#178)
hunterckx 3024379
fix: jsdoc profile hook (#178)
2199c17
refactor: auth status settled value (#178)
d6c92e6
refactor: rename DONE authentication status to SETTLED (#178)
hunterckx 6169b99
test: add tests for `useSessionAuth` and `getProfileStatus` (#178)
hunterckx ee23fd9
refactor: linting (#178)
df666cb
feat: added test for use route history hook (#178)
8f06f8e
feat: test linting (#178)
7ab2c8f
feat: added test use session active (#178)
b7aa1a2
feat: added terra profile provider test (#178)
6eebd53
feat: linting (#178)
0d2c2f5
feat: fix use route history test (#178)
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Terra profile is now separated from the now deleted authentication hook. |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SessionController required for populating the generic auth-related reducers with session from next-auth.