
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
react-cognito-auth-hook
Advanced tools
Package for handling and managing user authentication in react applications
A React hook for handling AWS Cognito authentication flows with built-in token management and persistence.
npm install react-cognito-auth-hook
or
yarn add react-cognito-auth-hook
Before using this package, you need:
USER_PASSWORD_AUTH
, USER_AUTH
authentication flowsYou will need the following information from your AWS Cognito setup:
First, create a configuration object with your AWS Cognito details:
const config = {
region: "YOUR_AWS_REGION",
userPoolId: "YOUR_USER_POOL_ID",
clientId: "YOUR_CLIENT_ID",
};
import { useCognitoAuth } from 'react-cognito-auth-hook';
function App() {
const {
authData,
authState,
login,
initSignUp,
confirmSignUp,
signOut
} = useCognitoAuth({ config });
// Check authentication state
if (authState === 'AUTHENTICATED') {
return (
<div>
<h1>Welcome, {authData?.username}!</h1>
<button onClick={signOut}>Sign Out</button>
</div>
);
}
return (
<div>
<h1>Please log in</h1>
{/* Your login/signup forms here */}
</div>
);
}
try {
await login(username, password);
// User is now logged in
} catch (error) {
// Handle login error
}
try {
// Initialize sign up
await initSignUp(username, password);
// Confirm sign up with code
await confirmSignUp(username, confirmationCode);
// User is now signed up and logged in
} catch (error) {
// Handle signup error
}
try {
await changePassword(currentPassword, newPassword);
// Password successfully changed
} catch (error) {
// Handle password change error
}
try {
await resendConfirmationCode(username);
// Confirmation code sent
} catch (error) {
// Handle error
}
try {
// Initialize forgot password flow
await initForgotPassword(username);
// This will trigger Cognito to send a confirmation code
// Confirm new password with code
await confirmForgotPassword(username, newPassword, confirmationCode);
// Password has been reset successfully
} catch (error) {
// Handle forgot password error
}
The hook provides the following authentication states:
INIT
: Initial loading stateAUTHENTICATED
: User is authenticatedUNAUTHENTICATED
: User is not authenticatedauthData
: Object containing the current authentication data (tokens, username)authState
: Current authentication statelogin(username: string, password: string)
: Login with username and passwordinitSignUp(username: string, password: string, passwordConfirm: string)
: Start the signup processconfirmSignUp(username: string, code: string, callback?: Function)
: Confirm signup with verification coderesendConfirmationCode(username: string)
: Resend verification codesignOut()
: Sign out the current userchangePassword(prevPassword: string, newPassword: string, newPasswordConfirm: string)
: Change user's passwordinitForgotPassword(username: string)
: Start the forgot password processconfirmForgotPassword(username: string, newPassword: string, code: string)
: Confirm new password with verification codeThis package handles authentication tokens securely by:
MIT
Contributions are welcome! Please feel free to submit a Pull Request.
FAQs
Package for handling and managing user authentication in react applications
The npm package react-cognito-auth-hook receives a total of 0 weekly downloads. As such, react-cognito-auth-hook popularity was classified as not popular.
We found that react-cognito-auth-hook demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.