Files
sign/packages/auth/client/index.ts

114 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-01-02 15:33:37 +11:00
import type { ClientResponse } from 'hono/client';
import { hc } from 'hono/client';
2025-02-06 01:57:23 +11:00
import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app';
2025-01-02 15:33:37 +11:00
import { AppError } from '@documenso/lib/errors/app-error';
import type { AuthAppType } from '../server';
import type {
TForgotPasswordSchema,
TResetPasswordSchema,
2025-01-31 14:09:02 +11:00
TSignInSchema,
TSignUpSchema,
2025-01-02 15:33:37 +11:00
TVerifyEmailSchema,
} from '../server/types/email-password';
import type { TPasskeyAuthorizeSchema } from '../server/types/passkey';
export class AuthClient {
public client: ReturnType<typeof hc<AuthAppType>>;
private signOutRedirectUrl: string = '/signin';
constructor(options: { baseUrl: string }) {
this.client = hc<AuthAppType>(options.baseUrl);
}
public async signOut() {
await this.client.signout.$post();
window.location.href = this.signOutRedirectUrl;
}
public async session() {
return this.client.session.$get();
}
private async handleResponse<T>(response: ClientResponse<T>) {
if (!response.ok) {
const error = await response.json();
throw AppError.parseError(error);
}
if (response.headers.get('content-type')?.includes('application/json')) {
return response.json();
}
return response.text();
}
public emailPassword = {
2025-01-31 14:09:02 +11:00
signIn: async (data: TSignInSchema & { redirectUrl?: string }) => {
const response = await this.client['email-password'].authorize
.$post({ json: data })
.then(this.handleResponse);
if (data.redirectUrl) {
window.location.href = data.redirectUrl;
}
return response;
2025-01-02 15:33:37 +11:00
},
forgotPassword: async (data: TForgotPasswordSchema) => {
const response = await this.client['email-password']['forgot-password'].$post({ json: data });
return this.handleResponse(response);
},
resetPassword: async (data: TResetPasswordSchema) => {
const response = await this.client['email-password']['reset-password'].$post({ json: data });
return this.handleResponse(response);
},
2025-01-31 14:09:02 +11:00
signUp: async (data: TSignUpSchema) => {
2025-01-02 15:33:37 +11:00
const response = await this.client['email-password']['signup'].$post({ json: data });
return this.handleResponse(response);
},
verifyEmail: async (data: TVerifyEmailSchema) => {
const response = await this.client['email-password']['verify-email'].$post({ json: data });
return this.handleResponse(response);
},
};
2025-01-31 14:09:02 +11:00
public passkey = {
signIn: async (data: TPasskeyAuthorizeSchema & { redirectUrl?: string }) => {
const response = await this.client['passkey'].authorize
.$post({ json: data })
.then(this.handleResponse);
if (data.redirectUrl) {
window.location.href = data.redirectUrl;
}
return response;
},
};
2025-01-02 15:33:37 +11:00
public google = {
signIn: async () => {
2025-01-31 14:09:02 +11:00
const response = await this.client['google'].authorize.$post().then(this.handleResponse);
2025-01-02 15:33:37 +11:00
2025-01-31 14:09:02 +11:00
if (response.redirectUrl) {
window.location.href = response.redirectUrl;
2025-01-02 15:33:37 +11:00
}
2025-01-31 14:09:02 +11:00
return response;
2025-01-02 15:33:37 +11:00
},
};
}
export const authClient = new AuthClient({
2025-02-06 01:57:23 +11:00
baseUrl: `${NEXT_PUBLIC_WEBAPP_URL()}/api/auth`,
2025-01-02 15:33:37 +11:00
});