diff --git a/src/domain/authService.schema.ts b/src/domain/authService.schema.ts index ca14905..fde6c4c 100644 --- a/src/domain/authService.schema.ts +++ b/src/domain/authService.schema.ts @@ -1,9 +1,102 @@ -export interface GetAuthMethodsReq { +// Request schemas +export interface OtpPleSendCodeReq { + unameMailPhone: string; + type: number; +} + +export interface OtpPleVerifyCodeReq { + unameMailPhone: string; + type: number; + code: number; + os: string | null; + language: string | null; +} + +export interface LoginPasswordAuthReq { + unameMailPhone: string; + password: string; + os: string | null; + language: string | null; +} + +export interface PleSendVCodeReq { + phoneMail: string; + type: number; +} + +export interface PleVerifyCodeReq { + phoneMail: string; + code: number; + type: number; +} + +export interface FinishPleAccountReq { + phoneMail: string; + authCode: string; + username: string; + displayName: string; + os: string | null; + language: string | null; +} + +export interface LoginWithGoogleReq { + code: string; + os: string | null; + language: string | null; +} + +export interface LoginWithApple { + code: string; + isApple: boolean; + os: string | null; + language: string | null; +} + +export interface RegisterReq { + username: string; + displayName: string; + password: string; + os: string | null; + language: string | null; +} + +export interface ResetPasswordReq { unameMailPhone: string; } +export interface VerifyPasswordResetReq { + unameMailPhone: string; + vCode: number; + newPassword: string; +} + +// Response schemas +export interface SignInSuccessResp { + token: string; + username: string; + displayName: string; + pfp: string; + userid: string; +} + +// Types export interface AuthMethods { password: boolean; email: boolean; sms: boolean; -} \ No newline at end of file +} + +export interface UserDataValidationResp { + used: boolean; +} + +export interface PleVerifyCodeResp { + authCode: string; +} + +export interface ResetPasswordResp { + userid: string; +} + +export const VerificationTypeSMS = 0 +export const VerificationTypeEmail = 1 diff --git a/src/domain/common.schema.ts b/src/domain/common.schema.ts deleted file mode 100644 index eda21e1..0000000 --- a/src/domain/common.schema.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface GenericErrorBody { - error: string; -} \ No newline at end of file diff --git a/src/domain/http.schema.ts b/src/domain/http.schema.ts new file mode 100644 index 0000000..c0c87eb --- /dev/null +++ b/src/domain/http.schema.ts @@ -0,0 +1,7 @@ +export interface GenericErrorBody { + error: string; +} + +export interface GenericSuccessBody { + response: string; +} \ No newline at end of file diff --git a/src/services/authService.ts b/src/services/authService.ts index a24d6a5..83036a0 100644 --- a/src/services/authService.ts +++ b/src/services/authService.ts @@ -1,10 +1,16 @@ import {getClient} from "../core/http"; -import {AuthMethods} from "../domain/authService.schema"; +import { + AuthMethods, FinishPleAccountReq, + LoginPasswordAuthReq, LoginWithApple, LoginWithGoogleReq, + OtpPleSendCodeReq, + OtpPleVerifyCodeReq, PleVerifyCodeResp, RegisterReq, ResetPasswordReq, ResetPasswordResp, + SignInSuccessResp, UserDataValidationResp, VerifyPasswordResetReq +} from "../domain/authService.schema"; import {isAxiosError} from "axios"; -import {GenericErrorBody} from "../domain/common.schema"; +import {GenericErrorBody, GenericSuccessBody} from "../domain/http.schema"; export class AuthService { - async getAuthMethods(unameMailPhone: String): Promise { + async getAuthMethods(unameMailPhone: string): Promise { try { const resp = await getClient().get(`user/authOptions?unameMailPhone=${unameMailPhone}`); return resp.data @@ -15,4 +21,197 @@ export class AuthService { throw new Error("Unexpected error") } } + + async otpSendCode(unameMailPhone: string, type: number): Promise { + try { + await getClient().post("v2/user/otpSendCode", { + unameMailPhone: unameMailPhone, + type: type + }); + return + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async otpVerifyCode(unameMailPhone: string, type: number, code: number): Promise { + try { + await getClient().post("v2/user/otpVerifyCode", { + unameMailPhone: unameMailPhone, + type: type, + code: code + }); + return + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async loginPasswordAuth(usernamePhoneMail: string, password: string): Promise { + try { + const resp = await getClient().post("v2/user/loginPasswordAuth", { + unameMailPhone: usernamePhoneMail, + password: password + }); + return resp.data + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async isUsernameUsed(username: string): Promise { + try { + const resp = await getClient().get(`v2/user/unameUsage?username=${username}`); + return resp.data.used + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async isEmailUsed(email: string): Promise { + try { + const resp = await getClient().get(`v2/user/emailUsage?email=${email}`); + return resp.data.used + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async pleSendVCode(phoneMail: string, type: number): Promise { + try { + await getClient().post("v2/user/pleSendVCode", { + unameMailPhone: phoneMail, + type: type, + }); + return + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async pleVerifyCode(phoneMail: string, type: number, code: number): Promise { + try { + const resp = await getClient().post("v2/user/pleVerifyCode", { + unameMailPhone: phoneMail, + type: type, + code: code + }); + return resp.data.authCode + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async finishPLEAccount(phoneMail: string, type: number, authCode: string, username: string, displayName: string): Promise { + try { + const resp = await getClient().post("v2/user/finishPLEAccount", { + phoneMail: phoneMail, + authCode: authCode, + displayName: displayName, + username: username, + }); + return resp.data + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async loginWithGoogle(code: string): Promise { + try { + const resp = await getClient().post("user/loginWithGoogle", { + code: code + }); + return resp.data + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async loginWithApple(code: string): Promise { + try { + const resp = await getClient().post("user/loginWithGoogle", { + code: code, + isApple: false, + }); + return resp.data + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async register(username: string, displayName: string, password: string): Promise { + try { + const resp = await getClient().post("v2/user/register", { + username: username, + displayName: displayName, + password: password, + }); + return resp.data + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async resetPassword(unameMailPhone: string): Promise { + try { + const resp = await getClient().post("user/resetPassword", { + unameMailPhone: unameMailPhone, + }); + return resp.data.userid + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } + + async verifyPasswordReset(unameMailPhone: string, code: number, newPassword: string): Promise { + try { + await getClient().post("user/resetPassword", { + unameMailPhone: unameMailPhone, + vCode: code, + newPassword: newPassword, + }); + return + } catch (e) { + if (isAxiosError(e)) { + throw e; + } + throw new Error("Unexpected error") + } + } } \ No newline at end of file