clean-authentication
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

#Clean Authentication

Installation

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

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

API

AuthenticationService

isAuthenticated(): boolean

getAuthenticatedUser(): NullableUser

logout(): Promise<AuthenticationServiceResult>

loginWithEmailPassword(email: string, password: string): Promise<AuthenticationServiceResult>

registerWithEmailPassword(email: string, password: string): Promise<AuthenticationServiceResult>

sendPasswordResetEmail(email: string): Promise<AuthenticationServiceResult>

sendEmailVerification(): Promise<AuthenticationServiceResult>

setPersistence(persistence: AuthenticationPersistence): Promise<AuthenticationServiceResult>

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

setLanguageCode(code: string): void

updatePassword(newPassword: string): Promise<AuthenticationServiceResult>

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>;
        

Readme

Keywords

none

Package Sidebar

Install

npm i clean-authentication

Weekly Downloads

6

Version

0.4.0

License

ISC

Unpacked Size

39.6 kB

Total Files

18

Last publish

Collaborators

  • tontransport.com