From c2e5425fad6419f935dbadd881e05937679bd6fc Mon Sep 17 00:00:00 2001 From: xtian7489 Date: Fri, 2 Feb 2024 22:27:03 -0300 Subject: [PATCH 1/6] pass session management to authContextProvider --- next.config.js | 3 + package-lock.json | 14 ++ package.json | 1 + src/app/admin/page.jsx | 13 +- src/app/admin/proyectos/page.jsx | 4 +- src/app/admin/usuarios/[id]/page.jsx | 4 +- src/app/admin/usuarios/page.jsx | 4 +- src/app/auth/login/page.jsx | 5 - src/app/auth/register/page.jsx | 5 - src/app/layout.jsx | 8 +- src/app/pacto/[id]/editar/exito/page.jsx | 4 +- src/app/pacto/[id]/editar/page.jsx | 4 +- src/app/pacto/[id]/estadisticas/page.jsx | 4 +- .../pacto/[id]/nueva-version/exito/page.jsx | 4 +- src/app/pacto/[id]/nueva-version/page.jsx | 4 +- src/app/pacto/nuevo/exito/page.jsx | 4 +- src/app/pacto/nuevo/page.jsx | 4 +- src/components/auth/login.jsx | 34 ++- src/components/auth/register.jsx | 197 +++++++++--------- src/components/common/language-selector.jsx | 3 +- src/components/common/replies-modal.jsx | 13 +- src/components/home/banner-pacto.jsx | 2 +- src/components/landing/countries-banner.jsx | 1 - src/components/layout/footer.jsx | 8 +- src/components/layout/navbar.jsx | 28 ++- .../pacto/body/pacto/article-modal.jsx | 10 +- src/components/pacto/body/pacto/comment.jsx | 8 +- src/components/pacto/body/pacto/comments.jsx | 9 +- src/components/pacto/form/authorField.jsx | 5 +- src/components/pacto/form/projectForm.jsx | 4 +- src/components/pacto/header/breadcrumb.jsx | 4 +- src/components/pacto/header/edit-project.jsx | 4 +- src/context/auth-context.jsx | 129 +++++++----- src/middleware.js | 32 +++ src/utils/axios.js | 5 +- src/utils/axiosServer.js | 5 +- src/utils/get-data.js | 21 ++ src/utils/post-data.js | 113 +++++++--- 38 files changed, 420 insertions(+), 304 deletions(-) create mode 100644 src/middleware.js diff --git a/next.config.js b/next.config.js index 28d65b3..ebe8b5a 100644 --- a/next.config.js +++ b/next.config.js @@ -1,5 +1,8 @@ /** @type {import('next').NextConfig} */ const nextConfig = { + experimental: { + serverActions: true, + }, } module.exports = nextConfig diff --git a/package-lock.json b/package-lock.json index 818877d..58da286 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,6 +23,7 @@ "bulma": "^0.9.4", "cookies-js": "^1.2.3", "cookies-next": "^4.1.0", + "js-cookie": "^3.0.5", "next": "13.5.5", "next-transpile-modules": "^10.0.1", "react": "^18", @@ -5803,6 +5804,14 @@ "set-function-name": "^2.0.1" } }, + "node_modules/js-cookie": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz", + "integrity": "sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==", + "engines": { + "node": ">=14" + } + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -13525,6 +13534,11 @@ "set-function-name": "^2.0.1" } }, + "js-cookie": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz", + "integrity": "sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==" + }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", diff --git a/package.json b/package.json index 8fd5b29..ad61136 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "bulma": "^0.9.4", "cookies-js": "^1.2.3", "cookies-next": "^4.1.0", + "js-cookie": "^3.0.5", "next": "13.5.5", "next-transpile-modules": "^10.0.1", "react": "^18", diff --git a/src/app/admin/page.jsx b/src/app/admin/page.jsx index 3c667f8..42e86b7 100644 --- a/src/app/admin/page.jsx +++ b/src/app/admin/page.jsx @@ -4,18 +4,13 @@ import { useRouter, redirect } from "next/navigation" import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faFile } from "@fortawesome/free-regular-svg-icons"; import { faPencil } from "@fortawesome/free-solid-svg-icons"; -import { useSelector } from "react-redux"; -import axiosServices from "@/utils/axios"; +import { useAuthContext } from "@/context/auth-context"; import ProjectFormComponent from "@/components/pacto/form/projectForm"; import { useEffect } from "react"; -export default function AdminHomePage({params}) { +export default function AdminHomePage({ params }) { // get the user from store - const { user } = useSelector(state => state.auth) - // redirect if user is not logged in - if (!user) { - redirect('/auth/login') - } + const { user } = useAuthContext() // redirect if user is not logged in // redirect if user is not admin or author if (user.role !== 'admin') { redirect('/') @@ -26,7 +21,7 @@ export default function AdminHomePage({params}) {

