last update
This commit is contained in:
@@ -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 (
|
||||
<Layout>
|
||||
<BlogPage blog={resBlog} blogs={resBlogs.blogs} />
|
||||
<BlogPage blog={blog} blogs={blogs.blogs} />
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
+1
-1
@@ -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(
|
||||
|
||||
@@ -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(
|
||||
|
||||
+1
-1
@@ -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() {
|
||||
|
||||
+16
-21
@@ -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 (
|
||||
<Content
|
||||
dataUser={response}
|
||||
logged={token.value}
|
||||
dataUser={dashboard}
|
||||
params={searchParams}
|
||||
matchedCity={matchedCity}
|
||||
initialTurnsDetails={isMobile}
|
||||
|
||||
@@ -11,8 +11,6 @@ async function Doctors({ searchParams }) {
|
||||
const response = await axios.get(
|
||||
"https://back-dev.clinic-pro.ir/api/v1/doctors"
|
||||
);
|
||||
console.log(response);
|
||||
|
||||
doctors = response.data.$data;
|
||||
} catch (error) {
|
||||
// console.error("problem with req:", error.message);
|
||||
|
||||
+11
-1
@@ -1,6 +1,16 @@
|
||||
import ContentLogin from "@/components/register/ContentLogin";
|
||||
import { defineAbilitiesFor } from "@/lib/ability";
|
||||
import { getUser } from "@/lib/auth";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
async function LogIn() {
|
||||
const user = await getUser();
|
||||
const ability = defineAbilitiesFor(user);
|
||||
|
||||
if (!ability.can("access", "Login")) {
|
||||
return redirect("/");
|
||||
}
|
||||
|
||||
function LogIn() {
|
||||
return <ContentLogin />;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 (
|
||||
<DashboardPage
|
||||
|
||||
@@ -11,6 +11,24 @@ function SendReq({
|
||||
setIsError,
|
||||
uuid,
|
||||
}) {
|
||||
const handleReq = async () => {
|
||||
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 (
|
||||
|
||||
+2
-15
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
+11
@@ -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;
|
||||
}
|
||||
};
|
||||
@@ -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),
|
||||
};
|
||||
|
||||
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user