v28 fix
This commit is contained in:
Vendored
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
/// <reference types="next" />
|
/// <reference types="next" />
|
||||||
/// <reference types="next/image-types/global" />
|
/// <reference types="next/image-types/global" />
|
||||||
import "./.next/dev/types/routes.d.ts";
|
import "./.next/types/routes.d.ts";
|
||||||
|
|
||||||
// NOTE: This file should not be edited
|
// NOTE: This file should not be edited
|
||||||
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { getTranslations } from "next-intl/server";
|
import { getTranslations } from "next-intl/server";
|
||||||
import AdminContent from "@/components/admin/admin-content";
|
import AdminContent from "@/components/admin/admin-content";
|
||||||
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
import { authOptions } from "@/lib/auth/auth-options";
|
||||||
import { isAdminRole } from "@/lib/auth/roles";
|
import { isAdminRole } from "@/lib/auth/roles";
|
||||||
import { getServerSession } from "next-auth";
|
import { getServerSession } from "next-auth";
|
||||||
import { notFound } from "next/navigation";
|
import { notFound } from "next/navigation";
|
||||||
|
|||||||
@@ -1,124 +1,5 @@
|
|||||||
import { authService } from "@/lib/api/auth/service";
|
import { authOptions } from "@/lib/auth/auth-options";
|
||||||
import { normalizeRoles } from "@/lib/auth/roles";
|
|
||||||
import NextAuth from "next-auth";
|
import NextAuth from "next-auth";
|
||||||
import type { NextAuthOptions } from "next-auth";
|
|
||||||
import type { JWT } from "next-auth/jwt";
|
|
||||||
import type { Session, User } from "next-auth";
|
|
||||||
import Credentials from "next-auth/providers/credentials";
|
|
||||||
|
|
||||||
function randomToken() {
|
|
||||||
return Math.random().toString(36).substring(2) + Date.now().toString(36);
|
|
||||||
}
|
|
||||||
|
|
||||||
const isMockMode = process.env.NEXT_PUBLIC_ENABLE_MOCK_MODE === "true";
|
|
||||||
|
|
||||||
export const authOptions: NextAuthOptions = {
|
|
||||||
providers: [
|
|
||||||
Credentials({
|
|
||||||
name: "Credentials",
|
|
||||||
credentials: {
|
|
||||||
email: { label: "Email", type: "text" },
|
|
||||||
password: { label: "Password", type: "password" },
|
|
||||||
},
|
|
||||||
async authorize(credentials) {
|
|
||||||
try {
|
|
||||||
console.log("Starting authorization with:", {
|
|
||||||
email: credentials?.email,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!credentials?.email || !credentials?.password) {
|
|
||||||
throw new Error("Email ve şifre gereklidir.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Eğer mock mod aktifse backend'e gitme
|
|
||||||
if (isMockMode) {
|
|
||||||
console.log("Mock mode active, bypassing backend");
|
|
||||||
return {
|
|
||||||
id: credentials.email,
|
|
||||||
name: credentials.email.split("@")[0],
|
|
||||||
email: credentials.email,
|
|
||||||
accessToken: randomToken(),
|
|
||||||
refreshToken: randomToken(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normal mod: backend'e istek at
|
|
||||||
console.log("Sending login request to backend...");
|
|
||||||
const res = await authService.login({
|
|
||||||
email: credentials.email,
|
|
||||||
password: credentials.password,
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
"Backend response received:",
|
|
||||||
JSON.stringify(res, null, 2),
|
|
||||||
);
|
|
||||||
|
|
||||||
const response = res;
|
|
||||||
|
|
||||||
// Backend returns ApiResponse<TokenResponseDto>
|
|
||||||
// Structure: { data: { accessToken, refreshToken, expiresIn, user }, message, statusCode }
|
|
||||||
if (!res.success || !response?.data?.accessToken) {
|
|
||||||
console.error("Login failed or no access token in response");
|
|
||||||
throw new Error(response?.message || "Giriş başarısız");
|
|
||||||
}
|
|
||||||
|
|
||||||
const { accessToken, refreshToken, user } = response.data;
|
|
||||||
const normalizedRoles = normalizeRoles(user.roles);
|
|
||||||
|
|
||||||
console.log("Login successful, creating user session object");
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: user.id,
|
|
||||||
name: user.firstName
|
|
||||||
? `${user.firstName} ${user.lastName || ""}`.trim()
|
|
||||||
: user.email.split("@")[0],
|
|
||||||
email: user.email,
|
|
||||||
accessToken,
|
|
||||||
refreshToken,
|
|
||||||
roles: normalizedRoles,
|
|
||||||
};
|
|
||||||
} catch (error: unknown) {
|
|
||||||
console.error("Authorize error detailed:", error);
|
|
||||||
const err = error as Error & {
|
|
||||||
response?: { data: unknown; status: number };
|
|
||||||
};
|
|
||||||
if (err.response) {
|
|
||||||
console.error("Error response data:", err.response.data);
|
|
||||||
console.error("Error response status:", err.response.status);
|
|
||||||
}
|
|
||||||
throw new Error(
|
|
||||||
err.message || "An error occurred during authentication",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
callbacks: {
|
|
||||||
async jwt({ token, user }: { token: JWT; user?: User }) {
|
|
||||||
if (user) {
|
|
||||||
token.accessToken = user.accessToken;
|
|
||||||
token.refreshToken = user.refreshToken;
|
|
||||||
token.id = user.id;
|
|
||||||
token.roles = normalizeRoles(user.roles);
|
|
||||||
}
|
|
||||||
return token;
|
|
||||||
},
|
|
||||||
async session({ session, token }: { session: Session; token: JWT }) {
|
|
||||||
session.user.id = token.id;
|
|
||||||
session.user.roles = normalizeRoles(token.roles);
|
|
||||||
session.accessToken = token.accessToken;
|
|
||||||
session.refreshToken = token.refreshToken;
|
|
||||||
return session;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
pages: {
|
|
||||||
signIn: "/signin",
|
|
||||||
error: "/signin",
|
|
||||||
},
|
|
||||||
session: { strategy: "jwt" },
|
|
||||||
secret: process.env.NEXTAUTH_SECRET,
|
|
||||||
};
|
|
||||||
|
|
||||||
const handler = NextAuth(authOptions);
|
const handler = NextAuth(authOptions);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,120 @@
|
|||||||
|
import { authService } from "@/lib/api/auth/service";
|
||||||
|
import { normalizeRoles } from "@/lib/auth/roles";
|
||||||
|
import type { NextAuthOptions } from "next-auth";
|
||||||
|
import type { JWT } from "next-auth/jwt";
|
||||||
|
import type { Session, User } from "next-auth";
|
||||||
|
import Credentials from "next-auth/providers/credentials";
|
||||||
|
|
||||||
|
function randomToken() {
|
||||||
|
return Math.random().toString(36).substring(2) + Date.now().toString(36);
|
||||||
|
}
|
||||||
|
|
||||||
|
const isMockMode = process.env.NEXT_PUBLIC_ENABLE_MOCK_MODE === "true";
|
||||||
|
|
||||||
|
export const authOptions: NextAuthOptions = {
|
||||||
|
providers: [
|
||||||
|
Credentials({
|
||||||
|
name: "Credentials",
|
||||||
|
credentials: {
|
||||||
|
email: { label: "Email", type: "text" },
|
||||||
|
password: { label: "Password", type: "password" },
|
||||||
|
},
|
||||||
|
async authorize(credentials) {
|
||||||
|
try {
|
||||||
|
console.log("Starting authorization with:", {
|
||||||
|
email: credentials?.email,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!credentials?.email || !credentials?.password) {
|
||||||
|
throw new Error("Email ve şifre gereklidir.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Eğer mock mod aktifse backend'e gitme
|
||||||
|
if (isMockMode) {
|
||||||
|
console.log("Mock mode active, bypassing backend");
|
||||||
|
return {
|
||||||
|
id: credentials.email,
|
||||||
|
name: credentials.email.split("@")[0],
|
||||||
|
email: credentials.email,
|
||||||
|
accessToken: randomToken(),
|
||||||
|
refreshToken: randomToken(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normal mod: backend'e istek at
|
||||||
|
console.log("Sending login request to backend...");
|
||||||
|
const res = await authService.login({
|
||||||
|
email: credentials.email,
|
||||||
|
password: credentials.password,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
"Backend response received:",
|
||||||
|
JSON.stringify(res, null, 2),
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = res;
|
||||||
|
|
||||||
|
// Backend returns ApiResponse<TokenResponseDto>
|
||||||
|
// Structure: { data: { accessToken, refreshToken, expiresIn, user }, message, statusCode }
|
||||||
|
if (!res.success || !response?.data?.accessToken) {
|
||||||
|
console.error("Login failed or no access token in response");
|
||||||
|
throw new Error(response?.message || "Giriş başarısız");
|
||||||
|
}
|
||||||
|
|
||||||
|
const { accessToken, refreshToken, user } = response.data;
|
||||||
|
const normalizedRoles = normalizeRoles(user.roles);
|
||||||
|
|
||||||
|
console.log("Login successful, creating user session object");
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: user.id,
|
||||||
|
name: user.firstName
|
||||||
|
? `${user.firstName} ${user.lastName || ""}`.trim()
|
||||||
|
: user.email.split("@")[0],
|
||||||
|
email: user.email,
|
||||||
|
accessToken,
|
||||||
|
refreshToken,
|
||||||
|
roles: normalizedRoles,
|
||||||
|
};
|
||||||
|
} catch (error: unknown) {
|
||||||
|
console.error("Authorize error detailed:", error);
|
||||||
|
const err = error as Error & {
|
||||||
|
response?: { data: unknown; status: number };
|
||||||
|
};
|
||||||
|
if (err.response) {
|
||||||
|
console.error("Error response data:", err.response.data);
|
||||||
|
console.error("Error response status:", err.response.status);
|
||||||
|
}
|
||||||
|
throw new Error(
|
||||||
|
err.message || "An error occurred during authentication",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
callbacks: {
|
||||||
|
async jwt({ token, user }: { token: JWT; user?: User }) {
|
||||||
|
if (user) {
|
||||||
|
token.accessToken = user.accessToken;
|
||||||
|
token.refreshToken = user.refreshToken;
|
||||||
|
token.id = user.id;
|
||||||
|
token.roles = normalizeRoles(user.roles);
|
||||||
|
}
|
||||||
|
return token;
|
||||||
|
},
|
||||||
|
async session({ session, token }: { session: Session; token: JWT }) {
|
||||||
|
session.user.id = token.id;
|
||||||
|
session.user.roles = normalizeRoles(token.roles);
|
||||||
|
session.accessToken = token.accessToken;
|
||||||
|
session.refreshToken = token.refreshToken;
|
||||||
|
return session;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pages: {
|
||||||
|
signIn: "/signin",
|
||||||
|
error: "/signin",
|
||||||
|
},
|
||||||
|
session: { strategy: "jwt" },
|
||||||
|
secret: process.env.NEXTAUTH_SECRET,
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user