CodeLeap Docs

CommonWebMobileCLIConceptsUpdates

AppAuthErrors

Tools

Handling auth errors

Source

View source code

Package

@codeleap/common

What is it

Handle authentication errors simply and easily. It offers several errors by default with personalized messages, in addition to being able to customize these standard errors and add new errors.

How to use

This code can be used for mobile and web.

Implementation

import { AppAuthErrors } from '@codeleap/common'
export const AuthErrors = new AppAuthErrors({}, (err, module, args) => {
// onError event handler
})

Standard errors and adding new errors

The standard errors that are recorded in AppAuthErrors are:

export const DEFAULT_AUTH_ERRORS = {
'auth/wrong-password': 'Email or password is incorrect',
'auth/not-registered': 'This user is not registered',
'auth/requires-recent-login': 'You need to log in again to continue',
'auth/invalid-login-credentials': 'Email or password is incorrect',
'auth/too-many-requests': 'Access to this account has been temporarily disabled due to many failed login attempts.',
'auth/email-in-use': 'This email address is already taken',
'auth/email-not-found': 'Could not find an account matching the specified email address',
'auth/email-already-in-use': 'This email address is already taken',
'auth/invalid-email': 'Email or password is incorrect',
'auth/user-disabled': 'Could not find an account with the specified email address and password',
'auth/user-not-found': 'Email or password is incorrect',
}

You can customize these errors or add new ones like this:

const errors = {
'auth/new-error': 'Added a new error',
'auth/user-not-found': 'Overwriting a standard error'
}
export const AuthErrors = new AppAuthErrors(errors, () => {})

Social login errors are separate, but follow the same logic as common errors.

export const DEFAULT_SOCIAL_AUTH_ERRORS = {
'12501': null,
'EUNSPECIFIED': null,
'facebook-login-canceled': null,
'-5': null,
'1001': null,
}
const errors = {
'social': {
'auth/social-new-error': 'Added a new error',
'EUNSPECIFIED': 'Overwriting a standard error',
}
}
export const AuthErrors = new AppAuthErrors(errors, () => {})

You can also pass a function that returns the message, this is useful if you want to pass an argument and show different messages for the same error based on the arguments, or to consider i18N, because when the message is executed, it will get user translation.

const errors = {
'auth/wrong-password': (args) => {
if (args) {
return 'Message' + args
} else {
return 'Message'
}
}
}
export const AuthErrors = new AppAuthErrors(errors, () => {})

Methods

AppAuthErrors returns some methods that you can use, they are as follows:

onError

Function that receives an error and handles it internally, then passing it to the handler function registered in AppAuthErrors (second argument).

The second argument is the error module and can accept any string.

The third argument is any value you want to capture in the handler, it can be used to show different messages.

AuthErrors.onError(err, 'login', args)

exception

Creates a throw exception with the error you pass as an argument.

AuthErrors.exception('auth/wrong-password')

verifyError

Checks if the error is registered within AppAuthErrors, returns a boolean.

AuthErrors.verifyError('auth/wrong-password')

getError

Returns the error or null if it is not registered.

AuthErrors.getError('auth/wrong-password')

isError

Checks for an error, this is mainly used for try...catch returns, the error capture can be passed as an argument and validation will be done.

AuthErrors.isError(err, 'auth/wrong-password')

Example of use

export const AuthErrors = new AppAuthErrors(errors, (err, module, args) => {
// It is necessary to pass the args to the message
const msg = err?.msg?.(args)
if (!!err && !!msg) {
OSAlert.error({ title: I18N.t('error'), body: msg })
}
logger.error(`${module ?? 'Auth'} Error`, err, 'Auth')
})
const login = async (data: Profile) => {
try {
const { email, password } = data
const emailAvailable = await isEmailAvailable(email)
if (!emailAvailable) {
AuthErrors.exception('auth/email-not-found')
}
const firebase = await getFirebase()
await firebase.auth().signInWithEmailAndPassword(email, password)
} catch (err) {
AuthErrors.onError(err, 'login')
}
}

Table of contents

What is it
How to use