Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions ui/actions/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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`);
Expand Down
28 changes: 28 additions & 0 deletions ui/actions/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
2 changes: 1 addition & 1 deletion ui/app/(prowler)/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Loading