diff --git a/app/App.jsx b/app/App.jsx index 9fbfaa0..6fabfb3 100644 --- a/app/App.jsx +++ b/app/App.jsx @@ -1,8 +1,16 @@ +"use client"; +import { useUserStore } from "./store/auth"; import { Navbar } from "./components/Navbar/Navbar"; import { Inter } from "next/font/google"; +import { useEffect } from "react"; const inter = Inter({ subsets: ["latin"] }); export const App = (props) => { + const userStore = useUserStore(); + useEffect(() => { + userStore.checkAuth(); + }, []) + return ( diff --git a/app/components/Navbar/Navbar.jsx b/app/components/Navbar/Navbar.jsx index a968f5c..b883423 100644 --- a/app/components/Navbar/Navbar.jsx +++ b/app/components/Navbar/Navbar.jsx @@ -1,11 +1,13 @@ -"use client" +"use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; +import { useUserStore } from "@/app/store/auth"; export const Navbar = () => { const pathname = usePathname(); + const userStore = useUserStore(); - const isNotAuthorizedStyle = "text-gray-700" + const isNotAuthorizedStyle = "text-gray-700"; const navLinks = [ { id: 1, @@ -14,7 +16,7 @@ export const Navbar = () => { title: "Домашняя", href: "/", withAuthOnly: false, - mobileMenu: false + mobileMenu: false, }, { id: 2, @@ -23,7 +25,7 @@ export const Navbar = () => { title: "Поиск", href: "/search", withAuthOnly: false, - mobileMenu: false + mobileMenu: false, }, { id: 3, @@ -32,7 +34,7 @@ export const Navbar = () => { title: "Закладки", href: "/bookmarks", withAuthOnly: true, - mobileMenu: true + mobileMenu: true, }, { id: 4, @@ -41,7 +43,7 @@ export const Navbar = () => { title: "Избранное", href: "/favorites", withAuthOnly: true, - mobileMenu: true + mobileMenu: true, }, { id: 5, @@ -50,7 +52,7 @@ export const Navbar = () => { title: "История", href: "/history", withAuthOnly: true, - mobileMenu: true + mobileMenu: true, }, ]; @@ -59,13 +61,56 @@ export const Navbar = () => {
- - - Войти - + {userStore.user ? ( +
+ +

{userStore.user.login}

+
+ ) : ( + + + + Войти + + + )}
); diff --git a/app/store/auth.js b/app/store/auth.js index 2e436d5..a46c655 100644 --- a/app/store/auth.js +++ b/app/store/auth.js @@ -8,7 +8,7 @@ export const useUserStore = create((set, get) => ({ token: null, login: (user, token) => { - set({ isAuth: true, user, token }); + set({ isAuth: true, user: user, token: token }); }, logout: () => { set({ isAuth: false, user: null, token: null }); @@ -21,7 +21,7 @@ export const useUserStore = create((set, get) => ({ `/api/profile/${jwt.user_id}?token=${jwt.jwt}` ); if (data && data.is_my_profile) { - get().login(data, jwt.user_id, jwt.jwt); + get().login(data.profile, jwt.jwt); } else { get().logout(); }