mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-09 14:00:15 -04:00
Fix enum bugs and sign out issues
This commit is contained in:
parent
b0ced6ef9f
commit
a979d6b051
|
@ -1,37 +1,57 @@
|
||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { revalidatePath } from "next/cache";
|
import { revalidatePath } from "next/cache";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
import { createClient } from "@/utils/supabase/server";
|
import { createClient } from "@/utils/supabase/server";
|
||||||
|
import User, { Role } from "@/utils/models/User";
|
||||||
const supabase = createClient();
|
|
||||||
|
|
||||||
export async function login(email: string, password: string) {
|
export async function login(email: string, password: string) {
|
||||||
|
const supabase = createClient();
|
||||||
// type-casting here for convenience
|
// type-casting here for convenience
|
||||||
// in practice, you should validate your inputs
|
// in practice, you should validate your inputs
|
||||||
const data = {
|
const data = {
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
};
|
};
|
||||||
|
|
||||||
const { error } = await supabase.auth.signInWithPassword(data);
|
const { error } = await supabase.auth.signInWithPassword(data);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return "Incorrect email/password";
|
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");
|
revalidatePath("/resource", "layout");
|
||||||
redirect("/resource");
|
redirect("/resource");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function signOut() {
|
export async function signOut() {
|
||||||
|
const supabase = createClient();
|
||||||
|
|
||||||
const { data, error } = await supabase.auth.getUser();
|
const { data, error } = await supabase.auth.getUser();
|
||||||
if (error || !data?.user) {
|
if (error || !data?.user) {
|
||||||
redirect("auth/login");
|
redirect("auth/login");
|
||||||
}
|
}
|
||||||
|
|
||||||
supabase.auth.signOut();
|
console.log(`Signed out ${data.user.email}!`);
|
||||||
|
|
||||||
|
await supabase.auth.signOut();
|
||||||
|
|
||||||
revalidatePath("/resource", "layout");
|
revalidatePath("/resource", "layout");
|
||||||
redirect("/auth/login");
|
redirect("/auth/login");
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import Sidebar from "@/components/resource/Sidebar";
|
import Sidebar from "@/components/resource/Sidebar";
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { ChevronDoubleRightIcon } from "@heroicons/react/24/outline";
|
import { ChevronDoubleRightIcon } from "@heroicons/react/24/outline";
|
||||||
|
@ -7,7 +6,6 @@ import { createClient } from "@/utils/supabase/client";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { User } from "@supabase/supabase-js";
|
import { User } from "@supabase/supabase-js";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
|
|
||||||
export default function RootLayout({
|
export default function RootLayout({
|
||||||
children,
|
children,
|
||||||
}: {
|
}: {
|
||||||
|
@ -16,30 +14,26 @@ export default function RootLayout({
|
||||||
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
|
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
|
||||||
const [user, setUser] = useState<User>();
|
const [user, setUser] = useState<User>();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const supabase = createClient();
|
const supabase = createClient();
|
||||||
|
|
||||||
async function getUser() {
|
async function getUser() {
|
||||||
const { data, error } = await supabase.auth.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");
|
router.push("auth/login");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setUser(data.user);
|
setUser(data.user);
|
||||||
|
|
||||||
const userData = await fetch(
|
const userData = await fetch(
|
||||||
`${process.env.NEXT_PUBLIC_HOST}/api/user?uuid=${data.user.id}`
|
`${process.env.NEXT_PUBLIC_HOST}/api/user?uuid=${data.user.id}`
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(await userData.json());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getUser();
|
getUser();
|
||||||
}, [router]);
|
}, [router]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex-row">
|
<div className="flex-row">
|
||||||
{/* button to open sidebar */}
|
{/* button to open sidebar */}
|
||||||
|
|
|
@ -5,6 +5,10 @@ interface UserProfileProps {
|
||||||
email: string;
|
email: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleClick = async (event: React.MouseEvent<HTMLButtonElement>) => {
|
||||||
|
await signOut();
|
||||||
|
};
|
||||||
|
|
||||||
export const UserProfile = ({ name, email }: UserProfileProps) => {
|
export const UserProfile = ({ name, email }: UserProfileProps) => {
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col items-start space-y-2">
|
<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>
|
<span className="text-xs text-gray-500">{email}</span>
|
||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
onClick={signOut}
|
onClick={handleClick}
|
||||||
className="text-red-600 font-semibold text-xs hover:underline mt-1"
|
className="text-red-600 font-semibold text-xs hover:underline mt-1"
|
||||||
>
|
>
|
||||||
Sign out
|
Sign out
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
export enum Program {
|
export enum Program {
|
||||||
"ECONOMIC",
|
ECONOMIC = "ECONOMIC",
|
||||||
"DOMESTIC",
|
DOMESTIC = "DOMESTIC",
|
||||||
"COMMUNITY",
|
COMMUNITY = "COMMUNITY",
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum Role {
|
export enum Role {
|
||||||
"ADMIN",
|
ADMIN = "ADMIN",
|
||||||
"EMPLOYEE",
|
EMPLOYEE = "EMPLOYEE",
|
||||||
"VOLUNTEER",
|
VOLUNTEER = "VOLUNTEER",
|
||||||
}
|
}
|
||||||
|
|
||||||
export default interface User {
|
export default interface User {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user