Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link

FirebaseAuthentication Class in Android

FirebaseAuthentication Class in Android

Firebase login, logout, and signup operation class in an Android application


public class FirebaseAuthentication {

    private FirebaseAuth firebaseAuth;

    public FirebaseAuthentication() {
        firebaseAuth = FirebaseAuth.getInstance();
    }

    public void login(String email, String password, final OnLoginCompleteListener listener) {
        firebaseAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            listener.onLoginSuccess();
                        } else {
                            listener.onLoginFailed(task.getException().getMessage());
                        }
                    }
                });
    }

    public void logout() {
        firebaseAuth.signOut();
    }

    public void signup(String email, String password, final OnSignupCompleteListener listener) {
        firebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            listener.onSignupSuccess();
                        } else {
                            listener.onSignupFailed(task.getException().getMessage());
                        }
                    }
                });
    }

    public interface OnLoginCompleteListener {
        void onLoginSuccess();

        void onLoginFailed(String errorMessage);
    }

    public interface OnSignupCompleteListener {
        void onSignupSuccess();

        void onSignupFailed(String errorMessage);
    }
}

        

To use this class, create an instance of it in your activity or fragment and call the appropriate method with the required parameters. Here's an example of using this class to login, logout, and signup:


FirebaseAuthentication firebaseAuthentication = new FirebaseAuthentication();

// Login
firebaseAuthentication.login("user@example.com", "password", new FirebaseAuthentication.OnLoginCompleteListener() {
    @Override
    public void onLoginSuccess() {
        // Handle login success
    }

    @Override
    public void onLoginFailed(String errorMessage) {
        // Handle login failure
    }
});

// Logout
firebaseAuthentication.logout();

// Signup
firebaseAuthentication.signup("user@example.com", "password", new FirebaseAuthentication.OnSignupCompleteListener() {
    @Override
    public void onSignupSuccess() {
        // Handle signup success
    }

    @Override
    public void onSignupFailed(String errorMessage) {
        // Handle signup failure
    }
});

        

In this example, we create a new instance of the FirebaseAuthentication class, call the login() method to login, call the logout() method to logout, and call the signup() method to signup. To handle login or signup events, you can implement the OnLoginCompleteListener or OnSignupCompleteListener interface and pass an instance of it to the login() or signup() method. Inside the onLoginSuccess(), onLoginFailed(), onSignupSuccess(), or onSignupFailed() methods, you can do something after the login or signup process completes.

Post a Comment