summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorl3wdfut4pwr <l3wdfut4pwr@gmail.com>2026-04-29 02:07:46 +0300
committerl3wdfut4pwr <l3wdfut4pwr@gmail.com>2026-04-29 02:07:46 +0300
commite619245f1fa83a29a9ec553ef9017871bb5c27c0 (patch)
treed945801c8dd8e2b3d3fd36f962c31f29ead4b690 /src/lib
parent42a5d2de33564c060d2d6f3cefdd3cf21c26a996 (diff)
add google auth
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/api/RefreshUser.ts37
-rw-r--r--src/lib/api/SetPassword.ts52
2 files changed, 89 insertions, 0 deletions
diff --git a/src/lib/api/RefreshUser.ts b/src/lib/api/RefreshUser.ts
new file mode 100644
index 0000000..d93d310
--- /dev/null
+++ b/src/lib/api/RefreshUser.ts
@@ -0,0 +1,37 @@
+const API_URL = process.env.NEXT_PUBLIC_API_URL;
+
+export type RefreshUserResponse = {
+ authenticated: boolean;
+ user: any | null;
+};
+
+export const refreshUser = async (): Promise<RefreshUserResponse> => {
+ try {
+ const res = await fetch(`${API_URL}/api/me`, {
+ method: 'GET',
+ credentials: 'include',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ });
+
+ const data = await res.json().catch(() => null);
+
+ if (!res.ok || !data) {
+ return {
+ authenticated: false,
+ user: null,
+ };
+ }
+
+ return {
+ authenticated: data.authenticated,
+ user: data.authenticated ? data.user : null,
+ };
+ } catch (err) {
+ return {
+ authenticated: false,
+ user: null,
+ };
+ }
+};
diff --git a/src/lib/api/SetPassword.ts b/src/lib/api/SetPassword.ts
new file mode 100644
index 0000000..ee445fc
--- /dev/null
+++ b/src/lib/api/SetPassword.ts
@@ -0,0 +1,52 @@
+const API_URL = process.env.NEXT_PUBLIC_API_URL;
+
+export type SetPasswordResponse =
+ | { data: { success: true }; error: null }
+ | { data: null; error: { general: string } };
+
+export const setPassword = async (
+ newPassword: string,
+ repeatPassword: string,
+): Promise<SetPasswordResponse> => {
+ try {
+ const res = await fetch(`${API_URL}/api/users/password/set`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ credentials: 'include',
+ body: JSON.stringify({
+ new_password: newPassword,
+ repeat_password: repeatPassword,
+ }),
+ });
+
+ const data = await res.json().catch(() => null);
+
+ if (!res.ok) {
+ const detail = data?.detail;
+
+ return {
+ data: null,
+ error: {
+ general:
+ typeof detail === 'string'
+ ? detail
+ : detail?.msg || 'Ошибка установки пароля',
+ },
+ };
+ }
+
+ return {
+ data: { success: true },
+ error: null,
+ };
+ } catch (err: any) {
+ return {
+ data: null,
+ error: {
+ general: err?.message || 'Network error',
+ },
+ };
+ }
+};