mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 09:50:43 +00:00
ff5eb78689
Resolves #4028 (client half — backend is openfrontio/infra#368, which must be deployed first). ## Description: Adds "Login with Google" to the client, alongside the existing Discord login. Companion to the backend PR (openfrontio/infra#368). - `Auth.ts` — `googleLogin()` (full-page redirect to `/auth/login/google?redirect_uri=…`, mirrors `discordLogin()`). - `ApiSchemas.ts` — `GoogleUserSchema` + optional `user.google` on `UserMeResponseSchema`. - `AccountModal.ts` — a "Login with Google" button (Google brand guidelines: white surface, dark text, the multicolor "G" mark) in the login options, and the logged-in view now renders a Google-authenticated user's email (also added `google` to `isLinkedAccount()`). - `en.json` — `main.login_google`. - `resources/images/GoogleLogo.svg` — the Google "G" mark. > **Draft.** Depends on infra#368 being deployed (the button hits the live `/auth/login/google`). ## Please complete the following: - [x] I have added screenshots for all UI updates <!-- TODO: add screenshot of the Google button --> - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory <!-- no client tests exist for AccountModal/Auth; verified via tsc --noEmit + eslint. Backend behaviour is covered in infra#368 --> ## Please put your Discord username so you can be contacted if a bug or regression is found: jish
392 lines
9.9 KiB
TypeScript
392 lines
9.9 KiB
TypeScript
import newsItemsFallback from "resources/news.json";
|
|
import { z } from "zod";
|
|
import type { NewsItem } from "../core/ApiSchemas";
|
|
import {
|
|
NewsItemSchema,
|
|
PlayerProfile,
|
|
PlayerProfileSchema,
|
|
RankedLeaderboardResponse,
|
|
RankedLeaderboardResponseSchema,
|
|
UserMeResponse,
|
|
UserMeResponseSchema,
|
|
} from "../core/ApiSchemas";
|
|
import { AnalyticsRecord, AnalyticsRecordSchema } from "../core/Schemas";
|
|
import { getAuthHeader, logOut, userAuth } from "./Auth";
|
|
|
|
export async function fetchPlayerById(
|
|
playerId: string,
|
|
): Promise<PlayerProfile | false> {
|
|
try {
|
|
const userAuthResult = await userAuth();
|
|
if (!userAuthResult) return false;
|
|
const { jwt } = userAuthResult;
|
|
|
|
const url = `${getApiBase()}/player/${encodeURIComponent(playerId)}`;
|
|
|
|
const res = await fetch(url, {
|
|
headers: {
|
|
Accept: "application/json",
|
|
Authorization: `Bearer ${jwt}`,
|
|
},
|
|
});
|
|
|
|
if (res.status !== 200) {
|
|
console.warn(
|
|
"fetchPlayerById: unexpected status",
|
|
res.status,
|
|
res.statusText,
|
|
);
|
|
return false;
|
|
}
|
|
|
|
const json = await res.json();
|
|
const parsed = PlayerProfileSchema.safeParse(json);
|
|
if (!parsed.success) {
|
|
console.warn("fetchPlayerById: Zod validation failed", parsed.error);
|
|
return false;
|
|
}
|
|
|
|
return parsed.data;
|
|
} catch (err) {
|
|
console.warn("fetchPlayerById: request failed", err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
let __userMe: Promise<UserMeResponse | false> | null = null;
|
|
export async function getUserMe(): Promise<UserMeResponse | false> {
|
|
if (__userMe !== null) {
|
|
return __userMe;
|
|
}
|
|
__userMe = (async () => {
|
|
try {
|
|
const userAuthResult = await userAuth();
|
|
if (!userAuthResult) return false;
|
|
const { jwt } = userAuthResult;
|
|
|
|
// Get the user object
|
|
const response = await fetch(getApiBase() + "/users/@me", {
|
|
headers: {
|
|
authorization: `Bearer ${jwt}`,
|
|
},
|
|
});
|
|
if (response.status === 401) {
|
|
await logOut();
|
|
return false;
|
|
}
|
|
if (response.status !== 200) return false;
|
|
const body = await response.json();
|
|
const result = UserMeResponseSchema.safeParse(body);
|
|
if (!result.success) {
|
|
const error = z.prettifyError(result.error);
|
|
console.error("Invalid response", error);
|
|
return false;
|
|
}
|
|
return result.data;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
})();
|
|
return __userMe;
|
|
}
|
|
|
|
export function invalidateUserMe() {
|
|
__userMe = null;
|
|
}
|
|
|
|
export async function purchaseWithCurrency(
|
|
cosmeticType: "pattern" | "skin" | "flag",
|
|
cosmeticName: string,
|
|
currencyType: "hard" | "soft",
|
|
colorPaletteName?: string,
|
|
): Promise<boolean> {
|
|
try {
|
|
const response = await fetch(`${getApiBase()}/shop/purchase`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: await getAuthHeader(),
|
|
},
|
|
body: JSON.stringify({
|
|
cosmeticType,
|
|
cosmeticName,
|
|
currencyType,
|
|
colorPaletteName,
|
|
}),
|
|
});
|
|
if (response.status === 401) {
|
|
await logOut();
|
|
return false;
|
|
}
|
|
if (!response.ok) {
|
|
console.error(
|
|
"purchaseWithCurrency: request failed",
|
|
response.status,
|
|
response.statusText,
|
|
);
|
|
return false;
|
|
}
|
|
return true;
|
|
} catch (e) {
|
|
console.error("purchaseWithCurrency: request failed", e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function createCheckoutSession(
|
|
priceId: string,
|
|
colorPaletteName?: string,
|
|
): Promise<string | false> {
|
|
try {
|
|
const response = await fetch(
|
|
`${getApiBase()}/stripe/create-checkout-session`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: await getAuthHeader(),
|
|
},
|
|
body: JSON.stringify({
|
|
priceId: priceId,
|
|
hostname: window.location.origin,
|
|
colorPaletteName: colorPaletteName,
|
|
}),
|
|
},
|
|
);
|
|
if (!response.ok) {
|
|
console.error(
|
|
"createCheckoutSession: request failed",
|
|
response.status,
|
|
response.statusText,
|
|
);
|
|
return false;
|
|
}
|
|
const json = await response.json();
|
|
return json.url;
|
|
} catch (e) {
|
|
console.error("createCheckoutSession: request failed", e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function cancelSubscription(): Promise<boolean> {
|
|
try {
|
|
const response = await fetch(`${getApiBase()}/subscriptions/@me/cancel`, {
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: await getAuthHeader(),
|
|
},
|
|
});
|
|
if (response.status === 401) {
|
|
await logOut();
|
|
return false;
|
|
}
|
|
if (!response.ok) {
|
|
console.error(
|
|
"cancelSubscription: request failed",
|
|
response.status,
|
|
response.statusText,
|
|
);
|
|
return false;
|
|
}
|
|
return true;
|
|
} catch (e) {
|
|
console.error("cancelSubscription: request failed", e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function changeSubscriptionTier(
|
|
tierName: string,
|
|
): Promise<boolean> {
|
|
try {
|
|
const response = await fetch(
|
|
`${getApiBase()}/subscriptions/@me/change-tier`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: await getAuthHeader(),
|
|
},
|
|
body: JSON.stringify({ tierName }),
|
|
},
|
|
);
|
|
if (response.status === 401) {
|
|
await logOut();
|
|
return false;
|
|
}
|
|
if (!response.ok) {
|
|
console.error(
|
|
"changeSubscriptionTier: request failed",
|
|
response.status,
|
|
response.statusText,
|
|
);
|
|
return false;
|
|
}
|
|
return true;
|
|
} catch (e) {
|
|
console.error("changeSubscriptionTier: request failed", e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function openSubscriptionPortal(): Promise<string | false> {
|
|
try {
|
|
const response = await fetch(`${getApiBase()}/subscriptions/@me/portal`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: await getAuthHeader(),
|
|
},
|
|
body: JSON.stringify({
|
|
returnUrl: window.location.origin,
|
|
}),
|
|
});
|
|
if (response.status === 401) {
|
|
await logOut();
|
|
return false;
|
|
}
|
|
if (!response.ok) {
|
|
console.error(
|
|
"openSubscriptionPortal: request failed",
|
|
response.status,
|
|
response.statusText,
|
|
);
|
|
return false;
|
|
}
|
|
const json = await response.json();
|
|
return json.url;
|
|
} catch (e) {
|
|
console.error("openSubscriptionPortal: request failed", e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function getApiBase() {
|
|
const domainname = getAudience();
|
|
|
|
if (domainname === "localhost") {
|
|
const apiDomain = process?.env?.API_DOMAIN;
|
|
if (apiDomain) {
|
|
return `https://${apiDomain}`;
|
|
}
|
|
return localStorage.getItem("apiHost") ?? "http://localhost:8787";
|
|
}
|
|
|
|
return `https://api.${domainname}`;
|
|
}
|
|
|
|
export function getAudience() {
|
|
const { hostname } = new URL(window.location.href);
|
|
const domainname = hostname.split(".").slice(-2).join(".");
|
|
return domainname;
|
|
}
|
|
|
|
// Check if the user's account is linked to a Discord, Google, or email account.
|
|
export function hasLinkedAccount(
|
|
userMeResponse: UserMeResponse | false,
|
|
): boolean {
|
|
return (
|
|
userMeResponse !== false &&
|
|
(userMeResponse.user?.discord !== undefined ||
|
|
userMeResponse.user?.google !== undefined ||
|
|
userMeResponse.user?.email !== undefined)
|
|
);
|
|
}
|
|
|
|
export async function fetchGameById(
|
|
gameId: string,
|
|
): Promise<AnalyticsRecord | false> {
|
|
try {
|
|
const url = `${getApiBase()}/game/${encodeURIComponent(gameId)}`;
|
|
const res = await fetch(url, {
|
|
headers: {
|
|
Accept: "application/json",
|
|
},
|
|
});
|
|
|
|
if (res.status !== 200) {
|
|
console.warn(
|
|
"fetchGameById: unexpected status",
|
|
res.status,
|
|
res.statusText,
|
|
);
|
|
return false;
|
|
}
|
|
|
|
const json = await res.json();
|
|
const parsed = AnalyticsRecordSchema.safeParse(json);
|
|
if (!parsed.success) {
|
|
console.warn("fetchGameById: Zod validation failed", parsed.error);
|
|
return false;
|
|
}
|
|
|
|
return parsed.data;
|
|
} catch (err) {
|
|
console.warn("fetchGameById: request failed", err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function fetchPlayerLeaderboard(
|
|
page: number,
|
|
): Promise<RankedLeaderboardResponse | "reached_limit" | false> {
|
|
try {
|
|
const url = new URL(`${getApiBase()}/leaderboard/ranked`);
|
|
url.searchParams.set("page", String(page));
|
|
const res = await fetch(url.toString(), {
|
|
headers: { Accept: "application/json" },
|
|
});
|
|
|
|
if (!res.ok) {
|
|
console.warn(
|
|
"fetchPlayerLeaderboard: unexpected status",
|
|
res.status,
|
|
res.statusText,
|
|
);
|
|
return false;
|
|
}
|
|
|
|
const json = await res.json();
|
|
const parsed = RankedLeaderboardResponseSchema.safeParse(json);
|
|
if (!parsed.success) {
|
|
// Handle "Page must be between X and Y" error as end of list
|
|
if (json?.message?.includes?.("Page must be between")) {
|
|
return "reached_limit";
|
|
}
|
|
console.warn(
|
|
"fetchPlayerLeaderboard: Zod validation failed",
|
|
parsed.error.toString(),
|
|
);
|
|
return false;
|
|
}
|
|
|
|
return parsed.data;
|
|
} catch (err) {
|
|
console.error("fetchPlayerLeaderboard: request failed", err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function getNews(): Promise<NewsItem[]> {
|
|
try {
|
|
const res = await fetch(`${getApiBase()}/news.json`, {
|
|
headers: { Accept: "application/json" },
|
|
});
|
|
if (res.status !== 200) {
|
|
console.warn("getNews: unexpected status", res.status);
|
|
return newsItemsFallback as NewsItem[];
|
|
}
|
|
const json = await res.json();
|
|
const parsed = z.array(NewsItemSchema).safeParse(json);
|
|
if (!parsed.success) {
|
|
console.warn("getNews: Zod validation failed", parsed.error);
|
|
return newsItemsFallback as NewsItem[];
|
|
}
|
|
return parsed.data;
|
|
} catch (err) {
|
|
console.warn("getNews: request failed, using fallback", err);
|
|
return newsItemsFallback as NewsItem[];
|
|
}
|
|
}
|