mirror of
https://github.com/Equicord/Equicord.git
synced 2025-06-15 09:33:03 -04:00
change database to use proper methods, fix auth on every single change (#281)
This commit is contained in:
parent
e2a686630c
commit
c90df61b88
2 changed files with 98 additions and 41 deletions
|
@ -8,7 +8,6 @@ import { openModal } from "@utils/index";
|
||||||
import { OAuth2AuthorizeModal, showToast, Toasts } from "@webpack/common";
|
import { OAuth2AuthorizeModal, showToast, Toasts } from "@webpack/common";
|
||||||
|
|
||||||
const databaseTimezones: Record<string, { value: string | null; }> = {};
|
const databaseTimezones: Record<string, { value: string | null; }> = {};
|
||||||
|
|
||||||
const DOMAIN = "https://timezone.creations.works";
|
const DOMAIN = "https://timezone.creations.works";
|
||||||
const REDIRECT_URI = `${DOMAIN}/auth/discord/callback`;
|
const REDIRECT_URI = `${DOMAIN}/auth/discord/callback`;
|
||||||
const CLIENT_ID = "1377021506810417173";
|
const CLIENT_ID = "1377021506810417173";
|
||||||
|
@ -26,7 +25,6 @@ export async function loadDatabaseTimezones(): Promise<boolean> {
|
||||||
const res = await fetch(`${DOMAIN}/list`, {
|
const res = await fetch(`${DOMAIN}/list`, {
|
||||||
headers: { Accept: "application/json" }
|
headers: { Accept: "application/json" }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const json = await res.json();
|
const json = await res.json();
|
||||||
for (const id in json) {
|
for (const id in json) {
|
||||||
|
@ -34,10 +32,8 @@ export async function loadDatabaseTimezones(): Promise<boolean> {
|
||||||
value: json[id]?.timezone ?? null
|
value: json[id]?.timezone ?? null
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Failed to fetch timezones list:", e);
|
console.error("Failed to fetch timezones list:", e);
|
||||||
|
@ -45,30 +41,93 @@ export async function loadDatabaseTimezones(): Promise<boolean> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setTimezone(timezone: string): Promise<boolean> {
|
async function checkAuthentication(): Promise<boolean> {
|
||||||
const res = await fetch(`${DOMAIN}/set?timezone=${encodeURIComponent(timezone)}`, {
|
try {
|
||||||
method: "POST",
|
const res = await fetch(`${DOMAIN}/me`, {
|
||||||
headers: {
|
credentials: "include",
|
||||||
"Content-Type": "application/json",
|
headers: { Accept: "application/json" }
|
||||||
Accept: "application/json"
|
});
|
||||||
},
|
return res.ok;
|
||||||
credentials: "include"
|
} catch (e) {
|
||||||
});
|
console.error("Failed to check authentication:", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return res.ok;
|
export async function setTimezone(timezone: string): Promise<boolean> {
|
||||||
|
const isAuthenticated = await checkAuthentication();
|
||||||
|
|
||||||
|
if (!isAuthenticated) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
authModal(() => {
|
||||||
|
setTimezoneInternal(timezone).then(resolve);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return setTimezoneInternal(timezone);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setTimezoneInternal(timezone: string): Promise<boolean> {
|
||||||
|
const formData = new URLSearchParams();
|
||||||
|
formData.append("timezone", timezone);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${DOMAIN}/set`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
Accept: "application/json"
|
||||||
|
},
|
||||||
|
credentials: "include",
|
||||||
|
body: formData
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const error = await res.json().catch(() => ({ message: "Unknown error" }));
|
||||||
|
showToast(error.message || "Failed to set timezone", Toasts.Type.FAILURE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
showToast("Timezone updated successfully!", Toasts.Type.SUCCESS);
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error setting timezone:", e);
|
||||||
|
showToast("Failed to set timezone", Toasts.Type.FAILURE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteTimezone(): Promise<boolean> {
|
export async function deleteTimezone(): Promise<boolean> {
|
||||||
const res = await fetch(`${DOMAIN}/delete`, {
|
const isAuthenticated = await checkAuthentication();
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Accept: "application/json"
|
|
||||||
},
|
|
||||||
credentials: "include"
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.ok;
|
if (!isAuthenticated) {
|
||||||
|
showToast("You must be logged in to delete your timezone", Toasts.Type.FAILURE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${DOMAIN}/delete`, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
Accept: "application/json"
|
||||||
|
},
|
||||||
|
credentials: "include"
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const error = await res.json().catch(() => ({ message: "Unknown error" }));
|
||||||
|
showToast(error.message || "Failed to delete timezone", Toasts.Type.FAILURE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
showToast("Timezone deleted successfully!", Toasts.Type.SUCCESS);
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error deleting timezone:", e);
|
||||||
|
showToast("Failed to delete timezone", Toasts.Type.FAILURE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function authModal(callback?: () => void) {
|
export function authModal(callback?: () => void) {
|
||||||
|
@ -83,21 +142,17 @@ export function authModal(callback?: () => void) {
|
||||||
cancelCompletesFlow={false}
|
cancelCompletesFlow={false}
|
||||||
callback={async (res: any) => {
|
callback={async (res: any) => {
|
||||||
if (!res || !res.location) return;
|
if (!res || !res.location) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const url = new URL(res.location);
|
const url = new URL(res.location);
|
||||||
|
|
||||||
const r = await fetch(url, {
|
const r = await fetch(url, {
|
||||||
credentials: "include",
|
credentials: "include",
|
||||||
headers: { Accept: "application/json" }
|
headers: { Accept: "application/json" }
|
||||||
});
|
});
|
||||||
|
|
||||||
const json = await r.json();
|
const json = await r.json();
|
||||||
if (!r.ok) {
|
if (!r.ok) {
|
||||||
showToast(json.message ?? "Authorization failed", Toasts.Type.FAILURE);
|
showToast(json.message ?? "Authorization failed", Toasts.Type.FAILURE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
showToast("Authorization successful!", Toasts.Type.SUCCESS);
|
showToast("Authorization successful!", Toasts.Type.SUCCESS);
|
||||||
callback?.();
|
callback?.();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -17,7 +17,7 @@ import { findByPropsLazy } from "@webpack";
|
||||||
import { Button, Menu, showToast, Toasts, Tooltip, useEffect, UserStore, useState } from "@webpack/common";
|
import { Button, Menu, showToast, Toasts, Tooltip, useEffect, UserStore, useState } from "@webpack/common";
|
||||||
import { Message, User } from "discord-types/general";
|
import { Message, User } from "discord-types/general";
|
||||||
|
|
||||||
import { authModal, deleteTimezone, getTimezone, loadDatabaseTimezones, setUserDatabaseTimezone } from "./database";
|
import { deleteTimezone, getTimezone, loadDatabaseTimezones, setUserDatabaseTimezone } from "./database";
|
||||||
import { SetTimezoneModal } from "./TimezoneModal";
|
import { SetTimezoneModal } from "./TimezoneModal";
|
||||||
|
|
||||||
export let timezones: Record<string, string | null> = {};
|
export let timezones: Record<string, string | null> = {};
|
||||||
|
@ -68,9 +68,7 @@ export const settings = definePluginSettings({
|
||||||
type: OptionType.COMPONENT,
|
type: OptionType.COMPONENT,
|
||||||
component: () => (
|
component: () => (
|
||||||
<Button onClick={() => {
|
<Button onClick={() => {
|
||||||
authModal(async () => {
|
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
||||||
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
|
||||||
});
|
|
||||||
}}>
|
}}>
|
||||||
Set Timezone on Database
|
Set Timezone on Database
|
||||||
</Button>
|
</Button>
|
||||||
|
@ -83,11 +81,19 @@ export const settings = definePluginSettings({
|
||||||
component: () => (
|
component: () => (
|
||||||
<Button
|
<Button
|
||||||
color={Button.Colors.RED}
|
color={Button.Colors.RED}
|
||||||
onClick={() => {
|
onClick={async () => {
|
||||||
authModal(async () => {
|
try {
|
||||||
await setUserDatabaseTimezone(UserStore.getCurrentUser().id, null);
|
await setUserDatabaseTimezone(UserStore.getCurrentUser().id, null);
|
||||||
await deleteTimezone();
|
const success = await deleteTimezone();
|
||||||
});
|
if (success) {
|
||||||
|
showToast("Database timezone reset successfully!", Toasts.Type.SUCCESS);
|
||||||
|
} else {
|
||||||
|
showToast("Failed to reset database timezone", Toasts.Type.FAILURE);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error resetting database timezone:", error);
|
||||||
|
showToast("Failed to reset database timezone", Toasts.Type.FAILURE);
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Reset Database Timezones
|
Reset Database Timezones
|
||||||
|
@ -228,9 +234,7 @@ export default definePlugin({
|
||||||
|
|
||||||
toolboxActions: {
|
toolboxActions: {
|
||||||
"Set Database Timezone": () => {
|
"Set Database Timezone": () => {
|
||||||
authModal(async () => {
|
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
||||||
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
"Refresh Database Timezones": async () => {
|
"Refresh Database Timezones": async () => {
|
||||||
try {
|
try {
|
||||||
|
@ -265,9 +269,7 @@ export default definePlugin({
|
||||||
<Button
|
<Button
|
||||||
color={Button.Colors.GREEN}
|
color={Button.Colors.GREEN}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
authModal(async () => {
|
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
||||||
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
|
||||||
});
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Want to save your timezone to the database? Click here to set it.
|
Want to save your timezone to the database? Click here to set it.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue