#Clean Authentication
###npm
npm install --save clean-authentication
###yarn
yarn install --save clean-authentication
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>
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
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>;