0.4.0 • Published 4 years ago

clean-authentication v0.4.0

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

#Clean Authentication

Installation

###npm npm install --save clean-authentication

###yarn yarn install --save clean-authentication

API

AuthenticationService

isAuthenticated(): boolean

getAuthenticatedUser(): NullableUser

logout(): Promise\

loginWithEmailPassword(email: string, password: string): Promise\

registerWithEmailPassword(email: string, password: string): Promise\

sendPasswordResetEmail(email: string): Promise\

sendEmailVerification(): Promise\

setPersistence(persistence: AuthenticationPersistence): Promise\

subscribe(subscriber: (payload: AuthenticationPayload) => void): Subscription

setLanguageCode(code: string): void

updatePassword(newPassword: string): Promise\

Validation

const result = await authenticationService.loginWithEmailPassword('my-email@provider.com', 'my-password');
if (result.success) {
    // User logged in, you can redirect
} else {
    if (result.error.type === AuthenticationServiceErrors.WRONG_PASSWORD) { // Invalid password
        alert(result.error.message); // toast or set password field in error state
    } // else if other errors...
}

Available errors:

NOT_AUTHENTICATED
INVALID_EMAIL
USER_DISABLED
USER_NOT_FOUND
WRONG_PASSWORD
EMAIL_ALREADY_IN_USE
INTERNAL_ERROR
WEAK_PASSWORD

Usage in React

Usage with Context

Setup provider

<AuthenticationServiceContext.Provider value={new FirebaseAuthenticationService()}>
    { /* app here */ }
</AuthenticationServiceContext.Provider>

Get service, user, and isAuthenticated(boolean) with hook

const {user, isAuthenticated, authenticationService} = useAuthenticationService();

return isAuthenticated
    ? <>
        Email: {user.email}
        <button onClick={async () => authenticationService.logout()}>Logout</button>
    </>
    : <button onClick={async () => authenticationService.loginWithEmailPassword("myUsername", "myPassword")}>
        Login
    </button>;
        
0.4.0

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago