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

chore(web): migrate aws-amplify to v6 #1218

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"ajv": "8.17.1",
"antd": "5.20.3",
"apollo-upload-client": "18.0.1",
"aws-amplify": "5.3.21",
"aws-amplify": "6.5.3",
"cesium": "1.120.0",
"cesium-mvt-imagery-provider": "1.4.1",
"dayjs": "1.11.13",
Expand Down
24 changes: 12 additions & 12 deletions web/src/auth/CognitoAuth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Auth } from "aws-amplify";
import { fetchAuthSession, getCurrentUser, signInWithRedirect, signOut } from "aws-amplify/auth";
import { useState, useEffect } from "react";

import { logOutFromTenant } from "@reearth-cms/config";
Expand All @@ -9,11 +9,16 @@ export const useCognitoAuth = (): AuthHook => {
const [user, setUser] = useState<unknown>(null);
const [error, setError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);

useEffect(() => {
const checkUser = async () => {
try {
const cognitoUser = await Auth.currentAuthenticatedUser();
const { username, signInDetails } = await getCurrentUser();
const { tokens: session } = await fetchAuthSession();
const cognitoUser = {
username,
session,
authenticationFlowType: signInDetails?.authFlowType,
};
setUser(cognitoUser);
} catch (err) {
if (err instanceof Error) {
Expand All @@ -24,24 +29,20 @@ export const useCognitoAuth = (): AuthHook => {
}
setIsLoading(false);
};

checkUser();
}, []);

const getAccessToken = async () => {
const session = await Auth.currentSession();
return session.getIdToken().getJwtToken();
const session = await fetchAuthSession();
return session.tokens?.idToken?.toString() ?? "";
};

const login = () => {
logOutFromTenant();
Auth.federatedSignIn();
signInWithRedirect();
};

const logout = async () => {
logOutFromTenant();
try {
await Auth.signOut();
await signOut();
setUser(null);
} catch (err) {
if (err instanceof Error) {
Expand All @@ -51,6 +52,5 @@ export const useCognitoAuth = (): AuthHook => {
}
}
};

return { user, isAuthenticated: !!user, isLoading, error, getAccessToken, login, logout };
};
3 changes: 2 additions & 1 deletion web/src/config/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export function configureCognito(cognito: CognitoParams) {
const cognitoOauthRedirectSignOut = cognito.cognitoOauthRedirectSignOut;
const cognitoOauthResponseType = cognito.cognitoOauthResponseType;

const config = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const config: Record<string, any> = {
Auth: {
region: cognitoRegion,
userPoolId: cognitoUserPoolId,
Expand Down
Loading
Loading