diff --git a/actions/user-actions.ts b/actions/user-actions.ts index 40514c3..a986604 100644 --- a/actions/user-actions.ts +++ b/actions/user-actions.ts @@ -1,8 +1,9 @@ "use server"; -import { VerifyUserDetails } from "@/lib/person"; -import { revalidatePath } from "next/cache"; -import { redirect } from "next/navigation"; -import { CreateClient } from "./ninja/client"; + +import { getServerSession } from "next-auth"; +import { authOptions } from "@/app/auth"; +import type { User, UserProfile } from "@/lib/types/user"; +import { handleApiResponse } from "@/utils/tryCatch"; export async function VerifyUser(userId: string) { // const user = await prisma.user.findUnique({ @@ -52,72 +53,37 @@ export async function VerifyUser(userId: string) { // revalidatePath("/users"); } -export async function Rejectuser({ - userId, - reason, -}: { userId: string; reason: string }) { - // const user = await prisma.user.findUnique({ - // where: { - // id: userId, - // }, - // }); - // if (!user) { - // throw new Error("User not found"); - // } - - // await SendUserRejectionDetailSMS({ - // details: reason, - // phoneNumber: user.phoneNumber, - // }); - // await prisma.user.delete({ - // where: { - // id: userId, - // }, - // }); - revalidatePath("/users"); - redirect("/users"); -} - -export const SendUserRejectionDetailSMS = async ({ - details, - phoneNumber, -}: { - details: string; - phoneNumber: string; -}) => { - try { - const respose = await fetch(`${process.env.SMS_API_BASE_URL}/api/sms`, { - method: "POST", +export async function getProfile() { + const session = await getServerSession(authOptions); + const response = await fetch( + `${process.env.SARLINK_API_BASE_URL}/api/auth/profile/`, + { + method: "GET", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${process.env.SMS_API_KEY}`, + Authorization: `Token ${session?.apiToken}`, }, - body: JSON.stringify({ - check_delivery: false, - number: phoneNumber, - message: details, - }), - }); - const data = await respose.json(); - console.log(data); - return data; - } catch (error) { - console.error(error); - } -}; + }, + ); -export async function AddDevice({ - name, - mac_address, - user_id, -}: { name: string; mac_address: string; user_id: string }) { - // const newDevice = await prisma.device.create({ - // data: { - // name: name, - // mac: mac_address, - // userId: user_id, - // }, - // }); - revalidatePath("/devices"); - // return newDevice; + return handleApiResponse(response, "getProfile"); } + + + +export async function getProfileById(userId: string) { + const session = await getServerSession(authOptions); + const response = await fetch( + `${process.env.SARLINK_API_BASE_URL}/api/auth/users/${userId}/`, + { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Token ${session?.apiToken}`, + }, + }, + ); + + return handleApiResponse(response, "getProfilebyId"); +} + diff --git a/app/(dashboard)/profile/page.tsx b/app/(dashboard)/profile/page.tsx new file mode 100644 index 0000000..0b28631 --- /dev/null +++ b/app/(dashboard)/profile/page.tsx @@ -0,0 +1,95 @@ +import { redirect } from "next/navigation"; +import { getServerSession } from "next-auth"; +import { getProfileById } from "@/actions/user-actions"; +import { authOptions } from "@/app/auth"; +import ClientErrorMessage from "@/components/client-error-message"; +import { Badge } from "@/components/ui/badge"; +import { FloatingLabelInput } from "@/components/ui/floating-label"; +import { tryCatch } from "@/utils/tryCatch"; + +export default async function Profile() { + const session = await getServerSession(authOptions); + if (!session?.user) return redirect("/auth/signin?callbackUrl=/profile"); + const [error, profile] = await tryCatch(getProfileById(session?.user.id)); + if (error) { + if (error.message === "Invalid token.") redirect("/auth/signin"); + return ; + } + return ( +
+
+

Profile

+
+ Profile Status + {verifiedStatus(profile?.verified ?? false)} +
+
+
+
+ + + + + + + +
+
+ {/* + + */} +
+ ); +} + +function verifiedStatus(status: boolean) { + switch (status) { + case true: + return Verified; + case false: + return Not Verified; + default: + return Unknown; + } +} diff --git a/components/auth/account-popver.tsx b/components/auth/account-popver.tsx index 20aba64..aa447c5 100644 --- a/components/auth/account-popver.tsx +++ b/components/auth/account-popver.tsx @@ -1,13 +1,14 @@ "use client"; +import { Loader2, User as UserIcon } from "lucide-react"; +import Link from "next/link"; +import { signOut, useSession } from "next-auth/react"; +import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; -import { Loader2, User as UserIcon } from "lucide-react"; -import { signOut, useSession } from "next-auth/react"; -import { useState } from "react"; export function AccountPopover() { const session = useSession(); @@ -36,16 +37,24 @@ export function AccountPopover() {

{session.data?.user?.mobile}

- +
+ + + + +
+ diff --git a/components/auth/application-layout.tsx b/components/auth/application-layout.tsx index 08a9bf4..aff92a4 100644 --- a/components/auth/application-layout.tsx +++ b/components/auth/application-layout.tsx @@ -1,7 +1,7 @@ import { redirect } from "next/navigation"; import { getServerSession } from "next-auth"; import { NuqsAdapter } from 'nuqs/adapters/next/app' -import { getProfile } from "@/actions/payment"; +import { getProfile } from "@/actions/user-actions"; import { authOptions } from "@/app/auth"; import { DeviceCartDrawer } from "@/components/device-cart"; import { ModeToggle } from "@/components/theme-toggle";