fix server session problem;

optimize
This commit is contained in:
Dobromir Popov
2024-05-11 17:57:33 +03:00
parent ffe68b492b
commit e29807540f
3 changed files with 38 additions and 19 deletions

View File

@ -70,21 +70,6 @@ export const authOptions: NextAuthOptions = {
password: { label: "Парола", type: "password" } password: { label: "Парола", type: "password" }
}, },
async authorize(credentials, req) { async authorize(credentials, req) {
//const user = { id: "1", name: "Администратора", email: "jsmith@example.com" }
//return user
// const res = await fetch("/your/endpoint", {
// method: 'POST',
// body: JSON.stringify(credentials),
// headers: { "Content-Type": "application/json" }
// })
// const user = await res.json()
// // If no error and we have user data, return it
// if (res.ok && user) {
// return user
// }
// // Return null if user data could not be retrieved
// return null
const users = [ const users = [
{ id: "1", name: "admin", email: "admin@example.com", password: "admin123", role: "ADMIN", static: true }, { id: "1", name: "admin", email: "admin@example.com", password: "admin123", role: "ADMIN", static: true },
{ id: "2", name: "krasi", email: "krasi@example.com", password: "krasi123", role: "ADMIN", static: true }, { id: "2", name: "krasi", email: "krasi@example.com", password: "krasi123", role: "ADMIN", static: true },
@ -272,6 +257,8 @@ export const authOptions: NextAuthOptions = {
verifyRequest: "/auth/verify-request", // (used for check email message) verifyRequest: "/auth/verify-request", // (used for check email message)
newUser: null // If set, new users will be directed here on first sign in newUser: null // If set, new users will be directed here on first sign in
}, },
debug: process.env.NODE_ENV === 'development',
} }
export default NextAuth(authOptions) export default NextAuth(authOptions)

View File

@ -8,6 +8,7 @@ import Layout from "../../../components/layout";
import PublisherCard from "../../../components/publisher/PublisherCard"; import PublisherCard from "../../../components/publisher/PublisherCard";
import axiosInstance from "../../../src/axiosSecure"; import axiosInstance from "../../../src/axiosSecure";
import axiosServer from '../../../src/axiosServer'; import axiosServer from '../../../src/axiosServer';
const common = require("../../../src/helpers/common");
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import { levenshteinEditDistance } from "levenshtein-edit-distance"; import { levenshteinEditDistance } from "levenshtein-edit-distance";
@ -226,9 +227,38 @@ export default PublishersPage;
//import { set } from "date-fns"; //import { set } from "date-fns";
export const getServerSideProps = async (context) => { export const getServerSideProps = async (context) => {
const axios = await axiosServer(context); // const axios = await axiosServer(context);
//ToDo: refactor all axios calls to use axiosInstance and this URL // //ToDo: refactor all axios calls to use axiosInstance and this URL
const { data: publishers } = await axios.get('/api/data/publishers?select=id,firstName,lastName,email,isActive,isTrained,isImported,assignments.shift.startTime,availabilities.startTime&dev=fromuseefect'); // const { data: publishers } = await axios.get('/api/data/publishers?select=id,firstName,lastName,email,isActive,isTrained,isImported,assignments.shift.startTime,availabilities.startTime&dev=fromuseefect');
//use prisma instead of axios
const prisma = common.getPrismaClient();
let publishers = await prisma.publisher.findMany({
select: {
id: true,
firstName: true,
lastName: true,
email: true,
isActive: true,
isTrained: true,
isImported: true,
assignments: {
select: {
shift: {
select: {
startTime: true,
},
},
},
},
availabilities: {
select: {
startTime: true,
},
},
}
});
publishers = JSON.parse(JSON.stringify(publishers));
return { return {
props: { props: {

View File

@ -14,6 +14,8 @@ import { useSession, getSession } from 'next-auth/react';
import axiosInstance from 'src/axiosSecure'; import axiosInstance from 'src/axiosSecure';
import { toast } from 'react-toastify'; import { toast } from 'react-toastify';
import LocalShippingIcon from '@mui/icons-material/LocalShipping'; import LocalShippingIcon from '@mui/icons-material/LocalShipping';
import { getServerSession } from 'next-auth';
import { authOptions } from 'pages/api/auth/[...nextauth]';
export default function MySchedulePage({ assignments }) { export default function MySchedulePage({ assignments }) {
@ -160,7 +162,7 @@ export default function MySchedulePage({ assignments }) {
//get future assignments for the current user (session.user.id) //get future assignments for the current user (session.user.id)
export const getServerSideProps = async (context) => { export const getServerSideProps = async (context) => {
const session = await getSession(context); const session = await getServerSession(context.req, context.res, authOptions)
context.res.setHeader("Cache-Control", "s-maxage=1, stale-while-revalidate"); context.res.setHeader("Cache-Control", "s-maxage=1, stale-while-revalidate");