@ttoss/auth
📚 About
This module handles auth in your applications and other ttoss modules.
This module is intended to use with AWS Cognito. It uses AWS Amplify under the hood.
Amplify Auth configuration must be provided in your App to make Auth Module works properly.
🚀 Getting Started
Install
$ yarn add @ttoss/auth and yarn add @ttoss/notifications
# or
$ npm install @ttoss/auth and npm install @ttoss/notifications
📄 Examples of use
Amplify config
import Amplify from 'aws-amplify';
Amplify.configure({
Auth: {
identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab',
region: 'XX-XXXX-X',
identityPoolRegion: 'XX-XXXX-X',
userPoolId: 'XX-XXXX-X_abcd1234',
userPoolWebClientId: 'a1b2c3d4e5f6g7h8i9j0k1l2m3',
mandatorySignIn: false,
cookieStorage: {
domain: '.yourdomain.com',
path: '/',
expires: 365,
sameSite: 'strict' | 'lax',
secure: true,
},
storage: MyStorage,
authenticationFlowType: 'USER_PASSWORD_AUTH',
clientMetadata: { myCustomKey: 'myCustomValue' },
oauth: {
domain: 'your_cognito_domain',
scope: [
'phone',
'email',
'profile',
'openid',
'aws.cognito.signin.user.admin',
],
redirectSignIn: 'http://localhost:3000/',
redirectSignOut: 'http://localhost:3000/',
responseType: 'code',
},
},
});
PrivateRoute component
import { useAuth } from '@ttoss/auth';
const PrivateRoute = (props: any) => {
const { isAuthenticated } = useAuth();
if (!isAuthenticated) {
return <Navigate to="/login" state={{ redirectTo: props.path || '/' }} />;
}
return <Route {...props} />;
};
Login Page
import { Auth, useAuth } from '@ttoss/auth';
const Login = () => {
const auth = useAuth();
const onSuccess = () => {
};
return (
<div>
<h1>Login Page</h1>
<Auth onSignIn={onSuccess} />
<button onClick={auth.signOut}>Logout</button>
</div>
);
};
export default Login;
Auth with Progressbar
import { AuthProvider } from '@ttoss/auth';
import { NotificationsProvider } from '@ttoss/notifications';
ReactDOM.render(
<React.StrictMode>
<NotificationsProvider>
<AuthProvider>
<App />
</AuthProvider>
</NotificationsProvider>
</React.StrictMode>,
document.getElementById('root')
);
Types
export type OnSignInInput = {
email: string;
password: string;
};
export type OnSignIn = (input: OnSignInInput) => void;
export type OnSignUpInput = {
email: string;
password: string;
};
export type OnSignUp = (input: OnSignUpInput) => void;
export type OnConfirmSignUp = (input: { email: string; code: string }) => void;