Fix enum bugs and sign out issues

This commit is contained in:
pmoharana-cmd 2024-04-23 21:21:55 -04:00
parent b0ced6ef9f
commit a979d6b051
4 changed files with 41 additions and 23 deletions

View File

@ -1,37 +1,57 @@
"use server";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { createClient } from "@/utils/supabase/server";
const supabase = createClient();
import User, { Role } from "@/utils/models/User";
export async function login(email: string, password: string) {
const supabase = createClient();
// type-casting here for convenience
// in practice, you should validate your inputs
const data = {
email,
password,
};
const { error } = await supabase.auth.signInWithPassword(data);
if (error) {
return "Incorrect email/password";
}
const supabaseUser = await supabase.auth.getUser();
if (!supabaseUser.data.user) {
revalidatePath("/resource", "layout");
redirect("/resource");
}
const apiData = await fetch(
`${process.env.NEXT_PUBLIC_HOST}/api/user?uuid=${supabaseUser.data.user.id}`
);
const user: User = await apiData.json();
console.log(user);
if (user.role === Role.ADMIN) {
redirect("/admin");
}
revalidatePath("/resource", "layout");
redirect("/resource");
}
export async function signOut() {
const supabase = createClient();
const { data, error } = await supabase.auth.getUser();
if (error || !data?.user) {
redirect("auth/login");
}
supabase.auth.signOut();
console.log(`Signed out ${data.user.email}!`);
await supabase.auth.signOut();
revalidatePath("/resource", "layout");
redirect("/auth/login");

View File

@ -1,5 +1,4 @@
"use client";
import Sidebar from "@/components/resource/Sidebar";
import React, { useState } from "react";
import { ChevronDoubleRightIcon } from "@heroicons/react/24/outline";
@ -7,7 +6,6 @@ import { createClient } from "@/utils/supabase/client";
import { useEffect } from "react";
import { User } from "@supabase/supabase-js";
import { useRouter } from "next/navigation";
export default function RootLayout({
children,
}: {
@ -16,30 +14,26 @@ export default function RootLayout({
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
const [user, setUser] = useState<User>();
const router = useRouter();
useEffect(() => {
const supabase = createClient();
async function getUser() {
const { data, error } = await supabase.auth.getUser();
if (error || data.user === null) {
console.log(data, error);
if (error) {
console.log("Accessed resource page but not logged in");
router.push("auth/login");
return;
}
setUser(data.user);
const userData = await fetch(
`${process.env.NEXT_PUBLIC_HOST}/api/user?uuid=${data.user.id}`
);
console.log(await userData.json());
}
getUser();
}, [router]);
return (
<div className="flex-row">
{/* button to open sidebar */}

View File

@ -5,6 +5,10 @@ interface UserProfileProps {
email: string;
}
const handleClick = async (event: React.MouseEvent<HTMLButtonElement>) => {
await signOut();
};
export const UserProfile = ({ name, email }: UserProfileProps) => {
return (
<div className="flex flex-col items-start space-y-2">
@ -15,7 +19,7 @@ export const UserProfile = ({ name, email }: UserProfileProps) => {
<span className="text-xs text-gray-500">{email}</span>
</div>
<button
onClick={signOut}
onClick={handleClick}
className="text-red-600 font-semibold text-xs hover:underline mt-1"
>
Sign out

View File

@ -1,13 +1,13 @@
export enum Program {
"ECONOMIC",
"DOMESTIC",
"COMMUNITY",
ECONOMIC = "ECONOMIC",
DOMESTIC = "DOMESTIC",
COMMUNITY = "COMMUNITY",
}
export enum Role {
"ADMIN",
"EMPLOYEE",
"VOLUNTEER",
ADMIN = "ADMIN",
EMPLOYEE = "EMPLOYEE",
VOLUNTEER = "VOLUNTEER",
}
export default interface User {