feat: Integrate AWS Amplify authentication, update API queries, and enhance sidebar with user details
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
|
||||
import { fetchAuthSession, getCurrentUser } from "aws-amplify/auth";
|
||||
|
||||
export interface Project {
|
||||
id: number;
|
||||
@@ -76,10 +78,36 @@ export interface Team {
|
||||
export const api = createApi({
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: process.env.NEXT_PUBLIC_API_BASE_URL,
|
||||
prepareHeaders: async (headers) => {
|
||||
const session = await fetchAuthSession();
|
||||
const { accessToken } = session.tokens ?? {};
|
||||
if (accessToken) {
|
||||
headers.set("Authorization", `Bearer ${accessToken}`);
|
||||
}
|
||||
return headers;
|
||||
},
|
||||
}),
|
||||
reducerPath: "api",
|
||||
tagTypes: ["Projects", "Tasks", "Users", "Teams"],
|
||||
endpoints: (build) => ({
|
||||
getAuthUser: build.query({
|
||||
queryFn: async (_, _queryApi, _extraoptions, fetchWithBQ) => {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
const session = await fetchAuthSession();
|
||||
if (!session) throw new Error("No session found");
|
||||
|
||||
const { userSub } = session;
|
||||
|
||||
const userDetailsResponse = await fetchWithBQ(`users/${userSub}`);
|
||||
const userDetails = userDetailsResponse.data as User;
|
||||
|
||||
return { data: { user, userSub, userDetails } };
|
||||
} catch (error: any) {
|
||||
return { error: error.message || "Could not fetch user data" };
|
||||
}
|
||||
},
|
||||
}),
|
||||
getProjects: build.query<Project[], void>({
|
||||
query: () => "projects",
|
||||
providesTags: ["Projects"],
|
||||
@@ -139,6 +167,7 @@ export const api = createApi({
|
||||
});
|
||||
|
||||
export const {
|
||||
useGetAuthUserQuery,
|
||||
useGetProjectsQuery,
|
||||
useCreateProjectMutation,
|
||||
useGetTasksQuery,
|
||||
|
||||
Reference in New Issue
Block a user