Panel de administración

Seleccione una opción del menú lateral

-
+ ) } \ No newline at end of file diff --git a/src/app/admin/proyectos/page.jsx b/src/app/admin/proyectos/page.jsx index ec1a1b6..411a6a3 100644 --- a/src/app/admin/proyectos/page.jsx +++ b/src/app/admin/proyectos/page.jsx @@ -4,14 +4,14 @@ import { useRouter, redirect } from "next/navigation" import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faFile } from "@fortawesome/free-regular-svg-icons"; import { faPencil } from "@fortawesome/free-solid-svg-icons"; -import { useSelector } from "react-redux"; import Link from 'next/link' import Emoji from "@/components/common/emoji"; import axiosServices from "@/utils/axios"; +import { useAuthContext } from "@/context/auth-context"; export default function AdminProjectPage({params}) { // get the user from store - const { user } = useSelector(state => state.auth) + const { user } = useAuthContext() // redirect if user is not logged in if (!user) { diff --git a/src/app/admin/usuarios/[id]/page.jsx b/src/app/admin/usuarios/[id]/page.jsx index fafbd72..069e453 100644 --- a/src/app/admin/usuarios/[id]/page.jsx +++ b/src/app/admin/usuarios/[id]/page.jsx @@ -2,17 +2,17 @@ import { useState, useEffect } from "react" import { useRouter, redirect } from "next/navigation" import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; -import { useSelector } from "react-redux"; import Link from 'next/link' import Image from 'next/image' import axiosServices from "@/utils/axios"; import { faCheck, faDownload, faPenClip, faShield, faSync, faTimes, faUserEdit, faUserShield } from "@fortawesome/free-solid-svg-icons"; import Emoji from "@/components/common/emoji"; import { faCheckCircle, faTimesCircle } from "@fortawesome/free-regular-svg-icons"; +import { useAuthContext } from "@/context/auth-context"; export default function AdminUserInfoPage({params}) { // get the user from store - const { user } = useSelector(state => state.auth) + const { user } = useAuthContext() // redirect if user is not logged in if (!user) { diff --git a/src/app/admin/usuarios/page.jsx b/src/app/admin/usuarios/page.jsx index b2e27f0..97f6fe9 100644 --- a/src/app/admin/usuarios/page.jsx +++ b/src/app/admin/usuarios/page.jsx @@ -2,17 +2,17 @@ import { useState, useEffect } from "react" import { useRouter, redirect } from "next/navigation" import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; -import { useSelector } from "react-redux"; import Link from 'next/link' import Image from 'next/image' import axiosServices from "@/utils/axios"; import { faCheck, faDownload, faPenClip, faShield, faSync, faTimes, faUserEdit, faUserShield } from "@fortawesome/free-solid-svg-icons"; import Emoji from "@/components/common/emoji"; import { faCheckCircle, faTimesCircle } from "@fortawesome/free-regular-svg-icons"; +import { useAuthContext } from "@/context/auth-context"; export default function AdminUsersListPage({params}) { // get the user from store - const { user } = useSelector(state => state.auth) + const { user } = useAuthContext() // redirect if user is not logged in if (!user) { diff --git a/src/app/auth/login/page.jsx b/src/app/auth/login/page.jsx index 2cd1b13..9819d6b 100644 --- a/src/app/auth/login/page.jsx +++ b/src/app/auth/login/page.jsx @@ -5,11 +5,6 @@ import { cookies } from 'next/headers'; export default async function Login(props) { - const token = cookies().get('auth') - const user = token ? await fetchUserMe() : null - if (user) { - redirect('/') - } return (
diff --git a/src/app/auth/register/page.jsx b/src/app/auth/register/page.jsx index d5edbf0..d19cec1 100644 --- a/src/app/auth/register/page.jsx +++ b/src/app/auth/register/page.jsx @@ -5,11 +5,6 @@ import { cookies } from 'next/headers'; export default async function Register() { - const token = cookies().get('auth') - const user = token ? await fetchUserMe() : null - if (user) { - redirect('/') - } return (
diff --git a/src/app/layout.jsx b/src/app/layout.jsx index e10c512..b7c33f6 100644 --- a/src/app/layout.jsx +++ b/src/app/layout.jsx @@ -8,9 +8,7 @@ import './themes.scss' import Navbar from "@/components/layout/navbar" import Footer from '@/components/layout/footer' -import Overlay from '@/components/common/overlay' -import { createTheme } from '@mui/material' -import { AuthProvider } from '@/context/auth-context' +import AuthContextProvider from '@/context/auth-context' import { LandingProvider } from '@/context/landing-context' import Message from '@/components/common/message' @@ -32,14 +30,14 @@ export default function RootLayout({ children }) { - +
{children}