simple-firebase-auth-hook
v0.2.4
Published
A simple package for handling authentication with Firebase in a React/ React Native application.
Downloads
9
Readme
simple-firebase-auth-hook
A simple package for handling authentication with Firebase in a React/ React Native application.
Installation
To install the simple-firebase-auth-hook
package, use the following command:
npm install simple-firebase-auth-hook
or
yarn add simple-firebase-auth-hook
Usage
- Initialize using :
const firebaseConfig = {
apiKey: 'YOUR FIREBASE KEY',
// .... other configurations
};
var firebaseApp = initializeApp({ ...firebaseConfig });
const { initialize } = useFirebaseAuthentication();
initializeFireBaseAuth(app);
- Use the
useAuthentication
hook to manage authentication in your components:
const {
initialize,
user,
logout,
login,
sendResetPasswordEmail,
removeUser,
isBusy,
changePassword,
register,
username,
password,
firebaseApp,
} = useFirebaseAuthentication();
- Call the necessary functions based on your application's authentication flow:
// Example usage
const handleLogin = async () => {
const email = '[email protected]';
const password = 'password';
const saveEmail = true;
const savePassword = true;
const result = await login(email, password, saveEmail, savePassword);
if (result) {
// User logged in successfully
} else {
// Login failed
}
};
Functions
initializeFireBaseAuth(firebaseApp: FirebaseApp): void
Initializes the Firebase authentication with the provided firebaseApp
.
logout(): Promise<void>
Logs out the current user.
login(email: string, password: string, saveEmail: boolean = false, savePassword: boolean = false): Promise<UserCredential | undefined>
Logs in the user with the provided email
and password
. Optionally, saves the email and password if saveEmail
and savePassword
are set to true
, respectively.
sendResetPasswordEmail(email: string, onSuccess: () => void, onFailure: (error: any) => void): Promise<void>
Sends a password reset email to the provided email
. Calls onSuccess
on successful email sending and onFailure
on failure.
removeUser(password: string, firebaseApp: FirebaseApp): Promise<boolean>
Removes the current user's account. Requires the user's password
for reauthentication. Returns true
if the user is successfully removed, false
otherwise.
isBusy: boolean
A boolean flag indicating whether an authentication operation is in progress.
changePassword(oldPassword: string, newPassword: string): Promise<{ success: boolean; message?: string }>
Changes the current user's password from oldPassword
to newPassword
. Returns an object with the success
flag indicating the success of the operation and an optional message
field containing an error message if the operation fails.
register(firstName: string, lastName: string, email: string, password: string): Promise<UserCredential>
Registers a new user with the provided firstName
, lastName
, email
, and password
. Returns a UserCredential
object for the newly created user.
user: User | undefined | null
The currently authenticated user, or undefined
if not authenticated.
username: string
The saved username, if available.
password: string
The saved password, if available.
firebaseApp: FirebaseApp | undefined
The Firebase app instance used for authentication.
License
This package is open source and available under the MIT License.