@cirrobio/react-auth 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -5
- package/src/amplify/amplify-auth-provider.tsx +70 -0
- package/src/amplify/auth-listener.ts +23 -0
- package/src/amplify/configure-amplify.ts +35 -0
- package/src/amplify/magic-link.ts +62 -0
- package/src/auth-context/authentication-context-provider.tsx +65 -0
- package/src/auth-context/authentication-context.tsx +15 -0
- package/src/auth-context/useAuthenticator.tsx +31 -0
- package/src/components/LoginModal.tsx +109 -0
- package/src/components/LoginOptions.tsx +67 -0
- package/src/components/LoginWrapper.tsx +25 -0
- package/src/index.ts +12 -0
- package/src/models/auth-status.ts +1 -0
- package/src/static/static-token-provider.ts +44 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cirrobio/react-auth",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Provides authentication configuration and components for React applications using Cirro.",
|
|
5
5
|
"author": "CirroBio",
|
|
6
6
|
"repository": {
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"module": "dist/index.esm.js",
|
|
15
15
|
"types": "dist/types/index.d.ts",
|
|
16
16
|
"files": [
|
|
17
|
-
"dist"
|
|
17
|
+
"dist",
|
|
18
|
+
"src"
|
|
18
19
|
],
|
|
19
20
|
"scripts": {
|
|
20
21
|
"build": "npx rollup --config",
|
|
@@ -24,9 +25,9 @@
|
|
|
24
25
|
"test": "jest --coverage --silent --passWithNoTests"
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"@cirrobio/api-client": "^0.
|
|
28
|
-
"@cirrobio/react-core": "^0.0.
|
|
29
|
-
"@cirrobio/sdk": "^0.
|
|
28
|
+
"@cirrobio/api-client": "^0.12.0",
|
|
29
|
+
"@cirrobio/react-core": "^0.0.2",
|
|
30
|
+
"@cirrobio/sdk": "^0.12.0",
|
|
30
31
|
"@mui/icons-material": "^5.17.1",
|
|
31
32
|
"@mui/lab": "^5.0.0-alpha.176",
|
|
32
33
|
"@mui/material": "^5.15.10",
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { LoginProvider } from "@cirrobio/api-client";
|
|
2
|
+
import { configureAmplify } from "./configure-amplify";
|
|
3
|
+
import { fetchAuthSession } from "@aws-amplify/auth";
|
|
4
|
+
import {
|
|
5
|
+
AppConfig,
|
|
6
|
+
InteractiveAuthenticationProvider,
|
|
7
|
+
IRedeemSignInLink,
|
|
8
|
+
ISignInLinkRequest,
|
|
9
|
+
AuthEventHandlerOptions
|
|
10
|
+
} from "@cirrobio/react-core";
|
|
11
|
+
import { signInWithRedirect } from "aws-amplify/auth";
|
|
12
|
+
import { redeemSignInLink, requestSignInLink } from "./magic-link";
|
|
13
|
+
import { Hub } from "aws-amplify/utils";
|
|
14
|
+
import { amplifyAuthEventHandler } from "./auth-listener";
|
|
15
|
+
import { CurrentUser } from "@cirrobio/sdk";
|
|
16
|
+
import { signOut as amplifySignOut } from 'aws-amplify/auth';
|
|
17
|
+
|
|
18
|
+
export class AmplifyAuthProvider implements InteractiveAuthenticationProvider {
|
|
19
|
+
private loginProviders: LoginProvider[];
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
readonly clientId?: string
|
|
23
|
+
) {
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public getLoginProviders() {
|
|
27
|
+
return this.loginProviders;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public configure(config: AppConfig): void {
|
|
31
|
+
configureAmplify(config, this.clientId);
|
|
32
|
+
this.loginProviders = config.tenantInfo.loginProviders;
|
|
33
|
+
console.log('AmplifyAuthProvider loaded with config');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public async getAccessToken(): Promise<string> {
|
|
37
|
+
const session = await fetchAuthSession();
|
|
38
|
+
return session.tokens.accessToken.toString();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public async getCurrentUser(): Promise<CurrentUser> {
|
|
42
|
+
const session = await fetchAuthSession();
|
|
43
|
+
const idToken = session.tokens.idToken.payload;
|
|
44
|
+
return CurrentUser.fromCognitoUser(idToken);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public async forceRefresh(): Promise<void> {
|
|
48
|
+
await fetchAuthSession({ forceRefresh: true });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public async loginSSO(loginProvider: string): Promise<void> {
|
|
52
|
+
await signInWithRedirect({ provider: { custom: loginProvider } })
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public async loginEmail(request: ISignInLinkRequest): Promise<void> {
|
|
56
|
+
await requestSignInLink(request);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public async finishLoginEmail(request: IRedeemSignInLink): Promise<void> {
|
|
60
|
+
await redeemSignInLink(request);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public async signOut(): Promise<void> {
|
|
64
|
+
await amplifySignOut();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public registerAuthEventHandler(options: AuthEventHandlerOptions): () => void {
|
|
68
|
+
return Hub.listen('auth', (data) => amplifyAuthEventHandler(data, options), 'authentication-provider');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AuthHubEventData } from "@aws-amplify/core/src/Hub/types/AuthTypes";
|
|
2
|
+
import { AuthEventHandlerOptions } from "@cirrobio/react-core";
|
|
3
|
+
|
|
4
|
+
export const amplifyAuthEventHandler = ({ payload }, options: AuthEventHandlerOptions): void => {
|
|
5
|
+
const { event }: AuthHubEventData = payload;
|
|
6
|
+
const { onSignIn, onSignOut } = options ?? {};
|
|
7
|
+
switch (event) {
|
|
8
|
+
case 'signedIn': {
|
|
9
|
+
console.log('user has signed in successfully');
|
|
10
|
+
onSignIn();
|
|
11
|
+
break;
|
|
12
|
+
}
|
|
13
|
+
case 'signedOut':
|
|
14
|
+
case 'tokenRefresh_failure': {
|
|
15
|
+
onSignOut();
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
default: {
|
|
19
|
+
console.info(event);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Amplify } from "aws-amplify";
|
|
2
|
+
import { AppConfig } from "@cirrobio/react-core";
|
|
3
|
+
|
|
4
|
+
export function configureAmplify(config: AppConfig, clientIdOverride?: string): void {
|
|
5
|
+
Amplify.configure({
|
|
6
|
+
Auth: {
|
|
7
|
+
Cognito: {
|
|
8
|
+
userPoolId: config.auth.userPoolId,
|
|
9
|
+
userPoolClientId: clientIdOverride ?? config.auth.uiAppId,
|
|
10
|
+
loginWith: {
|
|
11
|
+
oauth: {
|
|
12
|
+
domain: config.auth.endpoint,
|
|
13
|
+
scopes: [
|
|
14
|
+
'phone',
|
|
15
|
+
'email',
|
|
16
|
+
'profile',
|
|
17
|
+
'openid',
|
|
18
|
+
'aws.cognito.signin.user.admin'
|
|
19
|
+
],
|
|
20
|
+
redirectSignIn: [window.location.origin],
|
|
21
|
+
redirectSignOut: [window.location.origin],
|
|
22
|
+
responseType: 'code',
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
API: {
|
|
28
|
+
GraphQL: {
|
|
29
|
+
endpoint: config.liveEndpoint,
|
|
30
|
+
region: config.region,
|
|
31
|
+
defaultAuthMode: 'userPool',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { confirmSignIn, signIn } from 'aws-amplify/auth';
|
|
2
|
+
import { IRedeemSignInLink, ISignInLinkRequest } from "@cirrobio/react-core";
|
|
3
|
+
|
|
4
|
+
export const COGNITO_PROVIDER_ID = 'COGNITO';
|
|
5
|
+
export const LOGIN_SENT_SUCCESS_MSG = 'Check your email for the login link. If you don’t have an account, the link won’t be sent.';
|
|
6
|
+
|
|
7
|
+
export async function requestSignInLink({ username, redirectUri }: ISignInLinkRequest): Promise<void> {
|
|
8
|
+
const redirectUriToUse = redirectUri || window.location.origin + "/magic-link";
|
|
9
|
+
try {
|
|
10
|
+
const { nextStep } = await signIn({
|
|
11
|
+
username,
|
|
12
|
+
password: null,
|
|
13
|
+
options: {
|
|
14
|
+
authFlowType: "CUSTOM_WITHOUT_SRP",
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
if (nextStep.signInStep !== 'CONFIRM_SIGN_IN_WITH_CUSTOM_CHALLENGE') {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const clientMetadata = {
|
|
22
|
+
signInMethod: "MAGIC_LINK",
|
|
23
|
+
redirectUri: redirectUriToUse,
|
|
24
|
+
hasMagicLink: "no",
|
|
25
|
+
}
|
|
26
|
+
await confirmSignIn({
|
|
27
|
+
challengeResponse: "cirro-is-awesome",
|
|
28
|
+
options: {
|
|
29
|
+
clientMetadata,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
} catch (e) {
|
|
33
|
+
// Cleanup the error message
|
|
34
|
+
const errorMessage = e.toString();
|
|
35
|
+
if (errorMessage.includes('UserLambdaValidationException')) {
|
|
36
|
+
throw Error(errorMessage.split('failed with error')[1]);
|
|
37
|
+
} else if (errorMessage.includes('Incorrect username or password')) {
|
|
38
|
+
throw Error('Incorrect username or password.');
|
|
39
|
+
}
|
|
40
|
+
throw Error(errorMessage);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function redeemSignInLink({ username, challenge }: IRedeemSignInLink): Promise<void> {
|
|
45
|
+
await signIn({
|
|
46
|
+
username: username,
|
|
47
|
+
password: null,
|
|
48
|
+
options: {
|
|
49
|
+
authFlowType: "CUSTOM_WITHOUT_SRP",
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
const clientMetadata = {
|
|
53
|
+
signInMethod: "MAGIC_LINK",
|
|
54
|
+
hasMagicLink: "yes",
|
|
55
|
+
}
|
|
56
|
+
await confirmSignIn({
|
|
57
|
+
challengeResponse: challenge,
|
|
58
|
+
options: {
|
|
59
|
+
clientMetadata,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { ReactElement, useCallback, useEffect, useMemo, useState } from "react";
|
|
3
|
+
import { AuthenticatorContext, AuthenticatorContextType } from './authentication-context';
|
|
4
|
+
import { AuthStatus } from "../models/auth-status";
|
|
5
|
+
import { UserDetail } from "@cirrobio/api-client";
|
|
6
|
+
import { useAppConfig } from "@cirrobio/react-core";
|
|
7
|
+
import { CurrentUser } from "@cirrobio/sdk";
|
|
8
|
+
|
|
9
|
+
export type AuthenticationProviderProps = {
|
|
10
|
+
children: React.ReactNode;
|
|
11
|
+
fetchUserInfo?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Manages the authentication state of the application.
|
|
16
|
+
* @param children - The child components to render within the provider.
|
|
17
|
+
* @param fetchUserInfo - If true, fetches detailed user information after authentication.
|
|
18
|
+
*/
|
|
19
|
+
export function AuthenticationContextProvider({ children, fetchUserInfo }: AuthenticationProviderProps): ReactElement {
|
|
20
|
+
const [authStatus, setAuthStatus] = useState<AuthStatus>('configuring');
|
|
21
|
+
const [authInfo, setAuthInfo] = useState<CurrentUser>(null);
|
|
22
|
+
const [userInfo, setUserInfo] = useState<UserDetail>(null);
|
|
23
|
+
const { dataService, authProvider } = useAppConfig();
|
|
24
|
+
|
|
25
|
+
const refresh = useCallback((): void => {
|
|
26
|
+
authProvider.getCurrentUser()
|
|
27
|
+
.then((currentUser) => {
|
|
28
|
+
setAuthStatus('authenticated');
|
|
29
|
+
setAuthInfo(currentUser);
|
|
30
|
+
if (fetchUserInfo) {
|
|
31
|
+
dataService.users.getUser({ username: currentUser.username })
|
|
32
|
+
.then((data) => setUserInfo(data))
|
|
33
|
+
.catch((error) => {
|
|
34
|
+
console.error('Failed to fetch user info:', error);
|
|
35
|
+
setUserInfo(null);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
.catch(() => {
|
|
40
|
+
setAuthStatus('unauthenticated');
|
|
41
|
+
});
|
|
42
|
+
}, []);
|
|
43
|
+
|
|
44
|
+
const value: AuthenticatorContextType = useMemo(
|
|
45
|
+
() => ({ authStatus, authInfo, refresh, userInfo }),
|
|
46
|
+
[authStatus, authInfo, userInfo, refresh]
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
// Refresh auth state on page load
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
refresh();
|
|
52
|
+
}, [refresh]);
|
|
53
|
+
|
|
54
|
+
// Refresh auth state in response to auth events
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const options = { onSignIn: refresh, onSignOut: refresh };
|
|
57
|
+
return authProvider.registerAuthEventHandler(options);
|
|
58
|
+
}, [refresh]);
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<AuthenticatorContext.Provider value={value}>
|
|
62
|
+
{children}
|
|
63
|
+
</AuthenticatorContext.Provider>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createContext } from 'react';
|
|
2
|
+
import { AuthStatus } from "../models/auth-status";
|
|
3
|
+
import { UserDetail } from "@cirrobio/api-client";
|
|
4
|
+
import { CurrentUser } from "@cirrobio/sdk";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export type AuthenticatorContextType = {
|
|
9
|
+
authStatus: AuthStatus;
|
|
10
|
+
authInfo: CurrentUser | null;
|
|
11
|
+
userInfo: UserDetail;
|
|
12
|
+
refresh: () => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const AuthenticatorContext = createContext<AuthenticatorContextType>(null);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useCallback, useContext } from 'react';
|
|
2
|
+
import { AuthenticatorContext, AuthenticatorContextType } from './authentication-context';
|
|
3
|
+
import { useAppConfig } from "@cirrobio/react-core";
|
|
4
|
+
|
|
5
|
+
type UseAuthenticator = AuthenticatorContextType & {
|
|
6
|
+
isLoggedIn: boolean;
|
|
7
|
+
signOut: () => void;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Custom hook to access authentication context
|
|
12
|
+
*/
|
|
13
|
+
export function useAuthenticator(): UseAuthenticator {
|
|
14
|
+
const context = useContext(AuthenticatorContext);
|
|
15
|
+
const { authProvider } = useAppConfig();
|
|
16
|
+
|
|
17
|
+
const signOut = useCallback(() => {
|
|
18
|
+
sessionStorage.clear();
|
|
19
|
+
authProvider.signOut().then(() => {
|
|
20
|
+
// Location reload clears any in-memory state
|
|
21
|
+
// Amplify does a hard reload automatically, but only if you are signing in from oauth / SSO
|
|
22
|
+
location.reload();
|
|
23
|
+
});
|
|
24
|
+
}, [authProvider]);
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
...context,
|
|
28
|
+
isLoggedIn: context?.authStatus === 'authenticated',
|
|
29
|
+
signOut,
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Alert, Dialog, IconButton, Stack, Typography } from '@mui/material';
|
|
2
|
+
import React, { ReactElement, useCallback, useEffect, useMemo, useState } from 'react';
|
|
3
|
+
import { LoginOptions } from './LoginOptions';
|
|
4
|
+
import { LoginProvider } from '@cirrobio/api-client';
|
|
5
|
+
import { handlePromiseError } from "@cirrobio/sdk";
|
|
6
|
+
import { useAppConfig } from "@cirrobio/react-core";
|
|
7
|
+
import { CloseOutlined } from '@mui/icons-material';
|
|
8
|
+
import { COGNITO_PROVIDER_ID, LOGIN_SENT_SUCCESS_MSG } from "../amplify/magic-link";
|
|
9
|
+
|
|
10
|
+
interface IProps {
|
|
11
|
+
onClose: () => void;
|
|
12
|
+
open: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function getLoginMessage(loginProviders: LoginProvider[]): string {
|
|
16
|
+
const loginMessage = 'Sign in using one of the options below.';
|
|
17
|
+
const cognitoEnabled = loginProviders.some(p => p.id === COGNITO_PROVIDER_ID);
|
|
18
|
+
const onlyCognito = loginProviders.length === 1 && cognitoEnabled;
|
|
19
|
+
// Only email sign in is enabled.
|
|
20
|
+
if (onlyCognito) {
|
|
21
|
+
return `Sign in with your email below.`;
|
|
22
|
+
}
|
|
23
|
+
// Both SSO and email sign in is enabled
|
|
24
|
+
if (!onlyCognito && cognitoEnabled) {
|
|
25
|
+
return `${loginMessage} If your sign in method isn't listed, please enter your email.`;
|
|
26
|
+
}
|
|
27
|
+
// Only SSO sign in is enabled
|
|
28
|
+
return loginMessage;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function LoginModal({ onClose, open }: Readonly<IProps>): ReactElement {
|
|
32
|
+
const { authProvider } = useAppConfig();
|
|
33
|
+
const loginProviders = authProvider.getLoginProviders();
|
|
34
|
+
const loginDescription = useMemo(() => getLoginMessage(loginProviders), [loginProviders]);
|
|
35
|
+
|
|
36
|
+
const [error, setError] = useState('');
|
|
37
|
+
const [message, setMessage] = useState('');
|
|
38
|
+
const [busy, setBusy] = useState(false);
|
|
39
|
+
|
|
40
|
+
const handleLogin = useCallback(async (providerId: string, email?: string): Promise<void> => {
|
|
41
|
+
setError('');
|
|
42
|
+
setMessage('');
|
|
43
|
+
setBusy(true);
|
|
44
|
+
if (providerId === COGNITO_PROVIDER_ID) {
|
|
45
|
+
try {
|
|
46
|
+
await authProvider.loginEmail({ username: email });
|
|
47
|
+
setBusy(false);
|
|
48
|
+
setMessage(LOGIN_SENT_SUCCESS_MSG);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
setError(e instanceof Error ? e.message : 'An error occurred');
|
|
51
|
+
setBusy(false);
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
authProvider.loginSSO(providerId).catch(handlePromiseError);
|
|
55
|
+
if (onClose) onClose();
|
|
56
|
+
}
|
|
57
|
+
}, [onClose]);
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
// Log in automatically if there is only one provider (and it's not Cognito)
|
|
61
|
+
const firstProviderId = loginProviders.at(0)?.id;
|
|
62
|
+
if (loginProviders.length === 1 && firstProviderId !== COGNITO_PROVIDER_ID) {
|
|
63
|
+
void handleLogin(firstProviderId);
|
|
64
|
+
}
|
|
65
|
+
}, [loginProviders, handleLogin]);
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<Dialog
|
|
69
|
+
open={open}
|
|
70
|
+
onClose={onClose}
|
|
71
|
+
PaperProps={{ sx: { p: 3, background: "#FFF", width: '480px' } }}
|
|
72
|
+
|
|
73
|
+
aria-label="login dialog"
|
|
74
|
+
>
|
|
75
|
+
<Stack alignItems="left" justifyContent="space-between" gap={1} direction={{ xs: 'row' }}>
|
|
76
|
+
<Stack direction="column">
|
|
77
|
+
<Typography id="dialog-title" variant="h4" color="secondary">
|
|
78
|
+
Login
|
|
79
|
+
</Typography>
|
|
80
|
+
<Typography variant="body2">
|
|
81
|
+
{loginDescription}
|
|
82
|
+
</Typography>
|
|
83
|
+
</Stack>
|
|
84
|
+
{!!onClose &&
|
|
85
|
+
<Stack alignItems="right" direction="row" gap={0}>
|
|
86
|
+
<IconButton
|
|
87
|
+
aria-label="Close"
|
|
88
|
+
onClick={() => onClose()}
|
|
89
|
+
size="small"
|
|
90
|
+
color="secondary"
|
|
91
|
+
sx={{ transform: 'translate(10px, 0px)' }}>
|
|
92
|
+
<CloseOutlined sx={{ fontSize: '22px' }} />
|
|
93
|
+
</IconButton>
|
|
94
|
+
</Stack>
|
|
95
|
+
}
|
|
96
|
+
</Stack>
|
|
97
|
+
<Stack sx={{ pt: 0 }}>
|
|
98
|
+
<LoginOptions loginProviders={loginProviders} onSelect={handleLogin} busy={busy} success={!!message} />
|
|
99
|
+
{error && (
|
|
100
|
+
<Alert severity="error" sx={{ mt: 4 }}>{error}</Alert>
|
|
101
|
+
)}
|
|
102
|
+
{message && (
|
|
103
|
+
<Alert severity="success" sx={{ mt: 4 }}>{message}</Alert>
|
|
104
|
+
)}
|
|
105
|
+
</Stack>
|
|
106
|
+
</Dialog>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import React, { Children, ReactElement, useMemo, useState } from 'react';
|
|
2
|
+
import { Button, Divider, Stack, TextField } from '@mui/material';
|
|
3
|
+
import { LoginProvider } from '@cirrobio/api-client';
|
|
4
|
+
import { LoadingButton } from '@mui/lab';
|
|
5
|
+
import { COGNITO_PROVIDER_ID } from "../amplify/magic-link";
|
|
6
|
+
|
|
7
|
+
interface IProps {
|
|
8
|
+
loginProviders: LoginProvider[];
|
|
9
|
+
onSelect: (providerId: string, email?: string) => void;
|
|
10
|
+
busy: boolean;
|
|
11
|
+
success: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export function LoginOptions({ loginProviders, onSelect, busy, success }: Readonly<IProps>): ReactElement {
|
|
16
|
+
const [email, setEmail] = useState('');
|
|
17
|
+
|
|
18
|
+
const ssoProviders = useMemo(() =>
|
|
19
|
+
loginProviders.filter(p => p.id !== COGNITO_PROVIDER_ID), [loginProviders]);
|
|
20
|
+
const cognitoEnabled = useMemo(() =>
|
|
21
|
+
loginProviders.some(p => p.id === COGNITO_PROVIDER_ID), [loginProviders]);
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<Stack alignItems="center" gap={3} direction="column" sx={{ pt: 2 }}>
|
|
25
|
+
{Children.toArray(ssoProviders.map(provider => {
|
|
26
|
+
return (<Button
|
|
27
|
+
sx={{ p: 2, width: '100%' }}
|
|
28
|
+
variant="contained"
|
|
29
|
+
color="secondary"
|
|
30
|
+
fullWidth={true}
|
|
31
|
+
startIcon={<img style={{ maxHeight: '17px' }} alt={provider.name} src={provider.logoUrl} />}
|
|
32
|
+
onClick={() => onSelect(provider.id)}
|
|
33
|
+
>{provider.name}</Button>)
|
|
34
|
+
}))}
|
|
35
|
+
{cognitoEnabled && (
|
|
36
|
+
<form style={{ width: '100%' }}>
|
|
37
|
+
<Stack alignItems="center" gap={3} direction="column">
|
|
38
|
+
{ssoProviders.length > 0 && (<Divider textAlign="center" color="secondary" flexItem>OR</Divider>)}
|
|
39
|
+
<TextField
|
|
40
|
+
label="Email"
|
|
41
|
+
size="medium"
|
|
42
|
+
value={email}
|
|
43
|
+
variant="outlined"
|
|
44
|
+
autoComplete="off"
|
|
45
|
+
type="email"
|
|
46
|
+
fullWidth={true}
|
|
47
|
+
required
|
|
48
|
+
onChange={(e) => setEmail(e.target.value)}
|
|
49
|
+
/>
|
|
50
|
+
<LoadingButton
|
|
51
|
+
type="submit"
|
|
52
|
+
sx={{ p: 2, width: '100%' }}
|
|
53
|
+
loading={busy}
|
|
54
|
+
disabled={!email?.trim() || success}
|
|
55
|
+
variant="contained"
|
|
56
|
+
color="secondary"
|
|
57
|
+
fullWidth={true}
|
|
58
|
+
onClick={() => onSelect(COGNITO_PROVIDER_ID, email)}
|
|
59
|
+
>
|
|
60
|
+
Sign in with email
|
|
61
|
+
</LoadingButton>
|
|
62
|
+
</Stack>
|
|
63
|
+
</form>
|
|
64
|
+
)}
|
|
65
|
+
</Stack>
|
|
66
|
+
)
|
|
67
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { useAuthenticator } from "../auth-context/useAuthenticator";
|
|
3
|
+
import { LoginModal } from "./LoginModal";
|
|
4
|
+
|
|
5
|
+
interface IProps {
|
|
6
|
+
children?: React.ReactNode;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* LoginWrapper component is used to conditionally render children based on the authentication status
|
|
11
|
+
* or display a login modal if the user is unauthenticated.
|
|
12
|
+
*/
|
|
13
|
+
export function LoginWrapper({ children }: IProps) {
|
|
14
|
+
const { authStatus } = useAuthenticator();
|
|
15
|
+
|
|
16
|
+
if (!authStatus || authStatus === 'configuring') {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (authStatus === 'unauthenticated') {
|
|
21
|
+
return <LoginModal onClose={null} open={true} />
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return children;
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { AmplifyAuthProvider } from './amplify/amplify-auth-provider';
|
|
2
|
+
export type { AuthStatus } from './models/auth-status';
|
|
3
|
+
|
|
4
|
+
// Components
|
|
5
|
+
export { LoginModal } from './components/LoginModal';
|
|
6
|
+
export { LoginOptions } from './components/LoginOptions';
|
|
7
|
+
export { LoginWrapper } from './components/LoginWrapper';
|
|
8
|
+
|
|
9
|
+
export { AuthenticationContextProvider } from './auth-context/authentication-context-provider';
|
|
10
|
+
export { useAuthenticator } from './auth-context/useAuthenticator';
|
|
11
|
+
|
|
12
|
+
export { StaticInteractiveAuthTokenProvider } from './static/static-token-provider';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type AuthStatus = 'configuring' | 'authenticated' | 'unauthenticated';
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { StaticTokenAuthProvider } from "@cirrobio/sdk";
|
|
2
|
+
import { InteractiveAuthenticationProvider } from "@cirrobio/react-core";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* StaticInteractiveAuthTokenProvider is a simple implementation of the InteractiveAuthenticationProvider
|
|
7
|
+
* that uses a static token for authentication. This is useful for testing or when you have a known token.
|
|
8
|
+
*/
|
|
9
|
+
export class StaticInteractiveAuthTokenProvider
|
|
10
|
+
extends StaticTokenAuthProvider
|
|
11
|
+
implements InteractiveAuthenticationProvider {
|
|
12
|
+
|
|
13
|
+
constructor(token: string) {
|
|
14
|
+
super(token);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async getAccessToken(): Promise<string> {
|
|
18
|
+
return this.token;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
loginSSO(): Promise<void> {
|
|
22
|
+
return Promise.resolve();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
loginEmail(_): Promise<void> {
|
|
26
|
+
return Promise.resolve();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
finishLoginEmail(_): Promise<void> {
|
|
30
|
+
return Promise.resolve();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
signOut(): Promise<void> {
|
|
34
|
+
return Promise.resolve();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getLoginProviders() {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
registerAuthEventHandler(_): void {
|
|
42
|
+
// No-op for static token provider
|
|
43
|
+
}
|
|
44
|
+
}
|