clean-authentication
v0.4.0
Published
Simple Authentication Service supporting firebase
Downloads
5
Readme
#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('[email protected]', '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>;