Add response codes to login result
							parent
							
								
									40d13f25de
								
							
						
					
					
						commit
						48885a8604
					
				| 
						 | 
					@ -98,7 +98,11 @@ export async function login(
 | 
				
			||||||
    await fetchPlatformData();
 | 
					    await fetchPlatformData();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    shared.setIsLogged(true);
 | 
					    shared.setIsLogged(true);
 | 
				
			||||||
    return new LoginResult(true, `${username} already authenticated (session)`);
 | 
					    return new LoginResult(
 | 
				
			||||||
 | 
					      true,
 | 
				
			||||||
 | 
					      LoginResult.ALREADY_AUTHENTICATED,
 | 
				
			||||||
 | 
					      `${username} already authenticated (session)`
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Creating credentials and fetch unique platform token
 | 
					  // Creating credentials and fetch unique platform token
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,17 +9,37 @@
 | 
				
			||||||
 * Object obtained in response to an attempt to login to the portal.
 | 
					 * Object obtained in response to an attempt to login to the portal.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export default class LoginResult {
 | 
					export default class LoginResult {
 | 
				
			||||||
 | 
					  //#region Login result codes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  static REQUIRE_2FA = 100;
 | 
				
			||||||
 | 
					  static AUTH_SUCCESSFUL = 200;
 | 
				
			||||||
 | 
					  static AUTH_SUCCESSFUL_2FA = 201;
 | 
				
			||||||
 | 
					  static ALREADY_AUTHENTICATED = 202;
 | 
				
			||||||
 | 
					  static UNKNOWN_ERROR = 400;
 | 
				
			||||||
 | 
					  static INCORRECT_CREDENTIALS = 401;
 | 
				
			||||||
 | 
					  static INCORRECT_2FA_CODE = 402;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  //#endregion Login result codes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  //#region Properties
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Result of the login operation
 | 
					   * Result of the login operation
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  success: boolean;
 | 
					  readonly success: boolean;
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * Code associated with the result of the login operation.
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  readonly code: number;
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Login response message
 | 
					   * Login response message
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  message: string;
 | 
					  readonly message: string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor(success: boolean, message: string) {
 | 
					  //#endregion Properties
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  constructor(success: boolean, code: number, message?: string) {
 | 
				
			||||||
    this.success = success;
 | 
					    this.success = success;
 | 
				
			||||||
 | 
					    this.code = code;
 | 
				
			||||||
    this.message = message;
 | 
					    this.message = message;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -129,6 +129,7 @@ export async function authenticate(
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
    authResult = new LoginResult(
 | 
					    authResult = new LoginResult(
 | 
				
			||||||
      false,
 | 
					      false,
 | 
				
			||||||
 | 
					      LoginResult.UNKNOWN_ERROR,
 | 
				
			||||||
      `Error ${e.message} while authenticating`
 | 
					      `Error ${e.message} while authenticating`
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -169,7 +170,13 @@ export async function send2faCode(
 | 
				
			||||||
    const message = result
 | 
					    const message = result
 | 
				
			||||||
      ? "Authentication successful"
 | 
					      ? "Authentication successful"
 | 
				
			||||||
      : r.data.errors.join(",");
 | 
					      : r.data.errors.join(",");
 | 
				
			||||||
    return new LoginResult(result, message);
 | 
					    const code = result
 | 
				
			||||||
 | 
					      ? LoginResult.AUTH_SUCCESSFUL_2FA
 | 
				
			||||||
 | 
					      : message ===
 | 
				
			||||||
 | 
					        "The two-step verification value could not be confirmed. Please try again"
 | 
				
			||||||
 | 
					      ? LoginResult.INCORRECT_2FA_CODE
 | 
				
			||||||
 | 
					      : LoginResult.UNKNOWN_ERROR;
 | 
				
			||||||
 | 
					    return new LoginResult(result, code, message as string);
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -358,6 +365,7 @@ function manageLoginPOSTResponse(response: AxiosResponse<any>) {
 | 
				
			||||||
  if (response.config.url.startsWith(urls.F95_2FA_LOGIN)) {
 | 
					  if (response.config.url.startsWith(urls.F95_2FA_LOGIN)) {
 | 
				
			||||||
    return new LoginResult(
 | 
					    return new LoginResult(
 | 
				
			||||||
      false,
 | 
					      false,
 | 
				
			||||||
 | 
					      LoginResult.REQUIRE_2FA,
 | 
				
			||||||
      "Two-factor authentication is needed to continue"
 | 
					      "Two-factor authentication is needed to continue"
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -371,7 +379,12 @@ function manageLoginPOSTResponse(response: AxiosResponse<any>) {
 | 
				
			||||||
  // Return the result of the authentication
 | 
					  // Return the result of the authentication
 | 
				
			||||||
  const result = errorMessage.trim() === "";
 | 
					  const result = errorMessage.trim() === "";
 | 
				
			||||||
  const message = result ? "Authentication successful" : errorMessage;
 | 
					  const message = result ? "Authentication successful" : errorMessage;
 | 
				
			||||||
  return new LoginResult(result, message);
 | 
					  const code = result
 | 
				
			||||||
 | 
					    ? LoginResult.AUTH_SUCCESSFUL
 | 
				
			||||||
 | 
					    : message === "Incorrect password. Please try again."
 | 
				
			||||||
 | 
					    ? LoginResult.INCORRECT_CREDENTIALS
 | 
				
			||||||
 | 
					    : LoginResult.UNKNOWN_ERROR;
 | 
				
			||||||
 | 
					  return new LoginResult(result, code, message);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//#endregion
 | 
					//#endregion
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue