sitepaige-mcp-server 1.0.2 → 1.1.0
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.
- package/components/IntegrationComponent.tsx +1 -0
- package/components/admin.tsx +30 -27
- package/components/auth.tsx +9 -9
- package/components/cta.tsx +3 -10
- package/components/headerlogin.tsx +9 -9
- package/components/login.tsx +90 -11
- package/components/logincallback.tsx +1 -0
- package/components/menu.tsx +0 -6
- package/components/profile.tsx +12 -11
- package/defaultapp/api/Auth/resend-verification/route.ts +130 -0
- package/defaultapp/api/Auth/route.ts +39 -49
- package/defaultapp/api/Auth/signup/route.ts +5 -15
- package/defaultapp/api/Auth/verify-email/route.ts +12 -5
- package/defaultapp/api/admin/users/route.ts +5 -3
- package/defaultapp/auth/auth.ts +9 -9
- package/defaultapp/db-mysql.ts +1 -1
- package/defaultapp/db-password-auth.ts +37 -0
- package/defaultapp/db-postgres.ts +1 -1
- package/defaultapp/db-sqlite.ts +1 -1
- package/defaultapp/db-users.ts +73 -73
- package/defaultapp/middleware.ts +15 -17
- package/dist/components/IntegrationComponent.tsx +1 -0
- package/dist/components/admin.tsx +30 -27
- package/dist/components/auth.tsx +9 -9
- package/dist/components/cta.tsx +3 -10
- package/dist/components/headerlogin.tsx +9 -9
- package/dist/components/login.tsx +90 -11
- package/dist/components/logincallback.tsx +1 -0
- package/dist/components/menu.tsx +0 -6
- package/dist/components/profile.tsx +12 -11
- package/dist/defaultapp/api/Auth/resend-verification/route.ts +130 -0
- package/dist/defaultapp/api/Auth/route.ts +39 -49
- package/dist/defaultapp/api/Auth/signup/route.ts +5 -15
- package/dist/defaultapp/api/Auth/verify-email/route.ts +12 -5
- package/dist/defaultapp/api/admin/users/route.ts +5 -3
- package/dist/defaultapp/auth/auth.ts +9 -9
- package/dist/defaultapp/db-mysql.ts +1 -1
- package/dist/defaultapp/db-password-auth.ts +37 -0
- package/dist/defaultapp/db-postgres.ts +1 -1
- package/dist/defaultapp/db-sqlite.ts +1 -1
- package/dist/defaultapp/db-users.ts +73 -73
- package/dist/defaultapp/middleware.ts +15 -17
- package/dist/generators/sql.js +11 -3
- package/dist/generators/sql.js.map +1 -1
- package/dist/generators/views.js +14 -14
- package/dist/generators/views.js.map +1 -1
- package/package.json +1 -1
|
@@ -9,7 +9,7 @@ checked in the system build settings. It is safe to modify this file without it
|
|
|
9
9
|
import React, { useState } from 'react';
|
|
10
10
|
|
|
11
11
|
interface LoginProps {
|
|
12
|
-
providers: ('apple' | 'facebook' | 'github' | 'google' | '
|
|
12
|
+
providers: ('apple' | 'facebook' | 'github' | 'google' | 'userpass')[];
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export default function Login({ providers }: LoginProps) {
|
|
@@ -20,6 +20,8 @@ export default function Login({ providers }: LoginProps) {
|
|
|
20
20
|
const [confirmPassword, setConfirmPassword] = useState('');
|
|
21
21
|
const [isLoading, setIsLoading] = useState(false);
|
|
22
22
|
const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null);
|
|
23
|
+
const [showResendVerification, setShowResendVerification] = useState(false);
|
|
24
|
+
const [resendEmail, setResendEmail] = useState('');
|
|
23
25
|
|
|
24
26
|
|
|
25
27
|
const handleProviderLogin = async (provider: string) => {
|
|
@@ -49,6 +51,37 @@ export default function Login({ providers }: LoginProps) {
|
|
|
49
51
|
|
|
50
52
|
};
|
|
51
53
|
|
|
54
|
+
const handleResendVerification = async () => {
|
|
55
|
+
setError(null);
|
|
56
|
+
setMessage(null);
|
|
57
|
+
setIsLoading(true);
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
const response = await fetch('/api/Auth/resend-verification', {
|
|
61
|
+
method: 'POST',
|
|
62
|
+
headers: {
|
|
63
|
+
'Content-Type': 'application/json'
|
|
64
|
+
},
|
|
65
|
+
credentials: 'include', // Include cookies in request
|
|
66
|
+
body: JSON.stringify({ email: resendEmail || email })
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const data = await response.json();
|
|
70
|
+
|
|
71
|
+
if (response.ok && data.success) {
|
|
72
|
+
setMessage({ type: 'success', text: data.message || 'Verification email sent! Please check your inbox.' });
|
|
73
|
+
setShowResendVerification(false);
|
|
74
|
+
setResendEmail('');
|
|
75
|
+
} else {
|
|
76
|
+
setError(data.error || 'Failed to send verification email');
|
|
77
|
+
}
|
|
78
|
+
} catch (err) {
|
|
79
|
+
setError('An unexpected error occurred');
|
|
80
|
+
} finally {
|
|
81
|
+
setIsLoading(false);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
52
85
|
const handleUsernamePasswordAuth = async (e: React.FormEvent) => {
|
|
53
86
|
e.preventDefault();
|
|
54
87
|
setError(null);
|
|
@@ -66,7 +99,10 @@ export default function Login({ providers }: LoginProps) {
|
|
|
66
99
|
|
|
67
100
|
const response = await fetch('/api/Auth/signup', {
|
|
68
101
|
method: 'POST',
|
|
69
|
-
headers: {
|
|
102
|
+
headers: {
|
|
103
|
+
'Content-Type': 'application/json'
|
|
104
|
+
},
|
|
105
|
+
credentials: 'include', // Include cookies in request
|
|
70
106
|
body: JSON.stringify({ email, password })
|
|
71
107
|
});
|
|
72
108
|
|
|
@@ -84,8 +120,11 @@ export default function Login({ providers }: LoginProps) {
|
|
|
84
120
|
// Handle login
|
|
85
121
|
const response = await fetch('/api/Auth', {
|
|
86
122
|
method: 'POST',
|
|
87
|
-
headers: {
|
|
88
|
-
|
|
123
|
+
headers: {
|
|
124
|
+
'Content-Type': 'application/json'
|
|
125
|
+
},
|
|
126
|
+
credentials: 'include', // Include cookies in request
|
|
127
|
+
body: JSON.stringify({ email, password, provider: 'userpass' })
|
|
89
128
|
});
|
|
90
129
|
|
|
91
130
|
const data = await response.json();
|
|
@@ -95,6 +134,11 @@ export default function Login({ providers }: LoginProps) {
|
|
|
95
134
|
window.location.href = '/';
|
|
96
135
|
} else {
|
|
97
136
|
setError(data.error || 'Login failed');
|
|
137
|
+
// Check if error is about email verification
|
|
138
|
+
if (response.status === 403 || data.error?.toLowerCase().includes('verify')) {
|
|
139
|
+
setShowResendVerification(true);
|
|
140
|
+
setResendEmail(email);
|
|
141
|
+
}
|
|
98
142
|
}
|
|
99
143
|
}
|
|
100
144
|
} catch (err) {
|
|
@@ -104,8 +148,8 @@ export default function Login({ providers }: LoginProps) {
|
|
|
104
148
|
}
|
|
105
149
|
};
|
|
106
150
|
|
|
107
|
-
const showUsernamePasswordForm = providers?.includes('
|
|
108
|
-
const oauthProviders = providers?.filter(p => p !== '
|
|
151
|
+
const showUsernamePasswordForm = providers?.includes('userpass');
|
|
152
|
+
const oauthProviders = providers?.filter(p => p !== 'userpass') || [];
|
|
109
153
|
|
|
110
154
|
return (
|
|
111
155
|
<div className="flex flex-col items-center justify-center min-h-[500px] p-4">
|
|
@@ -179,17 +223,18 @@ export default function Login({ providers }: LoginProps) {
|
|
|
179
223
|
</div>
|
|
180
224
|
|
|
181
225
|
<div className="text-center">
|
|
182
|
-
<
|
|
183
|
-
|
|
184
|
-
onClick={() => {
|
|
226
|
+
<a
|
|
227
|
+
href="#"
|
|
228
|
+
onClick={(e) => {
|
|
229
|
+
e.preventDefault();
|
|
185
230
|
setIsSignup(!isSignup);
|
|
186
231
|
setError(null);
|
|
187
232
|
setMessage(null);
|
|
188
233
|
}}
|
|
189
|
-
className="text-sm text-indigo-600 hover:text-indigo-500"
|
|
234
|
+
className="text-sm text-indigo-600 hover:text-indigo-500 underline"
|
|
190
235
|
>
|
|
191
236
|
{isSignup ? 'Already have an account? Sign in' : "Don't have an account? Sign up"}
|
|
192
|
-
</
|
|
237
|
+
</a>
|
|
193
238
|
</div>
|
|
194
239
|
</form>
|
|
195
240
|
)}
|
|
@@ -234,6 +279,40 @@ export default function Login({ providers }: LoginProps) {
|
|
|
234
279
|
{error}
|
|
235
280
|
</div>
|
|
236
281
|
)}
|
|
282
|
+
|
|
283
|
+
{showResendVerification && (
|
|
284
|
+
<div className="mt-4 p-4 border border-gray-200 rounded-md bg-gray-50">
|
|
285
|
+
<p className="text-sm text-gray-700 mb-3">
|
|
286
|
+
Need a new verification email? Enter your email address and we'll send you a new link.
|
|
287
|
+
</p>
|
|
288
|
+
<div className="space-y-3">
|
|
289
|
+
<input
|
|
290
|
+
type="email"
|
|
291
|
+
value={resendEmail}
|
|
292
|
+
onChange={(e) => setResendEmail(e.target.value)}
|
|
293
|
+
placeholder="Email address"
|
|
294
|
+
className="block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 text-sm"
|
|
295
|
+
/>
|
|
296
|
+
<button
|
|
297
|
+
onClick={handleResendVerification}
|
|
298
|
+
disabled={isLoading || !resendEmail}
|
|
299
|
+
className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50"
|
|
300
|
+
>
|
|
301
|
+
{isLoading ? 'Sending...' : 'Resend Verification Email'}
|
|
302
|
+
</button>
|
|
303
|
+
<button
|
|
304
|
+
type="button"
|
|
305
|
+
onClick={() => {
|
|
306
|
+
setShowResendVerification(false);
|
|
307
|
+
setResendEmail('');
|
|
308
|
+
}}
|
|
309
|
+
className="w-full text-sm text-gray-500 hover:text-gray-700 bg-transparent border-0 p-0 underline"
|
|
310
|
+
>
|
|
311
|
+
Cancel
|
|
312
|
+
</button>
|
|
313
|
+
</div>
|
|
314
|
+
</div>
|
|
315
|
+
)}
|
|
237
316
|
</div>
|
|
238
317
|
</div>
|
|
239
318
|
);
|
package/dist/components/menu.tsx
CHANGED
|
@@ -60,7 +60,6 @@ export default function Menu({ menu, onClick, pages = [] }: MenuProps) {
|
|
|
60
60
|
const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
|
|
61
61
|
const [isMobile, setIsMobile] = useState(false);
|
|
62
62
|
const [selectedPage, setSelectedPage] = useState<string | null>(null);
|
|
63
|
-
const [isPaigeLoading, setIsPaigeLoading] = useState(false);
|
|
64
63
|
|
|
65
64
|
// Handle case where menu is undefined/null
|
|
66
65
|
if (!menu) {
|
|
@@ -230,7 +229,6 @@ export default function Menu({ menu, onClick, pages = [] }: MenuProps) {
|
|
|
230
229
|
items-center
|
|
231
230
|
justify-center
|
|
232
231
|
${isSelected ? 'border-blue-600 bg-blue-50' : ''}
|
|
233
|
-
${isPaigeLoading ? 'opacity-50 cursor-not-allowed' : ''}
|
|
234
232
|
`}>
|
|
235
233
|
<h3
|
|
236
234
|
className={`${isSelected ? 'font-bold' : 'font-medium'} text-gray-800`}
|
|
@@ -284,10 +282,6 @@ export default function Menu({ menu, onClick, pages = [] }: MenuProps) {
|
|
|
284
282
|
href={linkUrl}
|
|
285
283
|
onClick={(e) => {
|
|
286
284
|
e.preventDefault();
|
|
287
|
-
if (isPaigeLoading) {
|
|
288
|
-
console.log('Navigation blocked: Paige is currently processing a request');
|
|
289
|
-
return;
|
|
290
|
-
}
|
|
291
285
|
setSelectedPage(item.page);
|
|
292
286
|
onClick?.();
|
|
293
287
|
}}
|
|
@@ -9,9 +9,9 @@ checked in the system build settings. It is safe to modify this file without it
|
|
|
9
9
|
import { useState, useEffect } from 'react';
|
|
10
10
|
|
|
11
11
|
interface UserProfile {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
id: string;
|
|
13
|
+
username: string;
|
|
14
|
+
avatarurl: string | null;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export default function Profile() {
|
|
@@ -23,7 +23,8 @@ export default function Profile() {
|
|
|
23
23
|
const fetchProfile = async () => {
|
|
24
24
|
try {
|
|
25
25
|
const response = await fetch('/api/Auth', {
|
|
26
|
-
method: 'GET'
|
|
26
|
+
method: 'GET',
|
|
27
|
+
credentials: 'include'
|
|
27
28
|
});
|
|
28
29
|
|
|
29
30
|
if (!response.ok) {
|
|
@@ -39,9 +40,9 @@ export default function Profile() {
|
|
|
39
40
|
// Use the user object from the response
|
|
40
41
|
const userData = data.user;
|
|
41
42
|
setProfile({
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
id: userData.userid,
|
|
44
|
+
username: userData.username,
|
|
45
|
+
avatarurl: userData.avatarurl
|
|
45
46
|
});
|
|
46
47
|
} catch (err) {
|
|
47
48
|
setError(err instanceof Error ? err.message : 'An error occurred');
|
|
@@ -68,17 +69,17 @@ export default function Profile() {
|
|
|
68
69
|
return (
|
|
69
70
|
<div className="max-w-md mx-auto mt-8 p-6 bg-white rounded-lg shadow-md">
|
|
70
71
|
<div className="flex flex-col items-center">
|
|
71
|
-
{profile.
|
|
72
|
+
{profile.avatarurl && (
|
|
72
73
|
<img
|
|
73
|
-
src={profile.
|
|
74
|
+
src={profile.avatarurl}
|
|
74
75
|
alt="Profile avatar"
|
|
75
76
|
className="w-32 h-32 rounded-full mb-4"
|
|
76
77
|
referrerPolicy="no-referrer"
|
|
77
78
|
crossOrigin="anonymous"
|
|
78
79
|
/>
|
|
79
80
|
)}
|
|
80
|
-
<h2 className="text-2xl font-semibold text-gray-800">{profile.
|
|
81
|
-
<p className="text-gray-500 mt-2">User ID: {profile.
|
|
81
|
+
<h2 className="text-2xl font-semibold text-gray-800">{profile.username}</h2>
|
|
82
|
+
<p className="text-gray-500 mt-2">User ID: {profile.id}</p>
|
|
82
83
|
</div>
|
|
83
84
|
</div>
|
|
84
85
|
);
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Resend verification email endpoint
|
|
3
|
+
* Allows users to request a new verification email
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { NextResponse } from 'next/server';
|
|
7
|
+
import { regenerateVerificationToken } from '../../../db-password-auth';
|
|
8
|
+
import { send_email } from '../../../storage/email';
|
|
9
|
+
|
|
10
|
+
// Email validation regex
|
|
11
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
12
|
+
|
|
13
|
+
export async function POST(request: Request) {
|
|
14
|
+
try {
|
|
15
|
+
const { email } = await request.json();
|
|
16
|
+
|
|
17
|
+
// Validate email format
|
|
18
|
+
if (!email || !emailRegex.test(email)) {
|
|
19
|
+
return NextResponse.json(
|
|
20
|
+
{ error: 'Invalid email address' },
|
|
21
|
+
{ status: 400 }
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Regenerate verification token
|
|
26
|
+
const result = await regenerateVerificationToken(email);
|
|
27
|
+
|
|
28
|
+
if (!result) {
|
|
29
|
+
// Don't reveal whether the email exists or not
|
|
30
|
+
return NextResponse.json({
|
|
31
|
+
success: true,
|
|
32
|
+
message: 'If an unverified account exists with this email, a verification email has been sent.'
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const { verificationToken } = result;
|
|
37
|
+
|
|
38
|
+
// Get site domain from environment or default
|
|
39
|
+
const siteDomain = process.env.SITE_DOMAIN || 'https://sitepaige.com';
|
|
40
|
+
const verificationUrl = `${siteDomain}/api/Auth/verify-email?token=${verificationToken}`;
|
|
41
|
+
|
|
42
|
+
// Send verification email
|
|
43
|
+
try {
|
|
44
|
+
const emailHtml = `
|
|
45
|
+
<!DOCTYPE html>
|
|
46
|
+
<html>
|
|
47
|
+
<head>
|
|
48
|
+
<style>
|
|
49
|
+
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
|
|
50
|
+
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
|
|
51
|
+
.button {
|
|
52
|
+
display: inline-block;
|
|
53
|
+
padding: 12px 24px;
|
|
54
|
+
background-color: #f0f0f0;
|
|
55
|
+
color: #000000;
|
|
56
|
+
text-decoration: none;
|
|
57
|
+
border: 1px solid #cccccc;
|
|
58
|
+
border-radius: 4px;
|
|
59
|
+
}
|
|
60
|
+
.footer { margin-top: 30px; font-size: 12px; color: #666; }
|
|
61
|
+
</style>
|
|
62
|
+
</head>
|
|
63
|
+
<body>
|
|
64
|
+
<div class="container">
|
|
65
|
+
<h2>Verify Your Email Address</h2>
|
|
66
|
+
<p>You requested a new verification email for your account at ${siteDomain}. Please verify your email address by clicking the button below:</p>
|
|
67
|
+
<p style="margin: 30px 0;">
|
|
68
|
+
<a href="${verificationUrl}" class="button">Verify Email Address</a>
|
|
69
|
+
</p>
|
|
70
|
+
<p>Or copy and paste this link into your browser:</p>
|
|
71
|
+
<p style="word-break: break-all; color: #0066cc;">${verificationUrl}</p>
|
|
72
|
+
<p>This link will expire in 24 hours.</p>
|
|
73
|
+
<div class="footer">
|
|
74
|
+
<p>If you didn't request this email, you can safely ignore it.</p>
|
|
75
|
+
</div>
|
|
76
|
+
</div>
|
|
77
|
+
</body>
|
|
78
|
+
</html>
|
|
79
|
+
`;
|
|
80
|
+
|
|
81
|
+
const emailText = `
|
|
82
|
+
Verify Your Email Address
|
|
83
|
+
|
|
84
|
+
You requested a new verification email for your account at ${siteDomain}. Please verify your email address by clicking the link below:
|
|
85
|
+
|
|
86
|
+
${verificationUrl}
|
|
87
|
+
|
|
88
|
+
This link will expire in 24 hours.
|
|
89
|
+
|
|
90
|
+
If you didn't request this email, you can safely ignore it.
|
|
91
|
+
`;
|
|
92
|
+
|
|
93
|
+
await send_email({
|
|
94
|
+
to: email,
|
|
95
|
+
from: process.env.EMAIL_FROM || 'noreply@sitepaige.com',
|
|
96
|
+
subject: 'Verify your email address',
|
|
97
|
+
html: emailHtml,
|
|
98
|
+
text: emailText
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return NextResponse.json({
|
|
102
|
+
success: true,
|
|
103
|
+
message: 'Verification email sent successfully! Please check your email.'
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
} catch (emailError) {
|
|
107
|
+
console.error('Failed to send verification email:', emailError);
|
|
108
|
+
return NextResponse.json({
|
|
109
|
+
success: false,
|
|
110
|
+
error: 'Failed to send verification email. Please try again later.'
|
|
111
|
+
}, { status: 500 });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
} catch (error: any) {
|
|
115
|
+
console.error('Resend verification error:', error);
|
|
116
|
+
|
|
117
|
+
if (error.message === 'Email is already verified') {
|
|
118
|
+
return NextResponse.json(
|
|
119
|
+
{ error: 'This email is already verified. Please sign in.' },
|
|
120
|
+
{ status: 400 }
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Generic response to avoid revealing whether email exists
|
|
125
|
+
return NextResponse.json({
|
|
126
|
+
success: true,
|
|
127
|
+
message: 'If an unverified account exists with this email, a verification email has been sent.'
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -10,7 +10,6 @@ import * as crypto from 'node:crypto';
|
|
|
10
10
|
|
|
11
11
|
import { db_init, db_query } from '../../db';
|
|
12
12
|
import { upsertUser, storeOAuthToken, validateSession, rotateSession } from '../../db-users';
|
|
13
|
-
import { validateCsrfToken } from '../../csrf';
|
|
14
13
|
|
|
15
14
|
type OAuthProvider = 'google' | 'facebook' | 'apple' | 'github';
|
|
16
15
|
|
|
@@ -42,7 +41,7 @@ export async function POST(request: Request) {
|
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
// Handle username/password authentication
|
|
45
|
-
if (provider === '
|
|
44
|
+
if (provider === 'userpass') {
|
|
46
45
|
if (!email || !password) {
|
|
47
46
|
return NextResponse.json(
|
|
48
47
|
{ error: 'Email and password are required' },
|
|
@@ -70,7 +69,7 @@ export async function POST(request: Request) {
|
|
|
70
69
|
// Create or update user in the main Users table
|
|
71
70
|
const user = await upsertUser(
|
|
72
71
|
`password_${authRecord.id}`, // Unique OAuth ID for password users
|
|
73
|
-
'
|
|
72
|
+
'userpass' as any, // Source type
|
|
74
73
|
email.split('@')[0], // Username from email
|
|
75
74
|
email,
|
|
76
75
|
undefined // No avatar for password auth
|
|
@@ -78,14 +77,14 @@ export async function POST(request: Request) {
|
|
|
78
77
|
|
|
79
78
|
// Delete existing sessions for this user
|
|
80
79
|
const existingSessions = await db_query(db,
|
|
81
|
-
"SELECT
|
|
80
|
+
"SELECT id FROM usersession WHERE userid = ?",
|
|
82
81
|
[user.userid]
|
|
83
82
|
);
|
|
84
83
|
|
|
85
84
|
if (existingSessions && existingSessions.length > 0) {
|
|
86
|
-
const sessionIds = existingSessions.map(session => session.
|
|
85
|
+
const sessionIds = existingSessions.map(session => session.id);
|
|
87
86
|
const placeholders = sessionIds.map(() => '?').join(',');
|
|
88
|
-
await db_query(db, `DELETE FROM usersession WHERE
|
|
87
|
+
await db_query(db, `DELETE FROM usersession WHERE id IN (${placeholders})`, sessionIds);
|
|
89
88
|
}
|
|
90
89
|
|
|
91
90
|
// Generate secure session token and ID
|
|
@@ -94,7 +93,7 @@ export async function POST(request: Request) {
|
|
|
94
93
|
|
|
95
94
|
// Create new session with secure token
|
|
96
95
|
await db_query(db,
|
|
97
|
-
"INSERT INTO usersession (
|
|
96
|
+
"INSERT INTO usersession (id, sessiontoken, userid, expirationdate) VALUES (?, ?, ?, ?)",
|
|
98
97
|
[sessionId, sessionToken, user.userid, new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString()]
|
|
99
98
|
);
|
|
100
99
|
|
|
@@ -113,10 +112,10 @@ export async function POST(request: Request) {
|
|
|
113
112
|
// Create a completely clean object to avoid any database result object issues
|
|
114
113
|
const cleanUserData = {
|
|
115
114
|
userid: String(user.userid),
|
|
116
|
-
userName: String(user.
|
|
117
|
-
avatarURL: String(user.
|
|
118
|
-
userLevel: Number(user.
|
|
119
|
-
isAdmin: Number(user.
|
|
115
|
+
userName: String(user.username),
|
|
116
|
+
avatarURL: String(user.avatarurl || ''),
|
|
117
|
+
userLevel: Number(user.userlevel),
|
|
118
|
+
isAdmin: Number(user.userlevel) === 2
|
|
120
119
|
};
|
|
121
120
|
|
|
122
121
|
return NextResponse.json({
|
|
@@ -153,7 +152,7 @@ export async function POST(request: Request) {
|
|
|
153
152
|
}
|
|
154
153
|
|
|
155
154
|
let userData = {
|
|
156
|
-
|
|
155
|
+
id: '',
|
|
157
156
|
name: '',
|
|
158
157
|
email: '',
|
|
159
158
|
avatar_url: '',
|
|
@@ -204,28 +203,28 @@ export async function POST(request: Request) {
|
|
|
204
203
|
switch (validProvider) {
|
|
205
204
|
|
|
206
205
|
case 'google':
|
|
207
|
-
userData.
|
|
206
|
+
userData.id = fetchedUserData.id;
|
|
208
207
|
userData.name = fetchedUserData.name;
|
|
209
208
|
userData.email = fetchedUserData.email;
|
|
210
209
|
userData.avatar_url = fetchedUserData.picture;
|
|
211
210
|
break;
|
|
212
211
|
|
|
213
212
|
case 'facebook':
|
|
214
|
-
userData.
|
|
213
|
+
userData.id = fetchedUserData.id;
|
|
215
214
|
userData.name = fetchedUserData.name;
|
|
216
215
|
userData.email = fetchedUserData.email;
|
|
217
216
|
userData.avatar_url = fetchedUserData.picture?.data?.url;
|
|
218
217
|
break;
|
|
219
218
|
|
|
220
219
|
case 'apple':
|
|
221
|
-
userData.
|
|
220
|
+
userData.id = fetchedUserData.sub;
|
|
222
221
|
userData.name = `${fetchedUserData.given_name || ''} ${fetchedUserData.family_name || ''}`.trim();
|
|
223
222
|
userData.email = fetchedUserData.email;
|
|
224
223
|
// Apple doesn't provide avatar URL
|
|
225
224
|
break;
|
|
226
225
|
|
|
227
226
|
case 'github':
|
|
228
|
-
userData.
|
|
227
|
+
userData.id = fetchedUserData.id?.toString();
|
|
229
228
|
userData.name = fetchedUserData.name || fetchedUserData.login;
|
|
230
229
|
userData.email = fetchedUserData.email;
|
|
231
230
|
userData.avatar_url = fetchedUserData.avatar_url;
|
|
@@ -240,7 +239,7 @@ export async function POST(request: Request) {
|
|
|
240
239
|
|
|
241
240
|
// Create or update user using the new user management system
|
|
242
241
|
const user = await upsertUser(
|
|
243
|
-
userData.
|
|
242
|
+
userData.id,
|
|
244
243
|
validProvider,
|
|
245
244
|
userData.name,
|
|
246
245
|
userData.email,
|
|
@@ -258,14 +257,14 @@ export async function POST(request: Request) {
|
|
|
258
257
|
|
|
259
258
|
// Delete existing sessions for this user
|
|
260
259
|
const existingSessions = await db_query(db,
|
|
261
|
-
"SELECT
|
|
260
|
+
"SELECT id FROM usersession WHERE userid = ?",
|
|
262
261
|
[user.userid]
|
|
263
262
|
);
|
|
264
263
|
|
|
265
264
|
if (existingSessions && existingSessions.length > 0) {
|
|
266
|
-
const sessionIds = existingSessions.map(session => session.
|
|
265
|
+
const sessionIds = existingSessions.map(session => session.id);
|
|
267
266
|
const placeholders = sessionIds.map(() => '?').join(',');
|
|
268
|
-
await db_query(db, `DELETE FROM usersession WHERE
|
|
267
|
+
await db_query(db, `DELETE FROM usersession WHERE id IN (${placeholders})`, sessionIds);
|
|
269
268
|
}
|
|
270
269
|
|
|
271
270
|
// Generate secure session token and ID
|
|
@@ -274,7 +273,7 @@ export async function POST(request: Request) {
|
|
|
274
273
|
|
|
275
274
|
// Create new session with secure token
|
|
276
275
|
await db_query(db,
|
|
277
|
-
"INSERT INTO usersession (
|
|
276
|
+
"INSERT INTO usersession (id, sessiontoken, userid, expirationdate) VALUES (?, ?, ?, ?)",
|
|
278
277
|
[sessionId, sessionToken, user.userid, new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString()]
|
|
279
278
|
);
|
|
280
279
|
|
|
@@ -293,10 +292,10 @@ export async function POST(request: Request) {
|
|
|
293
292
|
// Create a completely clean object to avoid any database result object issues
|
|
294
293
|
const cleanUserData = {
|
|
295
294
|
userid: String(user.userid),
|
|
296
|
-
userName: String(user.
|
|
297
|
-
avatarURL: String(user.
|
|
298
|
-
userLevel: Number(user.
|
|
299
|
-
isAdmin: Number(user.
|
|
295
|
+
userName: String(user.username),
|
|
296
|
+
avatarURL: String(user.avatarurl || ''),
|
|
297
|
+
userLevel: Number(user.userlevel),
|
|
298
|
+
isAdmin: Number(user.userlevel) === 2
|
|
300
299
|
};
|
|
301
300
|
|
|
302
301
|
return NextResponse.json({
|
|
@@ -347,13 +346,13 @@ export async function GET() {
|
|
|
347
346
|
const response = NextResponse.json({
|
|
348
347
|
user: {
|
|
349
348
|
userid: sessionData.user.userid,
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
349
|
+
username: sessionData.user.username,
|
|
350
|
+
avatarurl: sessionData.user.avatarurl,
|
|
351
|
+
email: sessionData.user.email,
|
|
352
|
+
userlevel: sessionData.user.userlevel,
|
|
353
|
+
isadmin: sessionData.user.userlevel === 2,
|
|
354
|
+
source: sessionData.user.source,
|
|
355
|
+
lastlogindate: sessionData.user.lastlogindate
|
|
357
356
|
}
|
|
358
357
|
});
|
|
359
358
|
|
|
@@ -376,13 +375,13 @@ export async function GET() {
|
|
|
376
375
|
return NextResponse.json({
|
|
377
376
|
user: {
|
|
378
377
|
userid: sessionData.user.userid,
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
378
|
+
username: sessionData.user.username,
|
|
379
|
+
avatarurl: sessionData.user.avatarurl,
|
|
380
|
+
email: sessionData.user.email,
|
|
381
|
+
userlevel: sessionData.user.userlevel,
|
|
382
|
+
isadmin: sessionData.user.userlevel === 2,
|
|
383
|
+
source: sessionData.user.source,
|
|
384
|
+
lastlogindate: sessionData.user.lastlogindate
|
|
386
385
|
}
|
|
387
386
|
});
|
|
388
387
|
|
|
@@ -395,15 +394,6 @@ export async function GET() {
|
|
|
395
394
|
}
|
|
396
395
|
|
|
397
396
|
export async function DELETE(request: Request) {
|
|
398
|
-
// Validate CSRF token for logout
|
|
399
|
-
const isValidCsrf = await validateCsrfToken(request);
|
|
400
|
-
if (!isValidCsrf) {
|
|
401
|
-
return NextResponse.json(
|
|
402
|
-
{ error: 'Invalid CSRF token' },
|
|
403
|
-
{ status: 403 }
|
|
404
|
-
);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
397
|
const db = await db_init();
|
|
408
398
|
|
|
409
399
|
try {
|
|
@@ -420,7 +410,7 @@ export async function DELETE(request: Request) {
|
|
|
420
410
|
|
|
421
411
|
// Delete session from database using the actual session token
|
|
422
412
|
await db_query(db,
|
|
423
|
-
"DELETE FROM usersession WHERE
|
|
413
|
+
"DELETE FROM usersession WHERE sessiontoken = ?",
|
|
424
414
|
[sessionToken]
|
|
425
415
|
);
|
|
426
416
|
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { NextResponse } from 'next/server';
|
|
7
|
-
import { validateCsrfToken } from '../../../csrf';
|
|
8
7
|
import { createPasswordAuth } from '../../../db-password-auth';
|
|
9
8
|
import { send_email } from '../../../storage/email';
|
|
10
9
|
|
|
@@ -15,15 +14,6 @@ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
|
15
14
|
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;
|
|
16
15
|
|
|
17
16
|
export async function POST(request: Request) {
|
|
18
|
-
// Validate CSRF token
|
|
19
|
-
const isValidCsrf = await validateCsrfToken(request);
|
|
20
|
-
if (!isValidCsrf) {
|
|
21
|
-
return NextResponse.json(
|
|
22
|
-
{ error: 'Invalid CSRF token' },
|
|
23
|
-
{ status: 403 }
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
17
|
try {
|
|
28
18
|
const { email, password } = await request.json();
|
|
29
19
|
|
|
@@ -62,11 +52,11 @@ export async function POST(request: Request) {
|
|
|
62
52
|
.button {
|
|
63
53
|
display: inline-block;
|
|
64
54
|
padding: 12px 24px;
|
|
65
|
-
background-color: #
|
|
66
|
-
color:
|
|
55
|
+
background-color: #f0f0f0;
|
|
56
|
+
color: #000000;
|
|
67
57
|
text-decoration: none;
|
|
68
|
-
border
|
|
69
|
-
|
|
58
|
+
border: 1px solid #cccccc;
|
|
59
|
+
border-radius: 4px;
|
|
70
60
|
}
|
|
71
61
|
.footer { margin-top: 30px; font-size: 12px; color: #666; }
|
|
72
62
|
</style>
|
|
@@ -79,7 +69,7 @@ export async function POST(request: Request) {
|
|
|
79
69
|
<a href="${verificationUrl}" class="button">Verify Email Address</a>
|
|
80
70
|
</p>
|
|
81
71
|
<p>Or copy and paste this link into your browser:</p>
|
|
82
|
-
<p style="word-break: break-all; color: #
|
|
72
|
+
<p style="word-break: break-all; color: #0066cc;">${verificationUrl}</p>
|
|
83
73
|
<p>This link will expire in 24 hours.</p>
|
|
84
74
|
<div class="footer">
|
|
85
75
|
<p>If you didn't create an account, you can safely ignore this email.</p>
|