
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
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
authentication flowYou 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, passwordConfirm);
// 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, newPasswordConfirm);
// Password successfully changed
} catch (error) {
// Handle password change error
}
try {
await resendConfirmationCode(username);
// Confirmation code sent
} catch (error) {
// Handle 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 passwordThis 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 1 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
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.