Skip to content

Commit

Permalink
feat: updated google sign in terra related authentication with auth s…
Browse files Browse the repository at this point in the history
…tate dispatch (#178)
  • Loading branch information
Fran McDade authored and Fran McDade committed Jan 5, 2025
1 parent 05fa1ed commit c076da6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function SessionController({
useEffect(() => {
// Dispatch only when profile is available:
// - Login errors are managed by the login service.
// - Logout operations handle resetting credentials and authentication state.
// - Logout operations handle resetting credentials, authentication and auth state.
if (!profile) return;
credentialsDispatch?.(updateCredentials(token)); // Release credentials.
authenticationDispatch?.(authenticationComplete()); // Authentication `status` is "SETTLED".
Expand Down
2 changes: 2 additions & 0 deletions src/providers/authentication/terra/hooks/useFetchProfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "./useFetchTerraTermsOfService";

export interface UseFetchProfiles {
isAuthenticated: boolean;
isComplete: boolean;
isProfileActive: boolean;
terraNIHProfileLoginStatus: LoginStatus<TerraNIHResponse>;
Expand Down Expand Up @@ -50,6 +51,7 @@ export const useFetchProfiles = (token?: string): UseFetchProfiles => {
]);

return {
isAuthenticated: isUserAuthenticated,
isComplete: status !== TERRA_PROFILE_STATUS.PENDING,
isProfileActive: status === TERRA_PROFILE_STATUS.AUTHENTICATED,
terraNIHProfileLoginStatus,
Expand Down
16 changes: 14 additions & 2 deletions src/providers/authentication/terra/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { useEffect } from "react";
import { authComplete } from "../auth/dispatch";
import { useAuth } from "../auth/hook";
import { authenticationComplete } from "../authentication/dispatch";
import { useAuthentication } from "../authentication/hook";
import { updateCredentials } from "../credentials/dispatch";
Expand All @@ -11,9 +13,11 @@ export function TerraProfileProvider({
children,
token,
}: TerraProfileProviderProps): JSX.Element {
const { authDispatch } = useAuth();
const { authenticationDispatch } = useAuthentication();
const { credentialsDispatch } = useCredentials();
const {
isAuthenticated,
isComplete,
isProfileActive,
terraNIHProfileLoginStatus,
Expand All @@ -22,13 +26,21 @@ export function TerraProfileProvider({
} = useFetchProfiles(token);

useEffect(() => {
if (!isComplete) return;
authenticationDispatch?.(authenticationComplete());
// Dispatch only when terra profile is available:
// - Login errors are managed by the login service.
// - Logout operations handle resetting credentials, authentication and auth state.
if (!isComplete) return; // Terra profile status is still "PENDING".
// Release authentication only when terra profile status is either "AUTHENTICATED" or "UNAUTHENTICATED" i.e. not "PENDING".
authenticationDispatch?.(authenticationComplete()); // Authentication `status` is "SETTLED".
authDispatch?.(authComplete({ isAuthenticated })); // Auth `status` is "SETTLED", and `isAuthenticated` is either "true" or "false".
if (!isProfileActive) return;
// Release credentials only when terra profile is "AUTHENTICATED".
credentialsDispatch?.(updateCredentials(token));
}, [
authDispatch,
authenticationDispatch,
credentialsDispatch,
isAuthenticated,
isComplete,
isProfileActive,
token,
Expand Down

0 comments on commit c076da6

Please sign in to comment.