-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): add aws cognito support in auth (#449)
- Loading branch information
Showing
15 changed files
with
2,572 additions
and
87 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export type AuthHook = { | ||
isAuthenticated: boolean; | ||
isLoading: boolean; | ||
error: string | null; | ||
getAccessToken: () => Promise<string>; | ||
login: () => void; | ||
logout: () => void; | ||
}; |
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,28 @@ | ||
import { useAuth0 } from "@auth0/auth0-react"; | ||
|
||
import { e2eAccessToken } from "@reearth/services/config"; | ||
|
||
import type { AuthHook } from "./authHook"; | ||
|
||
export const errorKey = "reeartherror"; | ||
|
||
export const useAuth0Auth = (): AuthHook => { | ||
const { isAuthenticated, error, isLoading, loginWithRedirect, logout, getAccessTokenSilently } = | ||
useAuth0(); | ||
|
||
return { | ||
isAuthenticated: !!e2eAccessToken() || (isAuthenticated && !error), | ||
isLoading, | ||
error: error?.message ?? null, | ||
getAccessToken: () => getAccessTokenSilently(), | ||
login: () => loginWithRedirect(), | ||
logout: () => | ||
logout({ | ||
logoutParams: { | ||
returnTo: error | ||
? `${window.location.origin}?${errorKey}=${encodeURIComponent(error?.message)}` | ||
: window.location.origin, | ||
}, | ||
}), | ||
}; | ||
}; |
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,51 @@ | ||
import { Auth0Provider } from "@auth0/auth0-react"; | ||
import React, { createContext, ReactNode } from "react"; | ||
|
||
import type { AuthHook } from "./authHook"; | ||
import { useAuth0Auth } from "./authOAuth"; | ||
import { useCognitoAuth } from "./cognitoAuth"; | ||
|
||
export const AuthContext = createContext<AuthHook | null>(null); | ||
|
||
const Auth0Wrapper = ({ children }: { children: ReactNode }) => { | ||
const auth = useAuth0Auth(); | ||
return <AuthContext.Provider value={auth}>{children}</AuthContext.Provider>; | ||
}; | ||
|
||
const CognitoWrapper = ({ children }: { children: ReactNode }) => { | ||
const auth = useCognitoAuth(); | ||
return <AuthContext.Provider value={auth}>{children}</AuthContext.Provider>; | ||
}; | ||
|
||
export const AuthProvider: React.FC<{ children?: ReactNode }> = ({ children }) => { | ||
const authProvider = window.REEARTH_CONFIG?.authProvider; | ||
|
||
if (authProvider === "auth0") { | ||
const domain = window.REEARTH_CONFIG?.auth0Domain; | ||
const clientId = window.REEARTH_CONFIG?.auth0ClientId; | ||
const audience = window.REEARTH_CONFIG?.auth0Audience; | ||
|
||
return domain && clientId ? ( | ||
<Auth0Provider | ||
domain={domain} | ||
clientId={clientId} | ||
authorizationParams={{ | ||
audience: audience, | ||
scope: "openid profile email offline_access", | ||
redirect_uri: window.location.origin, | ||
}} | ||
useRefreshTokens | ||
useRefreshTokensFallback | ||
cacheLocation="localstorage"> | ||
<Auth0Wrapper>{children}</Auth0Wrapper> | ||
</Auth0Provider> | ||
) : null; | ||
} | ||
|
||
if (authProvider === "cognito") { | ||
// No specific provider needed for Cognito/AWS Amplify | ||
return <CognitoWrapper>{children}</CognitoWrapper>; | ||
} | ||
|
||
return <>{children}</>; // or some default fallback | ||
}; |
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,52 @@ | ||
import { Auth } from "@aws-amplify/auth"; | ||
import { useState, useEffect } from "react"; | ||
|
||
import type { AuthHook } from "./authHook"; | ||
|
||
export const useCognitoAuth = (): AuthHook => { | ||
const [user, setUser] = useState<any>(null); | ||
const [error, setError] = useState<string | null>(null); | ||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
|
||
useEffect(() => { | ||
const checkUser = async () => { | ||
try { | ||
const cognitoUser = await Auth.currentAuthenticatedUser(); | ||
setUser(cognitoUser); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
setError(err.message); | ||
} else { | ||
setError(JSON.stringify(err)); | ||
} | ||
} | ||
setIsLoading(false); | ||
}; | ||
|
||
checkUser(); | ||
}, []); | ||
|
||
const getAccessToken = async () => { | ||
const session = await Auth.currentSession(); | ||
return session.getIdToken().getJwtToken(); | ||
}; | ||
|
||
const login = () => { | ||
Auth.federatedSignIn(); | ||
}; | ||
|
||
const logout = async () => { | ||
try { | ||
await Auth.signOut(); | ||
setUser(null); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
setError(err.message); | ||
} else { | ||
setError(JSON.stringify(err)); | ||
} | ||
} | ||
}; | ||
|
||
return { isAuthenticated: !!user, isLoading, error, getAccessToken, login, logout }; | ||
}; |
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 was deleted.
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
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 { Amplify } from "aws-amplify"; | ||
|
||
const authProvider = window.REEARTH_CONFIG?.authProvider; | ||
if (authProvider === "cognito") { | ||
const cognitoRegion = window.REEARTH_CONFIG?.cognitoRegion; | ||
const cognitoUserPoolId = window.REEARTH_CONFIG?.cognitoUserPoolId; | ||
const cognitoUserPoolWebClientId = window.REEARTH_CONFIG?.cognitoUserPoolWebClientId; | ||
const cognitoOauthScope = window.REEARTH_CONFIG?.cognitoOauthScope?.split(", "); | ||
const cognitoOauthDomain = window.REEARTH_CONFIG?.cognitoOauthDomain; | ||
const cognitoOauthRedirectSignIn = window.REEARTH_CONFIG?.cognitoOauthRedirectSignIn; | ||
const cognitoOauthRedirectSignOut = window.REEARTH_CONFIG?.cognitoOauthRedirectSignOut; | ||
const cognitoOauthResponseType = window.REEARTH_CONFIG?.cognitoOauthResponseType; | ||
|
||
const config = { | ||
Auth: { | ||
region: cognitoRegion, | ||
userPoolId: cognitoUserPoolId, | ||
userPoolWebClientId: cognitoUserPoolWebClientId, | ||
oauth: { | ||
scope: cognitoOauthScope, | ||
domain: cognitoOauthDomain, | ||
redirectSignIn: cognitoOauthRedirectSignIn, | ||
redirectSignOut: cognitoOauthRedirectSignOut, | ||
responseType: cognitoOauthResponseType, | ||
}, | ||
}, | ||
}; | ||
|
||
Amplify.configure(config); | ||
} |
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
Oops, something went wrong.