feat: Integrate AWS Amplify authentication, update API queries, and enhance sidebar with user details
This commit is contained in:
64
tasker-client/src/app/authProvider.tsx
Normal file
64
tasker-client/src/app/authProvider.tsx
Normal file
@@ -0,0 +1,64 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import React from "react";
|
||||
import { Authenticator } from "@aws-amplify/ui-react";
|
||||
import { Amplify } from "aws-amplify";
|
||||
import "@aws-amplify/ui-react/styles.css";
|
||||
|
||||
Amplify.configure({
|
||||
Auth: {
|
||||
Cognito: {
|
||||
userPoolId: process.env.NEXT_PUBLIC_COGNITO_USER_POOL_ID || "",
|
||||
userPoolClientId:
|
||||
process.env.NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID || "",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const formFields = {
|
||||
signUp: {
|
||||
username: {
|
||||
order: 1,
|
||||
placeholder: "Choose a username",
|
||||
label: "Username",
|
||||
inputProps: { required: true },
|
||||
},
|
||||
email: {
|
||||
order: 2,
|
||||
placeholder: "Enter your email address",
|
||||
label: "Email",
|
||||
inputProps: { type: "email", required: true },
|
||||
},
|
||||
password: {
|
||||
order: 3,
|
||||
placeholder: "Enter your password",
|
||||
label: "Password",
|
||||
inputProps: { type: "password", required: true },
|
||||
},
|
||||
confirm_password: {
|
||||
order: 4,
|
||||
placeholder: "Confirm your password",
|
||||
label: "Confirm Password",
|
||||
inputProps: { type: "password", required: true },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const AuthProvider = ({ children }: any) => {
|
||||
return (
|
||||
<div>
|
||||
<Authenticator formFields={formFields}>
|
||||
{({ user }: any) =>
|
||||
user ? (
|
||||
<div>{children}</div>
|
||||
) : (
|
||||
<div>
|
||||
<h1>Please sign in below:</h1>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
</Authenticator>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default AuthProvider;
|
||||
@@ -1,8 +1,11 @@
|
||||
import React from "react";
|
||||
import { Menu, Moon, Search, Settings, Sun } from "lucide-react";
|
||||
import { Menu, Moon, Search, Settings, Sun, User } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import Image from "next/image";
|
||||
import { useAppDispatch, useAppSelector } from "@/app/redux";
|
||||
import { setIsDarkMode, setIsSidebarCollapsed } from "@/state";
|
||||
import { useGetAuthUserQuery } from "@/state/api";
|
||||
import { signOut } from "aws-amplify/auth";
|
||||
|
||||
const Navbar = () => {
|
||||
const dispatch = useAppDispatch();
|
||||
@@ -11,6 +14,18 @@ const Navbar = () => {
|
||||
);
|
||||
const isDarkMode = useAppSelector((state) => state.global.isDarkMode);
|
||||
|
||||
const { data: currentUser } = useGetAuthUserQuery({});
|
||||
const handleSignOut = async () => {
|
||||
try {
|
||||
await signOut();
|
||||
} catch (error) {
|
||||
console.error("Error signing out: ", error);
|
||||
}
|
||||
};
|
||||
|
||||
if (!currentUser) return null;
|
||||
const currentUserDetails = currentUser?.userDetails;
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-between bg-white px-4 py-3 dark:bg-black">
|
||||
<div className="flex items-center gap-8">
|
||||
@@ -57,7 +72,31 @@ const Navbar = () => {
|
||||
>
|
||||
<Settings className="h-6 w-6 cursor-pointer dark:text-white" />
|
||||
</Link>
|
||||
<div className="ml-2 mr-2 hidden min-h-[2em] w-[0.1rem] bg-gray-200 md:inline-block"></div>
|
||||
<div className="ml-2 mr-5 hidden min-h-[2em] w-[0.1rem] bg-gray-200 md:inline-block"></div>
|
||||
<div className="hidden items-center justify-between md:flex">
|
||||
<div className="align-center flex h-9 w-9 justify-center">
|
||||
{!!currentUserDetails?.profilePictureUrl ? (
|
||||
<Image
|
||||
src={`/${currentUserDetails?.profilePictureUrl}`}
|
||||
alt={currentUserDetails?.username || "User Profile Picture"}
|
||||
width={100}
|
||||
height={50}
|
||||
className="h-full rounded-full object-cover"
|
||||
/>
|
||||
) : (
|
||||
<User className="h-6 w-6 cursor-pointer self-center rounded-full dark:text-white" />
|
||||
)}
|
||||
</div>
|
||||
<span className="mx-3 text-gray-800 dark:text-white">
|
||||
{currentUserDetails?.username}
|
||||
</span>
|
||||
<button
|
||||
className="hidden rounded bg-blue-400 px-4 py-2 text-xs font-bold text-white hover:bg-blue-500 md:block"
|
||||
onClick={handleSignOut}
|
||||
>
|
||||
Sign out
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -25,7 +25,8 @@ import { usePathname } from "next/navigation";
|
||||
import { setIsSidebarCollapsed } from "@/state";
|
||||
import { useAppDispatch, useAppSelector } from "@/app/redux";
|
||||
import Link from "next/link";
|
||||
import { useGetProjectsQuery } from "@/state/api";
|
||||
import { useGetAuthUserQuery, useGetProjectsQuery } from "@/state/api";
|
||||
import { signOut } from "aws-amplify/auth";
|
||||
|
||||
const Sidebar = () => {
|
||||
const [showProjects, setShowProjects] = React.useState(true);
|
||||
@@ -37,6 +38,17 @@ const Sidebar = () => {
|
||||
(state) => state.global.isSidebarCollapsed,
|
||||
);
|
||||
|
||||
const { data: currentUser } = useGetAuthUserQuery({});
|
||||
const handleSignOut = async () => {
|
||||
try {
|
||||
await signOut();
|
||||
} catch (error) {
|
||||
console.error("Error signing out: ", error);
|
||||
}
|
||||
};
|
||||
if (!currentUser) return null;
|
||||
const currentUserDetails = currentUser?.userDetails;
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`fixed z-40 flex h-full w-64 flex-col justify-between overflow-y-auto bg-white shadow-xl transition-all duration-300 dark:bg-black ${isSidebarCollapsed ? "hidden w-0" : "w-64"}`}
|
||||
@@ -141,6 +153,32 @@ const Sidebar = () => {
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<div className="z-10 mt-32 flex w-full flex-col items-center gap-4 bg-white px-8 py-4 dark:bg-black md:hidden">
|
||||
<div className="flex w-full items-center">
|
||||
<div className="align-center flex h-9 w-9 justify-center">
|
||||
{!!currentUserDetails?.profilePictureUrl ? (
|
||||
<Image
|
||||
src={`/${currentUserDetails?.profilePictureUrl}`}
|
||||
alt={currentUserDetails?.username || "User Profile Picture"}
|
||||
width={100}
|
||||
height={50}
|
||||
className="h-full rounded-full object-cover"
|
||||
/>
|
||||
) : (
|
||||
<User className="h-6 w-6 cursor-pointer self-center rounded-full dark:text-white" />
|
||||
)}
|
||||
</div>
|
||||
<span className="mx-3 text-gray-800 dark:text-white">
|
||||
{currentUserDetails?.username}
|
||||
</span>
|
||||
<button
|
||||
className="self-start rounded bg-blue-400 px-4 py-2 text-xs font-bold text-white hover:bg-blue-500 md:block"
|
||||
onClick={handleSignOut}
|
||||
>
|
||||
Sign out
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@ import React, { useEffect } from "react";
|
||||
import Navbar from "@/app/components/Navbar";
|
||||
import Sidebar from "@/app/components/Sidebar";
|
||||
import StoreProvider, { useAppSelector } from "./redux";
|
||||
import AuthProvider from "./authProvider";
|
||||
|
||||
const DashboardLayout = ({ children }: { children: React.ReactNode }) => {
|
||||
const isSidebarCollapsed = useAppSelector(
|
||||
@@ -35,7 +36,9 @@ const DashboardLayout = ({ children }: { children: React.ReactNode }) => {
|
||||
const DashboardWrapper = ({ children }: { children: React.ReactNode }) => {
|
||||
return (
|
||||
<StoreProvider>
|
||||
<DashboardLayout>{children}</DashboardLayout>
|
||||
<AuthProvider>
|
||||
<DashboardLayout>{children}</DashboardLayout>
|
||||
</AuthProvider>
|
||||
</StoreProvider>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -5,7 +5,12 @@ import Header from "@/app/components/Header";
|
||||
import ModalNewTask from "@/app/components/ModalNewTask";
|
||||
import TaskCard from "@/app/components/TaskCard";
|
||||
import { dataGridSxStyles } from "@/lib/utils";
|
||||
import { Priority, Task, useGetTasksByUserQuery } from "@/state/api";
|
||||
import {
|
||||
Priority,
|
||||
Task,
|
||||
useGetAuthUserQuery,
|
||||
useGetTasksByUserQuery,
|
||||
} from "@/state/api";
|
||||
import { DataGrid, GridColDef } from "@mui/x-data-grid";
|
||||
import React, { useState } from "react";
|
||||
|
||||
@@ -72,9 +77,8 @@ const ReusablePriorityPage = ({ priority }: Props) => {
|
||||
const [view, setView] = useState("list");
|
||||
const [isModalNewTaskOpen, setIsModalNewTaskOpen] = useState(false);
|
||||
|
||||
// const { data: currentUser } = useGetAuthUserQuery({});
|
||||
// const userId = currentUser?.userDetails?.userId ?? null;
|
||||
const userId = 1;
|
||||
const { data: currentUser } = useGetAuthUserQuery({});
|
||||
const userId = currentUser?.userDetails?.userId ?? null;
|
||||
const {
|
||||
data: tasks,
|
||||
isLoading,
|
||||
@@ -83,8 +87,6 @@ const ReusablePriorityPage = ({ priority }: Props) => {
|
||||
skip: userId === null,
|
||||
});
|
||||
|
||||
console.log(tasks);
|
||||
|
||||
const isDarkMode = useAppSelector((state) => state.global.isDarkMode);
|
||||
|
||||
const filteredTasks = tasks?.filter(
|
||||
|
||||
Reference in New Issue
Block a user