Skip to content

Commit

Permalink
refactor: google sign in with active terra profile status (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fran McDade authored and Fran McDade committed Jan 5, 2025
1 parent e9d8af4 commit 7fe5695
Show file tree
Hide file tree
Showing 30 changed files with 183 additions and 424 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
import { useSession } from "next-auth/react";
import React, { Fragment, useEffect, useMemo } from "react";
import React, { Fragment, useEffect } 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";
import { mapAuthentication } 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,
]);
authenticationDispatch?.(updateAuthentication(mapAuthentication(session)));
}, [authenticationDispatch, session]);

return <Fragment>{children}</Fragment>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import {
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.
Expand Down Expand Up @@ -51,28 +47,10 @@ function mapAuthenticationStatus(
): AUTHENTICATION_STATUS {
switch (status) {
case "authenticated":
return AUTHENTICATION_STATUS.AUTHENTICATED;
return AUTHENTICATION_STATUS.DONE;
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;
return AUTHENTICATION_STATUS.DONE;
}
}
16 changes: 0 additions & 16 deletions src/hooks/authentication/authorization/useAuthorizationReducer.ts

This file was deleted.

12 changes: 4 additions & 8 deletions src/hooks/authentication/profile/useProfile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useAuth } from "../../../providers/authentication/auth/hook";
import { AUTH_STATUS } from "../../../providers/authentication/auth/types";
import { useAuthentication } from "../../../providers/authentication/authentication/hook";
import { AUTHENTICATION_STATUS } from "../../../providers/authentication/authentication/types";
import { UseProfile } from "./types";

/**
Expand All @@ -9,13 +8,10 @@ import { UseProfile } from "./types";
*/
export const useProfile = (): UseProfile => {
const {
authState: { isAuthenticated, status },
} = useAuth();
const {
authenticationState: { profile },
authenticationState: { profile, status },
} = useAuthentication();
return {
isLoading: status === AUTH_STATUS.PENDING,
profile: isAuthenticated ? profile : undefined, // Release the profile only when the user is authenticated.
isLoading: status === AUTHENTICATION_STATUS.PENDING,
profile,
};
};
73 changes: 30 additions & 43 deletions src/hooks/authentication/session/useSessionAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,59 @@ import { updateAuthState } from "../../../providers/authentication/auth/dispatch
import {
AUTH_STATUS,
AuthContextProps,
UpdateAuthStatePayload,
} from "../../../providers/authentication/auth/types";
import {
AUTHENTICATION_STATUS,
AuthenticationContextProps,
} from "../../../providers/authentication/authentication/types";
import {
AUTHORIZATION_STATUS,
AuthorizationContextProps,
} from "../../../providers/authentication/authorization/types";

export const useSessionAuth = ({
authenticationReducer,
authorizationReducer,
authReducer,
}: {
authenticationReducer: AuthenticationContextProps;
authorizationReducer: AuthorizationContextProps;
authReducer: Omit<AuthContextProps, "service">;
}): void => {
const { authDispatch } = authReducer;
const { authenticationState } = authenticationReducer;
const { authorizationState } = authorizationReducer;
const isAuthenticated = isSessionAuthenticated(authenticationState.status);
const isAuthorized = isSessionAuthorized(authorizationState.status);
const status = getSessionStatus(
authenticationState.status,
authorizationState.status
);
const {
authenticationState: { profile, status },
} = authenticationReducer;
const isAuthenticated = !!profile;

useEffect(() => {
authDispatch?.(updateAuthState({ isAuthenticated, isAuthorized, status }));
}, [authDispatch, isAuthenticated, isAuthorized, status]);
authDispatch?.(
updateAuthState(getSession(isAuthenticated, getSessionStatus(status)))
);
}, [authDispatch, isAuthenticated, status]);
};

