diff --git a/app/blog/[slug]/page.js b/app/blog/[slug]/page.js index ba9082e..644fa06 100644 --- a/app/blog/[slug]/page.js +++ b/app/blog/[slug]/page.js @@ -1,6 +1,6 @@ import BlogPage from "@/components/blog"; import Layout from "@/components/layout/StLayout"; -import { fetchReq } from "@/lib/ability"; +import { fetchReq } from "@/lib/req"; async function Blog({ params: { slug } }) { const blog = await fetchReq( @@ -8,12 +8,9 @@ async function Blog({ params: { slug } }) { ); const blogs = await fetchReq("https://back-dev.clinic-pro.ir/api/v1/blogs"); - const resBlog = blog; - const resBlogs = blogs; - return ( - + ); } diff --git a/app/blogs/page.js b/app/blogs/page.js index de6c1b1..65f95ba 100644 --- a/app/blogs/page.js +++ b/app/blogs/page.js @@ -1,6 +1,6 @@ import BlogsPage from "@/components/blogs"; import Layout from "@/components/layout/StLayout"; -import { fetchReq } from "@/lib/ability"; +import { fetchReq } from "@/lib/req"; export default async function Blogs() { const response = await fetchReq( diff --git a/app/clinic/[slug]/page.js b/app/clinic/[slug]/page.js index 94dcff9..9ae78eb 100644 --- a/app/clinic/[slug]/page.js +++ b/app/clinic/[slug]/page.js @@ -1,6 +1,6 @@ import ClinicPage from "@/components/clinic"; import Layout from "@/components/layout/StLayout"; -import { fetchReq } from "@/lib/ability"; +import { fetchReq } from "@/lib/req"; async function Clinic({ params: { slug } }) { const reqClinics = await fetchReq( diff --git a/app/clinics/page.js b/app/clinics/page.js index 3871964..b19ea49 100644 --- a/app/clinics/page.js +++ b/app/clinics/page.js @@ -1,7 +1,7 @@ // app/clinics/page.js import ClinicsPage from "@/components/clinics"; import Layout from "@/components/layout/StLayout"; -import { fetchReq } from "@/lib/ability"; +import { fetchReq } from "@/lib/req"; import { getStateInfo } from "@/lib/getStateInfo"; export default async function Clinics() { diff --git a/app/dashboard/page.js b/app/dashboard/page.js index 4575ee7..b612113 100644 --- a/app/dashboard/page.js +++ b/app/dashboard/page.js @@ -1,8 +1,8 @@ import Content from "@/components/dashboard/Content"; -import { defineAbilitiesFor, fetchReq } from "@/lib/ability"; +import { defineAbilitiesFor } from "@/lib/ability"; import { getUser } from "@/lib/auth"; import { getStateInfo } from "@/lib/getStateInfo"; -import axios from "axios"; +import { fetchReq } from "@/lib/req"; import { cookies, headers } from "next/headers"; import { redirect } from "next/navigation"; @@ -12,36 +12,31 @@ export default async function Dashboard({ searchParams }) { const ability = defineAbilitiesFor(user); const cookieStore = cookies(); const token = cookieStore.get("access_token"); - const uuid = cookieStore.get("uuid"); - - let dashboard = null; - try { - const response = await fetchReq( - `https://back-dev.clinic-pro.ir/api/v1/user-profile/${uuid}`, - { - headers: { - Authorization: `Bearer ${token}`, - }, - } - ); - - dashboard = response.data; - } catch (error) { - // console.error("problem with req:", error.message); - // return redirect("/unauthorized"); - } + const userInfo = cookieStore.get("userInfo"); + console.log(userInfo.value.uuid); if (!ability.can("access", "Dashboard")) { return redirect("/"); } + const response = await fetchReq( + `https://back-dev.clinic-pro.ir/api/v1/user-profile/${userInfo.value.uuid}`, + { + headers: { + Authorization: `Bearer ${token.value}`, + }, + } + ) + .then((res) => console.log(res)) + .catch((err) => console.log(err)); + const userAgent = headers().get("user-agent") || ""; const isMobile = /Mobi|Android/i.test(userAgent); return ( ; } diff --git a/components/dashboard/Content.js b/components/dashboard/Content.js index f00ad29..ceefe4d 100644 --- a/components/dashboard/Content.js +++ b/components/dashboard/Content.js @@ -10,6 +10,7 @@ function Content({ matchedCity, params, logged, initialIsOpenSide, dataUser }) { const [value, setValue] = useState(Number(params?.sidebar) || 0); const [isOpenSide, setIsOpenSide] = useState(initialIsOpenSide); const [isTurnsDetails, setIsTurnsDetails] = useState(false); + console.log(dataUser); return ( { + setLoading(true); + request + .getToken( + "mobile", + process.env.NEXT_PUBLIC_CLIENT_ID, + process.env.NEXT_PUBLIC_CLIENT_SECRET, + uuid, + code.join("") + ) + .then((response) => { + if (response.access_token) { + handleSetCookie(response); + } + }) + .catch(() => {}); + }; + const handleSetCookie = (response) => { const accessTokenExpires = new Date(); accessTokenExpires.setSeconds( @@ -39,28 +57,28 @@ function SendReq({ secure: true, sameSite: "strict", }); + getInfo(response.access_token); }; - const handleReq = async () => { - setLoading(true); + const getInfo = (token) => { request - .getToken( - "mobile", - process.env.NEXT_PUBLIC_CLIENT_ID, - process.env.NEXT_PUBLIC_CLIENT_SECRET, - uuid, - code.join("") - ) - .then((response) => { + .getUserInfo({ + headers: { + Authorization: `Bearer ${token}`, + }, + }) + .then((res) => { setLoading(false); - if (response.access_token) { + if (res && res.uuid) { window.location.pathname = "/"; - handleSetCookie(response); + Cookies.set("userInfo", JSON.stringify(res), { + path: "/", + secure: true, + sameSite: "strict", + }); } }) - .catch(() => { - setLoading(false); - }); + .catch(() => {}); }; return ( diff --git a/lib/ability.js b/lib/ability.js index 064d0f9..9e0610a 100644 --- a/lib/ability.js +++ b/lib/ability.js @@ -1,28 +1,15 @@ import { AbilityBuilder, createMongoAbility } from "@casl/ability"; -import axios from "axios"; -import Cookies from "js-cookie"; - -const token = Cookies.get("access_token"); export function defineAbilitiesFor(user) { const { can, cannot, build } = new AbilityBuilder(createMongoAbility); if (user) { can("access", "Dashboard"); + cannot("access", "Login"); } else { - // can("access", "Dashboard"); + can("access", "Login"); cannot("access", "Dashboard"); } return build(); } - -export const fetchReq = async (url, headers) => { - try { - const response = await axios.get(url, headers); - return response.data; - } catch (error) { - console.error("fetchReq error:", error.message); - return null; - } -}; diff --git a/lib/req.js b/lib/req.js new file mode 100644 index 0000000..77ebdcd --- /dev/null +++ b/lib/req.js @@ -0,0 +1,11 @@ +import axios from "axios"; + +export const fetchReq = async (url, headers) => { + try { + const response = await axios.get(url, headers); + return response.data; + } catch (error) { + console.error("fetchReq error:", error.message); + return null; + } +}; diff --git a/services/response.js b/services/response.js index 97ec067..90dc014 100644 --- a/services/response.js +++ b/services/response.js @@ -33,4 +33,5 @@ export const request = { postUserProfile: (data) => api.post("api/v1/user-profile", data), patchUserProfile: (data, uuid) => api.patch(`api/v1/user-profile/${uuid}`, data), + getUserInfo: (headers) => api.get("oauth/userinfo", headers), }; diff --git a/utils/index.js b/utils/index.js index 0e0c2c3..d56fad2 100644 --- a/utils/index.js +++ b/utils/index.js @@ -4,6 +4,7 @@ const removeToken = () => { Cookies.remove("access_token", { path: "/" }); Cookies.remove("refresh_token", { path: "/" }); Cookies.remove("uuid", { path: "/" }); + Cookies.remove("userInfo", { path: "/" }); }; export { removeToken };