next-auth-mfelfelani72 0.0.47 → 0.0.49
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.
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
"use client";
|
|
9
9
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
10
|
-
import { useState, useMemo, lazy } from "react";
|
|
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";
|
|
@@ -17,29 +18,37 @@ const LAYOUT_MAP = {
|
|
|
17
18
|
twoColumn: LazyTwoColumn,
|
|
18
19
|
};
|
|
19
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
|
-
|
|
31
|
-
if (!email)
|
|
31
|
+
if (!email) {
|
|
32
32
|
newErrors.email = "Email is required";
|
|
33
|
-
|
|
33
|
+
}
|
|
34
|
+
else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
|
34
35
|
newErrors.email = "Invalid email format";
|
|
35
|
-
|
|
36
|
+
}
|
|
37
|
+
if (!password) {
|
|
36
38
|
newErrors.password = "Password is required";
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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,30 +62,42 @@ export default function Register({ layout = "twoColumn", theme, className, overl
|
|
|
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
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
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: {
|
|
65
|
-
|
|
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
|
-
|
|
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] ||
|
|
99
|
+
return (LAYOUT_MAP[layout] ||
|
|
100
|
+
LAYOUT_MAP.twoColumn);
|
|
80
101
|
}, [layout]);
|
|
81
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')`,
|
|
@@ -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 {
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
|
23
|
-
|
|
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() +
|
|
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
|
|
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,42 @@ export function registerHandler(apiUrl) {
|
|
|
83
83
|
success: false,
|
|
84
84
|
}, { status: 500 });
|
|
85
85
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
if (data.success &&
|
|
87
|
+
((_b = (_a = data.data) === null || _a === void 0 ? void 0 : _a.token) === null || _b === void 0 ? void 0 : _b.access_token)) {
|
|
88
|
+
const cookieStore = await cookies();
|
|
89
|
+
const returnTo = (_c = cookieStore.get("returnTo")) === null || _c === void 0 ? void 0 : _c.value;
|
|
90
|
+
const lastVisited = (_d = cookieStore.get("lastVisited")) === null || _d === void 0 ? void 0 : _d.value;
|
|
91
|
+
const redirectTo = returnTo ||
|
|
92
|
+
lastVisited ||
|
|
93
|
+
"/";
|
|
90
94
|
const res = NextResponse.json({
|
|
91
|
-
message: data.message ||
|
|
95
|
+
message: data.message ||
|
|
96
|
+
"Registration successful",
|
|
92
97
|
success: true,
|
|
93
98
|
user: data.data.user,
|
|
99
|
+
redirectTo,
|
|
94
100
|
});
|
|
95
101
|
const cookieOptions = {
|
|
96
|
-
secure: process.env.NODE_ENV ===
|
|
102
|
+
secure: process.env.NODE_ENV ===
|
|
103
|
+
"production",
|
|
97
104
|
sameSite: "strict",
|
|
98
105
|
path: "/",
|
|
99
106
|
maxAge: 60 * 60 * 24 * 7,
|
|
100
107
|
};
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
+
res.cookies.set(Object.assign(Object.assign({ name: "app_key", value: JSON.stringify({
|
|
109
|
+
tk: data.data.token.access_token,
|
|
110
|
+
ud: data.data.user.uuid ||
|
|
111
|
+
data.data.user.id,
|
|
112
|
+
}) }, cookieOptions), { httpOnly: false }));
|
|
113
|
+
res.cookies.set(Object.assign(Object.assign({ name: "key_type", value: data.data.token.token_type ||
|
|
114
|
+
"Bearer" }, cookieOptions), { httpOnly: false }));
|
|
108
115
|
res.cookies.set(Object.assign(Object.assign({ name: "user", value: JSON.stringify(data.data.user) }, cookieOptions), { httpOnly: false }));
|
|
109
116
|
res.cookies.set(Object.assign(Object.assign({ name: "isLoggedIn", value: "true" }, cookieOptions), { httpOnly: false }));
|
|
110
117
|
return res;
|
|
111
118
|
}
|
|
112
|
-
// ============================================
|
|
113
|
-
// Failed response from backend
|
|
114
|
-
// ============================================
|
|
115
119
|
return NextResponse.json({
|
|
116
|
-
message: data.message ||
|
|
120
|
+
message: data.message ||
|
|
121
|
+
"Registration failed",
|
|
117
122
|
success: false,
|
|
118
123
|
errors: data.errors,
|
|
119
124
|
}, { status: 400 });
|
|
@@ -121,7 +126,8 @@ export function registerHandler(apiUrl) {
|
|
|
121
126
|
catch (err) {
|
|
122
127
|
console.error("Registration error:", err);
|
|
123
128
|
return NextResponse.json({
|
|
124
|
-
message: err.message ||
|
|
129
|
+
message: err.message ||
|
|
130
|
+
"Internal server error",
|
|
125
131
|
success: false,
|
|
126
132
|
}, { status: 500 });
|
|
127
133
|
}
|