All files / src/helpers/httpClient/queries/auth auth.ts

0% Statements 0/26
0% Branches 0/12
0% Functions 0/6
0% Lines 0/26

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165                                                                                                                                                                                                                                                                                                                                         
/**
 * @fileoverview Authentication helper functions.
 * @module Auth
 * @description Helper functions for authentication.
 */
 
import { Header } from '@global/types/httpClient/Header'
import RegisterJSON from '@global/types/httpClient/auth/Registration'
import ConnectionJSON from '@global/types/httpClient/auth/Connection'
import ChangePasswordJSON from '@global/types/httpClient/auth/ChangePassword'
import { post } from '../../common'
 
interface SubscribeProps {
  url?: string
  email: string
  username?: string
  password: string
  headers?: Header
}
 
interface ConnectProps {
  url?: string
  formUsername: string
  password: string
  headers?: Header
}
interface ForgotPasswordProps {
  url?: string
  email: string
  headers?: Header
}
 
interface ChangePasswordProps {
  url?: string
  email: string
  newPassword: string
  headers?: Header
}
 
/**
 * @function subscribe Send the user's email and password to the server to subscribe them to the app.
 * @param param Object containing the url, email, username, password, and headers.
 * @param param.url The url to send the request to.
 * @param param.email The user's email.
 * @param param.username The user's username.
 * @param param.password The user's password.
 * @param param.headers The headers to send with the request.
 * @returns Promise of a Response object
 */
export async function subscribe({ email, username, password }: SubscribeProps): Promise<RegisterJSON> {
  try {
    const response = await post({
      endpoint: '/register',
      body: JSON.stringify({
        email,
        username,
        password,
      }),
    })
 
    const responseData = await response.json()
 
    if (!response.ok) {
      throw new Error(responseData.message)
    }
 
    return {
      json: responseData,
      status: response.status,
      message: responseData.message,
    }
  } catch (error) {
    throw new Error(error instanceof Error ? error.message : String(error))
  }
}
 
/**
 * @function connect Send the user's email and password to the server to connect them to the app.
 * @param param Object containing the url, email, username, password, and headers.
 * @param param.url The url to send the request to.
 * @param param.username The user's username.
 * @param param.password The user's password.
 * @param param.headers The headers to send with the request.
 * @returns Promise of a Response object
 */
export async function connect({ formUsername, password }: ConnectProps): Promise<ConnectionJSON> {
  try {
    const response = await post({
      endpoint: '/auth/login',
      body: JSON.stringify({
        username: formUsername,
        password,
      }),
    })
 
    const responseData = await response.json()
 
    if (!response.ok) {
      throw new Error(responseData.message)
    }
 
    return {
      json: {
        id: responseData.id,
        uuid: responseData.uuid,
        username: responseData.username,
        email: responseData.email,
        picture: responseData.picture,
        telNumber: responseData.telNumber,
        role: responseData.activeProfile.role,
        accessToken: responseData.accessToken,
        createdAt: responseData.createdAt,
      },
      status: response.status,
      message: responseData.message,
    }
  } catch (error) {
    throw new Error(error instanceof Error ? error.message : String(error))
  }
}
 
/**
 * @function forgotPassword Send the user's email to the server to reset their password.
 * @param email email The user's email.
 */
export async function forgotPassword({ email }: ForgotPasswordProps): Promise<Response> {
  return await post({
    endpoint: '/auth/forgot-password',
    body: JSON.stringify({
      email,
    }),
  })
}
 
/**
 * @function changePassword Send the user's email and new password to the server to change their password.
 * @param props Object containing the email and new password.
 * @param props.email The user's email.
 * @param props.newPassword The user's new password.
 * @returns Promise of a Response object
 */
export async function changePassword({ email, newPassword }: ChangePasswordProps): Promise<ChangePasswordJSON> {
  void email
  void newPassword
  const responseStatus = Math.floor(Math.random() * 2 + 1)
  await new Promise(resolve => {
    setTimeout(() => {
      console.log('Change password')
      resolve(responseStatus === 1 ? 201 : 400)
    }, 2000)
  })
 
  if (responseStatus === 1) {
    console.log('|-> Success')
    return {
      status: 201,
      message: 'Password changed',
    }
  }
  return {
    status: 400,
    message: 'Erreur serveur',
  }
}