Skip to content
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: using the new logout endpoint #44

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/auth-provider/src/common/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type ServiceCallProps = {
params: any;
type: "authenticate" | "logout" | (string & {});
};

export type AuthProviderProps = {
Expand Down
38 changes: 32 additions & 6 deletions packages/auth-provider/src/common/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import {
import { API_ENDPOINT } from "./constants";
import type { ServiceCallProps } from "./types";

export const isProd = process.env.NODE_ENV === "production";
export const isDev = !isProd;
const isProd = process.env.NODE_ENV === "production";
const isDev = !isProd;
const API_TYPE = {
AUTHENTICATE: "authenticate",
LOGOUT: "logout",
};

export const serviceCall = async ({ params = {} }: ServiceCallProps) => {
export const serviceCall = async ({ type, params = {} }: ServiceCallProps) => {
try {
const response = await fetch(
isDev
? `${API_ENDPOINT.dev}/authenticate`
: `${API_ENDPOINT.prod}/authenticate`,
isDev ? `${API_ENDPOINT.dev}/${type}` : `${API_ENDPOINT.prod}/${type}`,
{
credentials: "include",
method: "POST",
Expand All @@ -43,6 +45,29 @@ export const serviceCall = async ({ params = {} }: ServiceCallProps) => {
return { status: 500, data: [] };
}
};
export const logoutUser = async ({
idToken,
accessToken,
clientId,
}: { idToken: string; accessToken: string; clientId: string }) => {
try {
const response = await serviceCall({
type: API_TYPE.LOGOUT,
params: {
idToken,
accessToken,
clientId,
},
});
return {
status: response.status === 200,
};
} catch (_error) {
return {
status: false,
};
}
};

export const authenticateUser = async ({
username,
Expand All @@ -59,6 +84,7 @@ export const authenticateUser = async ({
}) => {
try {
const response = await serviceCall({
type: API_TYPE.AUTHENTICATE,
params: {
type: AUTH_TYPES.ID_AND_ACCESS_TOKEN,
username,
Expand Down
46 changes: 33 additions & 13 deletions packages/auth-provider/src/components/AuthProvider/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
LOGOUT_SESSION,
} from "../../common/constants";
import type { AuthProviderProps, AuthState } from "../../common/types";
import { authenticateUser } from "../../common/utilities";
import { usePrevious } from "../hooks/usePrevious";
import { authenticateUser, logoutUser } from "../../common/utilities";
import { AuthContext } from "./AuthContext";

export const AuthProvider = ({
Expand All @@ -37,9 +36,7 @@ export const AuthProvider = ({
idTokenClaims: null,
});

const previousIdToken = usePrevious(idToken) || "";

const cleanupSession = useCallback(
const removeStateAndLocalStorage = useCallback(
(logoutReason?: string) => {
setAuthState({
isLoading: false,
Expand All @@ -63,7 +60,7 @@ export const AuthProvider = ({
* idToken "string" and other claims in the state.
*/
useEffect(() => {
if (previousIdToken !== idToken && idToken !== null) {
if (authState.isLoading && idToken !== null) {
(async () => {
try {
const jwt = await verifyAndExtractToken(idToken, clientId);
Expand All @@ -79,14 +76,30 @@ export const AuthProvider = ({
},
});
} else {
cleanupSession(EXPIRED_SESSION);
removeStateAndLocalStorage(EXPIRED_SESSION);
await logoutUser({
idToken: idToken,
accessToken: accessToken,
clientId: clientId,
});
}
} catch (_error) {
cleanupSession(EXPIRED_SESSION);
removeStateAndLocalStorage(EXPIRED_SESSION);
await logoutUser({
idToken: idToken,
accessToken: accessToken,
clientId: clientId,
});
}
})();
}
}, [idToken, previousIdToken, clientId, cleanupSession]);
}, [
authState.isLoading,
accessToken,
idToken,
clientId,
removeStateAndLocalStorage,
]);

const login = async (
username: string,
Expand All @@ -111,16 +124,23 @@ export const AuthProvider = ({
});
return true;
}
cleanupSession(LOGIN_ERROR);
removeStateAndLocalStorage(LOGIN_ERROR);
return false;
};

const logout = () => {
cleanupSession(LOGOUT_SESSION);
const logout = async () => {
removeStateAndLocalStorage(LOGOUT_SESSION);
await logoutUser({
idToken: idToken,
accessToken: accessToken,
clientId: clientId,
});
};

const getAccessToken = () => {
return accessToken;
if (authState.isAuthenticated && accessToken) {
return accessToken;
}
};

return (
Expand Down