feat(auth): add google sign-in onboarding flow
This commit is contained in:
@@ -52,7 +52,7 @@ export class ApiError extends Error {
|
||||
|
||||
const cleanBaseUrl = API_BASE_URL.replace(/\/+$/, "")
|
||||
|
||||
const buildUrl = (endpoint: string) => {
|
||||
export const buildApiUrl = (endpoint: string) => {
|
||||
const cleanEndpoint = endpoint.startsWith("/") ? endpoint : `/${endpoint}`
|
||||
return `${cleanBaseUrl}${cleanEndpoint}`
|
||||
}
|
||||
@@ -154,7 +154,7 @@ const refreshAccessToken = async () => {
|
||||
|
||||
if (!refreshRequest) {
|
||||
refreshRequest = (async () => {
|
||||
const response = await fetch(buildUrl("/api/users/token/refresh/"), {
|
||||
const response = await fetch(buildApiUrl("/api/users/token/refresh/"), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
@@ -226,7 +226,7 @@ export const authFetch = async (endpoint: string, options: RequestInit = {}, all
|
||||
...options.headers,
|
||||
}
|
||||
|
||||
const response = await fetch(buildUrl(endpoint), {
|
||||
const response = await fetch(buildApiUrl(endpoint), {
|
||||
...options,
|
||||
headers,
|
||||
})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { authFetch, buildApiError } from './client';
|
||||
import { authFetch, buildApiError, buildApiUrl } from './client';
|
||||
|
||||
// --- Auth Endpoints ---
|
||||
|
||||
@@ -28,8 +28,72 @@ export const loginWithOtp = async (mobile: string, otp: string) => {
|
||||
if (!response.ok) throw await buildApiError(response);
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const logoutUser = async (refreshToken: string) => {
|
||||
|
||||
export const startGoogleLogin = () => {
|
||||
window.location.assign(buildApiUrl("/api/users/oauth/google/start/"));
|
||||
};
|
||||
|
||||
export type GoogleOAuthFlowResponse =
|
||||
| {
|
||||
status: "authenticated";
|
||||
access: string;
|
||||
refresh: string;
|
||||
}
|
||||
| {
|
||||
status: "collect_mobile";
|
||||
email: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
avatar_url: string;
|
||||
}
|
||||
| {
|
||||
status: "claim_required";
|
||||
mobile: string;
|
||||
detail?: string;
|
||||
};
|
||||
|
||||
export const getGoogleOAuthFlow = async (flow: string): Promise<GoogleOAuthFlowResponse> => {
|
||||
const response = await authFetch(`/api/users/oauth/google/flow/?flow=${encodeURIComponent(flow)}`, {
|
||||
method: "GET",
|
||||
});
|
||||
if (!response.ok) throw await buildApiError(response);
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const completeGoogleOAuthSignup = async (
|
||||
flow: string,
|
||||
mobile: string,
|
||||
): Promise<GoogleOAuthFlowResponse> => {
|
||||
const response = await authFetch("/api/users/oauth/google/complete/", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ flow, mobile }),
|
||||
});
|
||||
if (!response.ok) throw await buildApiError(response);
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const sendGoogleOAuthClaimOtp = async (flow: string) => {
|
||||
const response = await authFetch("/api/users/oauth/google/claim/send-otp/", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ flow }),
|
||||
});
|
||||
if (!response.ok) throw await buildApiError(response);
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const verifyGoogleOAuthClaim = async (
|
||||
flow: string,
|
||||
code: string,
|
||||
): Promise<GoogleOAuthFlowResponse> => {
|
||||
const response = await authFetch("/api/users/oauth/google/claim/verify/", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ flow, code }),
|
||||
});
|
||||
if (!response.ok) throw await buildApiError(response);
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const logoutUser = async (refreshToken: string) => {
|
||||
const response = await authFetch('/api/users/logout/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ refresh: refreshToken })
|
||||
|
||||
Reference in New Issue
Block a user