next-auth-mfelfelani72 0.0.46 → 0.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  import React from "react";
2
2
  import type { LoginProps } from "../../../types";
3
- export default function Login({ layout, theme, className, translations, }: LoginProps): React.JSX.Element;
3
+ export default function Login({ layout, theme, className, overlayOpacity, translations, }: LoginProps): React.JSX.Element;
@@ -6,8 +6,9 @@
6
6
  * @Description:
7
7
  */
8
8
  "use client";
9
- import { jsx as _jsx } from "react/jsx-runtime";
10
- import { useState, useMemo, lazy } from "react";
9
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
10
+ import { useState, useMemo, lazy, } from "react";
11
+ import { useRouter } from "next/navigation";
11
12
  // Components
12
13
  const LazyTwoColumn = lazy(() => import("../../../components/demo/auth/layouts/twoColumn/LoginLanding"));
13
14
  // Constants
@@ -17,7 +18,8 @@ const LAYOUT_MAP = {
17
18
  // Functions
18
19
  import { cn } from "forma-li";
19
20
  import { setTranslationOverrides } from "../../../locale";
20
- export default function Login({ layout, theme, className, translations, }) {
21
+ export default function Login({ layout, theme, className, overlayOpacity = 0, translations, }) {
22
+ const router = useRouter();
21
23
  setTranslationOverrides(translations);
22
24
  const [errors, setErrors] = useState({});
23
25
  const [message, setMessage] = useState(null);
@@ -27,14 +29,18 @@ export default function Login({ layout, theme, className, translations, }) {
27
29
  const email = formData.get("email") || "";
28
30
  const password = formData.get("password") || "";
29
31
  const newErrors = {};
30
- if (!email.trim())
32
+ if (!email.trim()) {
31
33
  newErrors.email = "Email is required";
32
- else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email))
34
+ }
35
+ else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
33
36
  newErrors.email = "Invalid email format";
34
- if (!password)
37
+ }
38
+ if (!password) {
35
39
  newErrors.password = "Password is required";
36
- else if (password.length < 6)
40
+ }
41
+ else if (password.length < 6) {
37
42
  newErrors.password = "Password must be at least 6 characters";
43
+ }
38
44
  if (Object.keys(newErrors).length > 0) {
39
45
  setErrors(newErrors);
40
46
  setMessage("Please fix the errors below.");
@@ -43,15 +49,22 @@ export default function Login({ layout, theme, className, translations, }) {
43
49
  try {
44
50
  const res = await fetch("/api/auth/login", {
45
51
  method: "POST",
46
- headers: { "Content-Type": "application/json" },
47
- body: JSON.stringify({ email, password }),
52
+ headers: {
53
+ "Content-Type": "application/json",
54
+ },
55
+ body: JSON.stringify({
56
+ email,
57
+ password,
58
+ }),
48
59
  });
49
60
  const data = await res.json();
61
+ console.log("LOGIN RESPONSE:", data);
50
62
  if (!res.ok) {
51
63
  setMessage((data === null || data === void 0 ? void 0 : data.message) || "Login failed");
52
64
  return;
53
65
  }
54
- setMessage((data === null || data === void 0 ? void 0 : data.message) || "Login successful");
66
+ const redirectTo = (data === null || data === void 0 ? void 0 : data.redirectTo) || "/";
67
+ router.push(redirectTo);
55
68
  }
56
69
  catch (err) {
57
70
  setMessage((err === null || err === void 0 ? void 0 : err.message) || "Network error");
@@ -60,7 +73,9 @@ export default function Login({ layout, theme, className, translations, }) {
60
73
  const RenderUi = useMemo(() => {
61
74
  return LAYOUT_MAP[layout || ""] || LAYOUT_MAP.twoColumn;
62
75
  }, [layout]);
63
- return (_jsx("div", { style: {
76
+ return (_jsxs("div", { className: cn("relative z-10 w-full max-w-md px-4 max-h-screen bg-cover bg-center bg-fixed overflow-hidden", className), style: {
64
77
  backgroundImage: `url('/images/jpg/${theme || "default"}_auth.jpeg')`,
65
- }, className: cn("zz-10 w-full max-w-md px-4 max-h-screen bg-cover bg-center bg-fixed overflow-hidden", className), children: _jsx(RenderUi, { onSubmit: handleSubmit, errors: errors, message: message, theme: theme, translations: translations }) }));
78
+ }, children: [_jsx("div", { className: "absolute inset-0 bg-black pointer-events-none", style: {
79
+ opacity: overlayOpacity,
80
+ } }), _jsx("div", { className: "relative z-10", children: _jsx(RenderUi, { onSubmit: handleSubmit, errors: errors, message: message, theme: theme, translations: translations }) })] }));
66
81
  }
@@ -1,3 +1,3 @@
1
1
  import React from "react";
2
2
  import type { RegisterProps } from "../../../types";
3
- export default function Register({ layout, theme, className, translations, }: RegisterProps): React.JSX.Element;
3
+ export default function Register({ layout, theme, className, overlayOpacity, translations, }: RegisterProps): React.JSX.Element;
@@ -6,8 +6,9 @@
6
6
  * @Description:
7
7
  */
8
8
  "use client";
9
- import { jsx as _jsx } from "react/jsx-runtime";
10
- import { useState, useMemo, lazy } from "react";
9
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
10
+ import { useState, useMemo, lazy, } from "react";
11
+ import { useRouter } from "next/navigation";
11
12
  // Functions
12
13
  import { cn } from "forma-li";
13
14
  import { setTranslationOverrides } from "../../../locale";
@@ -16,30 +17,38 @@ const LazyTwoColumn = lazy(() => import("../../../components/demo/auth/layouts/t
16
17
  const LAYOUT_MAP = {
17
18
  twoColumn: LazyTwoColumn,
18
19
  };
19
- export default function Register({ layout = "twoColumn", theme, className, translations, }) {
20
+ export default function Register({ layout = "twoColumn", theme, className, overlayOpacity = 0, translations, }) {
21
+ const router = useRouter();
20
22
  setTranslationOverrides(translations);
21
23
  const [errors, setErrors] = useState({});
22
24
  const [message, setMessage] = useState(null);
23
25
  const validate = (formData) => {
24
26
  var _a, _b, _c;
25
27
  const newErrors = {};
26
- // const name = formData.get("name")?.toString().trim();
27
28
  const email = (_a = formData.get("email")) === null || _a === void 0 ? void 0 : _a.toString().trim();
28
29
  const password = (_b = formData.get("password")) === null || _b === void 0 ? void 0 : _b.toString();
29
30
  const confirmPassword = (_c = formData.get("confirmPassword")) === null || _c === void 0 ? void 0 : _c.toString();
30
- // if (!name) newErrors.name = "Name is required";
31
- if (!email)
31
+ if (!email) {
32
32
  newErrors.email = "Email is required";
33
- else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email))
33
+ }
34
+ else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
34
35
  newErrors.email = "Invalid email format";
35
- if (!password)
36
+ }
37
+ if (!password) {
36
38
  newErrors.password = "Password is required";
37
- else if (password.length < 6)
38
- newErrors.password = "Password must be at least 6 characters";
39
- if (!confirmPassword)
40
- newErrors.confirmPassword = "Confirm your password";
41
- else if (password !== confirmPassword)
42
- newErrors.confirmPassword = "Passwords do not match";
39
+ }
40
+ else if (password.length < 6) {
41
+ newErrors.password =
42
+ "Password must be at least 6 characters";
43
+ }
44
+ if (!confirmPassword) {
45
+ newErrors.confirmPassword =
46
+ "Confirm your password";
47
+ }
48
+ else if (password !== confirmPassword) {
49
+ newErrors.confirmPassword =
50
+ "Passwords do not match";
51
+ }
43
52
  return newErrors;
44
53
  };
45
54
  const handleSubmit = async (formData) => {
@@ -53,32 +62,46 @@ export default function Register({ layout = "twoColumn", theme, className, trans
53
62
  return;
54
63
  }
55
64
  try {
56
- // const name = formData.get("name")?.toString().trim();
57
65
  const name = "sky";
58
- const email = (_a = formData.get("email")) === null || _a === void 0 ? void 0 : _a.toString().trim();
59
- const password = (_b = formData.get("password")) === null || _b === void 0 ? void 0 : _b.toString();
60
- const confirmPassword = (_c = formData.get("confirmPassword")) === null || _c === void 0 ? void 0 : _c.toString();
61
- console.log(formData);
66
+ const email = (_a = formData
67
+ .get("email")) === null || _a === void 0 ? void 0 : _a.toString().trim();
68
+ const password = (_b = formData
69
+ .get("password")) === null || _b === void 0 ? void 0 : _b.toString();
70
+ const confirmPassword = (_c = formData
71
+ .get("confirmPassword")) === null || _c === void 0 ? void 0 : _c.toString();
62
72
  const res = await fetch("/api/auth/register", {
63
73
  method: "POST",
64
- headers: { "Content-Type": "application/json" },
65
- body: JSON.stringify({ name, email, password, confirmPassword }),
74
+ headers: {
75
+ "Content-Type": "application/json",
76
+ },
77
+ body: JSON.stringify({
78
+ name,
79
+ email,
80
+ password,
81
+ confirmPassword,
82
+ }),
66
83
  });
67
84
  const data = await res.json();
85
+ console.log("REGISTER RESPONSE:", data);
68
86
  if (!res.ok) {
69
87
  setMessage((data === null || data === void 0 ? void 0 : data.message) || "Registration failed");
70
88
  return;
71
89
  }
72
- setMessage((data === null || data === void 0 ? void 0 : data.message) || "Registration successful");
90
+ const redirectTo = (data === null || data === void 0 ? void 0 : data.redirectTo) || "/";
91
+ console.log("REGISTER REDIRECT TO:", redirectTo);
92
+ router.push(redirectTo);
73
93
  }
74
94
  catch (err) {
75
95
  setMessage((err === null || err === void 0 ? void 0 : err.message) || "Network error");
76
96
  }
77
97
  };
78
98
  const RenderUi = useMemo(() => {
79
- return LAYOUT_MAP[layout] || LAYOUT_MAP.twoColumn;
99
+ return (LAYOUT_MAP[layout] ||
100
+ LAYOUT_MAP.twoColumn);
80
101
  }, [layout]);
81
- return (_jsx("div", { style: {
102
+ return (_jsxs("div", { className: cn("relative z-10 w-full max-w-md px-4 max-h-screen bg-cover bg-center bg-fixed overflow-hidden", className), style: {
82
103
  backgroundImage: `url('/images/jpg/${theme || "default"}_auth.jpeg')`,
83
- }, className: cn("z-10 w-full max-w-md px-4 max-h-screen bg-cover bg-center bg-fixed overflow-hidden", className), children: _jsx(RenderUi, { onSubmit: handleSubmit, errors: errors, message: message, theme: theme, translations: translations }) }));
104
+ }, children: [_jsx("div", { className: "absolute inset-0 bg-black pointer-events-none", style: {
105
+ opacity: overlayOpacity,
106
+ } }), _jsx("div", { className: "relative z-10", children: _jsx(RenderUi, { onSubmit: handleSubmit, errors: errors, message: message, theme: theme, translations: translations }) })] }));
84
107
  }
@@ -5,18 +5,19 @@
5
5
  * @Date: 2025-12-31 06:00:17
6
6
  * @Description: Login handler with Next.js native cookies
7
7
  */
8
+ import { cookies } from "next/headers";
8
9
  import { NextResponse } from "next/server";
9
10
  // Functions
10
11
  import { cns } from "forma-li";
11
12
  export function loginHandler(apiUrl) {
12
13
  return async function POST(req) {
13
- var _a, _b;
14
+ var _a, _b, _c, _d;
14
15
  try {
15
16
  const body = await req.json();
16
17
  const data = await cns({
17
18
  method: "post",
18
19
  endPoint: apiUrl,
19
- body: body,
20
+ body,
20
21
  route: "/login",
21
22
  });
22
23
  if (!data) {
@@ -26,10 +27,15 @@ export function loginHandler(apiUrl) {
26
27
  }, { status: 500 });
27
28
  }
28
29
  if (data.success && ((_b = (_a = data.data) === null || _a === void 0 ? void 0 : _a.token) === null || _b === void 0 ? void 0 : _b.access_token)) {
30
+ const cookieStore = await cookies();
31
+ const returnTo = (_c = cookieStore.get("returnTo")) === null || _c === void 0 ? void 0 : _c.value;
32
+ const lastVisited = (_d = cookieStore.get("lastVisited")) === null || _d === void 0 ? void 0 : _d.value;
33
+ const redirectTo = returnTo || lastVisited || "/";
29
34
  const res = NextResponse.json({
30
35
  message: "Login successful",
31
36
  success: true,
32
37
  user: data.data.user,
38
+ redirectTo,
33
39
  });
34
40
  const cookieOptions = {
35
41
  secure: process.env.NODE_ENV === "production",
@@ -39,9 +45,11 @@ export function loginHandler(apiUrl) {
39
45
  };
40
46
  res.cookies.set(Object.assign(Object.assign({ name: "app_key", value: JSON.stringify({
41
47
  tk: data.data.token.access_token,
42
- ud: data.data.user.uuid || data.data.user.id,
48
+ ud: data.data.user.uuid ||
49
+ data.data.user.id,
43
50
  }) }, cookieOptions), { httpOnly: false }));
44
- res.cookies.set(Object.assign(Object.assign({ name: "key_type", value: data.data.token.token_type || "Bearer" }, cookieOptions), { httpOnly: false }));
51
+ res.cookies.set(Object.assign(Object.assign({ name: "key_type", value: data.data.token.token_type ||
52
+ "Bearer" }, cookieOptions), { httpOnly: false }));
45
53
  res.cookies.set(Object.assign(Object.assign({ name: "user", value: JSON.stringify(data.data.user) }, cookieOptions), { httpOnly: false }));
46
54
  res.cookies.set(Object.assign(Object.assign({ name: "isLoggedIn", value: "true" }, cookieOptions), { httpOnly: false }));
47
55
  return res;
@@ -54,7 +62,8 @@ export function loginHandler(apiUrl) {
54
62
  catch (err) {
55
63
  console.error("Login error:", err);
56
64
  return NextResponse.json({
57
- message: err.message || "Internal server error",
65
+ message: err.message ||
66
+ "Internal server error",
58
67
  success: false,
59
68
  }, { status: 500 });
60
69
  }
@@ -1,26 +1,33 @@
1
1
  /*
2
2
  * @Author: Mohammad Felfelani
3
3
  * @Email: mfelfelani72@gmail.com
4
+ * @Team:
4
5
  * @Date: 2025-12-31 06:00:17
5
6
  * @Description: Register handler with Next.js native cookies
6
7
  */
7
- import { NextResponse } from "next/server";
8
+ import { cookies } from "next/headers";
9
+ import { NextResponse, } from "next/server";
10
+ // Functions
8
11
  import { cns } from "forma-li";
9
12
  export function registerHandler(apiUrl) {
10
- console.log("l.ksdhfkjsdfkjsds");
11
13
  return async function POST(req) {
12
- var _a, _b;
14
+ var _a, _b, _c, _d;
13
15
  try {
14
16
  const body = await req.json();
15
- console.log(body);
16
- const { name, email, password, confirmPassword } = body;
17
- if (!email || !password || !confirmPassword) {
17
+ const { name, email, password, confirmPassword, } = body;
18
+ if (!email ||
19
+ !password ||
20
+ !confirmPassword) {
18
21
  return NextResponse.json({
19
22
  message: "Email and password are required",
20
23
  success: false,
21
24
  errors: {
22
- email: !email ? "Email is required" : undefined,
23
- password: !password ? "Password is required" : undefined,
25
+ email: !email
26
+ ? "Email is required"
27
+ : undefined,
28
+ password: !password
29
+ ? "Password is required"
30
+ : undefined,
24
31
  confirmPassword: !confirmPassword
25
32
  ? "Confirm password is required"
26
33
  : undefined,
@@ -54,23 +61,16 @@ export function registerHandler(apiUrl) {
54
61
  },
55
62
  }, { status: 400 });
56
63
  }
57
- // ============================================
58
- // Build payload for backend API
59
- // name رو از email میسازیم
60
- // ============================================
61
64
  const nameFromEmail = email.split("@")[0];
62
- const displayName = nameFromEmail.charAt(0).toUpperCase() + nameFromEmail.slice(1);
65
+ const displayName = nameFromEmail.charAt(0).toUpperCase() +
66
+ nameFromEmail.slice(1);
63
67
  const payload = {
64
68
  name: displayName,
65
69
  email: email.trim().toLowerCase(),
66
- password: password,
70
+ password,
67
71
  password_confirmation: confirmPassword,
68
72
  username: nameFromEmail.toLowerCase(),
69
73
  };
70
- console.log(payload);
71
- // ============================================
72
- // Send request to backend
73
- // ============================================
74
74
  const data = await cns({
75
75
  method: "post",
76
76
  endPoint: apiUrl,
@@ -83,37 +83,51 @@ export function registerHandler(apiUrl) {
83
83
  success: false,
84
84
  }, { status: 500 });
85
85
  }
86
- // ============================================
87
- // Success - set cookies if token exists
88
- // ============================================
89
- if (data.success && ((_a = data.data) === null || _a === void 0 ? void 0 : _a.user)) {
86
+ if (data.success &&
87
+ ((_a = data.data) === null || _a === void 0 ? void 0 : _a.user)) {
88
+ const cookieStore = await cookies();
89
+ const returnTo = (_b = cookieStore.get("returnTo")) === null || _b === void 0 ? void 0 : _b.value;
90
+ const lastVisited = (_c = cookieStore.get("lastVisited")) === null || _c === void 0 ? void 0 : _c.value;
91
+ const redirectTo = returnTo ||
92
+ lastVisited ||
93
+ "/";
94
+ console.log("AUTH REGISTER REDIRECT:", {
95
+ returnTo,
96
+ lastVisited,
97
+ redirectTo,
98
+ });
90
99
  const res = NextResponse.json({
91
- message: data.message || "Registration successful",
100
+ message: data.message ||
101
+ "Registration successful",
92
102
  success: true,
93
103
  user: data.data.user,
104
+ redirectTo,
94
105
  });
95
106
  const cookieOptions = {
96
- secure: process.env.NODE_ENV === "production",
107
+ secure: process.env.NODE_ENV ===
108
+ "production",
97
109
  sameSite: "strict",
98
110
  path: "/",
99
111
  maxAge: 60 * 60 * 24 * 7,
100
112
  };
101
- if ((_b = data.data.token) === null || _b === void 0 ? void 0 : _b.access_token) {
113
+ if ((_d = data.data.token) === null || _d === void 0 ? void 0 : _d.access_token) {
102
114
  res.cookies.set(Object.assign(Object.assign({ name: "app_key", value: JSON.stringify({
103
- tk: data.data.token.access_token,
104
- ud: data.data.user.uuid || data.data.user.id,
115
+ tk: data.data.token
116
+ .access_token,
117
+ ud: data.data.user.uuid ||
118
+ data.data.user.id,
105
119
  }) }, cookieOptions), { httpOnly: false }));
106
- res.cookies.set(Object.assign(Object.assign({ name: "key_type", value: data.data.token.token_type || "Bearer" }, cookieOptions), { httpOnly: false }));
120
+ res.cookies.set(Object.assign(Object.assign({ name: "key_type", value: data.data.token
121
+ .token_type ||
122
+ "Bearer" }, cookieOptions), { httpOnly: false }));
107
123
  }
108
124
  res.cookies.set(Object.assign(Object.assign({ name: "user", value: JSON.stringify(data.data.user) }, cookieOptions), { httpOnly: false }));
109
125
  res.cookies.set(Object.assign(Object.assign({ name: "isLoggedIn", value: "true" }, cookieOptions), { httpOnly: false }));
110
126
  return res;
111
127
  }
112
- // ============================================
113
- // Failed response from backend
114
- // ============================================
115
128
  return NextResponse.json({
116
- message: data.message || "Registration failed",
129
+ message: data.message ||
130
+ "Registration failed",
117
131
  success: false,
118
132
  errors: data.errors,
119
133
  }, { status: 400 });
@@ -121,7 +135,8 @@ export function registerHandler(apiUrl) {
121
135
  catch (err) {
122
136
  console.error("Registration error:", err);
123
137
  return NextResponse.json({
124
- message: err.message || "Internal server error",
138
+ message: err.message ||
139
+ "Internal server error",
125
140
  success: false,
126
141
  }, { status: 500 });
127
142
  }
@@ -13,6 +13,7 @@ export interface LoginProps {
13
13
  layout: string;
14
14
  theme?: string;
15
15
  className?: string;
16
+ overlayOpacity?: number;
16
17
  translations?: Partial<Record<Lang, Partial<Dictionary>>>;
17
18
  }
18
19
  export interface RegisterComponentProps {
@@ -31,5 +32,6 @@ export interface RegisterProps {
31
32
  layout: string;
32
33
  theme?: string;
33
34
  className?: string;
35
+ overlayOpacity?: number;
34
36
  translations?: Partial<Record<Lang, Partial<Dictionary>>>;
35
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-auth-mfelfelani72",
3
- "version": "0.0.46",
3
+ "version": "0.0.48",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [