All files / services/api/auth resetPassword.tsx

91.66% Statements 11/12
100% Branches 5/5
100% Functions 2/2
91.66% Lines 11/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                    9x 2x 2x 1x   1x                 9x 2x 2x         2x 1x   1x        
import axios, { isAxiosError } from 'axios'
 
export type ForgotPasswordResponse = 'OK' | 'ERROR'
export type ChangePasswordResponse = 'OK' | 'TOKEN_INVALID' | 'ERROR'
 
/**
 * Request for sending a reset password email.
 * @param email the email address to send the reset password email to.
 * @returns an object containing the status code, headline and message use to display an alert.
 */
export const forgotPassword = async (email: string): Promise<ForgotPasswordResponse> => {
	try {
		await axios.post(`${process.env.NEXT_PUBLIC_API_BASE_URL}/auth/forgot-password`, { email })
		return 'OK'
	} catch (error) {
		return 'ERROR'
	}
}
 
/**
 * Request for changing the password.
 * @param password the new password.
 * @returns an object containing the status code, headline and message use to display an alert.
 */
export const changePassword = async (password: string, token: string): Promise<ChangePasswordResponse> => {
	try {
		await axios.post(`${process.env.NEXT_PUBLIC_API_BASE_URL}/auth/forgot-password-submit?token=${token}`, {
			password,
		})
		return 'OK'
	} catch (error) {
		if (isAxiosError(error) && error.response && error.response.status === 401) {
			return 'TOKEN_INVALID'
		} else {
			return 'ERROR'
		}
	}
}