chore(auth): restore auth file and move the server action to user file (#5951)

This commit is contained in:
Pablo Lara
2024-11-28 12:55:31 +01:00
committed by GitHub
parent fd8d34e8bc
commit d5187b3099
3 changed files with 29 additions and 32 deletions
-31
View File
@@ -1,12 +1,9 @@
"use server";
import { revalidatePath } from "next/cache";
import { AuthError } from "next-auth";
import { z } from "zod";
import { signIn, signOut } from "@/auth.config";
import { auth } from "@/auth.config";
import { parseStringify } from "@/lib";
import { authFormSchema } from "@/types";
const formSchemaSignIn = authFormSchema("sign-in");
@@ -142,34 +139,6 @@ export const getToken = async (formData: z.infer<typeof formSchemaSignIn>) => {
}
};
export const getProfileInfo = async () => {
const session = await auth();
const keyServer = process.env.API_BASE_URL;
const url = new URL(`${keyServer}/users/me`);
try {
const response = await fetch(url.toString(), {
method: "GET",
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
});
if (!response.ok) {
throw new Error(`Failed to fetch user data: ${response.statusText}`);
}
const data = await response.json();
const parsedData = parseStringify(data);
revalidatePath("/profile");
return parsedData;
} catch (error) {
console.error("Error fetching profile:", error);
return undefined;
}
};
export const getUserByMe = async (accessToken: string) => {
const keyServer = process.env.API_BASE_URL;
const url = new URL(`${keyServer}/users/me`);
+28
View File
@@ -114,3 +114,31 @@ export const deleteUser = async (formData: FormData) => {
};
}
};
export const getProfileInfo = async () => {
const session = await auth();
const keyServer = process.env.API_BASE_URL;
const url = new URL(`${keyServer}/users/me`);
try {
const response = await fetch(url.toString(), {
method: "GET",
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
});
if (!response.ok) {
throw new Error(`Failed to fetch user data: ${response.statusText}`);
}
const data = await response.json();
const parsedData = parseStringify(data);
revalidatePath("/profile");
return parsedData;
} catch (error) {
console.error("Error fetching profile:", error);
return undefined;
}
};
+1 -1
View File
@@ -1,7 +1,7 @@
import { Spacer } from "@nextui-org/react";
import React, { Suspense } from "react";
import { getProfileInfo } from "@/actions/auth";
import { getProfileInfo } from "@/actions/users/users";
import { Header } from "@/components/ui";
import { SkeletonUserInfo } from "@/components/users/profile";
import { UserInfo } from "@/components/users/profile/user-info";