All files / services/api/auth verifyMail.ts

66.66% Statements 8/12
100% Branches 5/5
50% Functions 1/2
66.66% Lines 8/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                53x 4x 4x   1x   3x         1x   2x               53x                  
import axios, { isAxiosError } from 'axios'
import { ApiErrorResponse } from '../../../types/api'
 
/**
 * Verify email request.
 * @param token - Token to verify email comes from param.
 * @returns Axios response.
 */
export const verifyMail = async (token: string) => {
	try {
		await axios.get(`${process.env.NEXT_PUBLIC_API_BASE_URL}/auth/verify-email?token=${token}`)
 
		return 'OK'
	} catch (error) {
		if (
			isAxiosError<ApiErrorResponse>(error) &&
			error.response &&
			error.response.data.message === 'This user already verified their email'
		) {
			return 'ALREADY_VERIFIED'
		}
		return 'ERROR'
	}
}
 
/**
 * Request a new verify mail.
 * @returns ok or error
 */
export const requestVerifyMail = async () => {
	try {
		await axios.get(`${process.env.NEXT_PUBLIC_API_BASE_URL}/auth/request-verify-email`)
 
		return 'OK'
	} catch (error) {
		return 'ERROR'
	}
}