/**
* Returns the session status based on the authentication and authorization status.
* @param authenticationStatus - Authentication status.
* @param authorizationStatus - Authorization status.
* @returns session status.
* Returns the auth session based on the authentication state.
* @param isAuthenticated - Authentication status.
* @param status - Auth status.
* @returns auth state payload.
*/
function getSessionStatus(
authenticationStatus: AUTHENTICATION_STATUS,
authorizationStatus: AUTHORIZATION_STATUS
): AUTH_STATUS {
if (
authenticationStatus === AUTHENTICATION_STATUS.PENDING ||
authorizationStatus === AUTHORIZATION_STATUS.PENDING
)
return AUTH_STATUS.PENDING;
return AUTH_STATUS.DONE;
function getSession(
isAuthenticated: boolean,
status: AUTH_STATUS
): UpdateAuthStatePayload {
switch (status) {
case AUTH_STATUS.PENDING:
return { isAuthenticated: false, status };
case AUTH_STATUS.DONE:
return { isAuthenticated, status };
default:
return { isAuthenticated: false, status };
}
}

/**
* Returns true if the session is authenticated.
* Returns the session status based on the authentication status.
* @param status - Authentication status.
* @returns true if the session is authenticated.
*/
function isSessionAuthenticated(status: AUTHENTICATION_STATUS): boolean {
return status === AUTHENTICATION_STATUS.AUTHENTICATED;
}

/**
* Returns true if the session is authorized.
* @param status - Authorization status.
* @returns true if the session is authorized.
* @returns session status.
*/
function isSessionAuthorized(status: AUTHORIZATION_STATUS): boolean {
return status === AUTHORIZATION_STATUS.AUTHORIZED;
function getSessionStatus(status: AUTHENTICATION_STATUS): AUTH_STATUS {
if (status === AUTHENTICATION_STATUS.PENDING) return AUTH_STATUS.PENDING;
return AUTH_STATUS.DONE;
}
31 changes: 0 additions & 31 deletions src/hooks/authentication/session/useSessionCredentials.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/providers/authentication/auth/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ import { AUTH_STATUS, AuthState } from "./types";

export const DEFAULT_AUTH_STATE: AuthState = {
isAuthenticated: false,
isAuthorized: false,
status: AUTH_STATUS.PENDING,
};
2 changes: 0 additions & 2 deletions src/providers/authentication/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface AuthContextProps {
export interface AuthState {
initialState?: AuthState;
isAuthenticated: boolean;
isAuthorized: boolean;
status: AUTH_STATUS;
}

Expand Down Expand Up @@ -49,6 +48,5 @@ export interface UpdateAuthStateAction {

export interface UpdateAuthStatePayload {
isAuthenticated?: boolean;
isAuthorized?: boolean;
status?: AUTH_STATUS;
}
12 changes: 12 additions & 0 deletions src/providers/authentication/authentication/dispatch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import {
AUTHENTICATION_STATUS,
AuthenticationActionKind,
RequestAuthenticationAction,
ResetStateAction,
UpdateAuthenticationAction,
UpdateAuthenticationPayload,
} from "./types";

/**
* Authentication is complete.
* @returns Action.
*/
export function authenticationComplete(): UpdateAuthenticationAction {
return {
payload: { status: AUTHENTICATION_STATUS.DONE },
type: AuthenticationActionKind.UpdateAuthentication,
};
}

/**
* Request authentication action.
* @returns Action.
Expand Down
3 changes: 1 addition & 2 deletions src/providers/authentication/authentication/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Dispatch } from "react";

export enum AUTHENTICATION_STATUS {
AUTHENTICATED = "AUTHENTICATED",
DONE = "DONE",
PENDING = "PENDING",
UNAUTHENTICATED = "UNAUTHENTICATED",
}

export type AuthenticationAction =
Expand Down
17 changes: 0 additions & 17 deletions src/providers/authentication/authorization/actions.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/providers/authentication/authorization/constants.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/providers/authentication/authorization/context.ts

This file was deleted.

43 changes: 0 additions & 43 deletions src/providers/authentication/authorization/dispatch.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/providers/authentication/authorization/hook.ts

This file was deleted.

Loading

0 comments on commit 7fe5695

Please sign in to comment.