Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
何 广一
chatroom
Commits
fe1da4d0
Commit
fe1da4d0
authored
Aug 26, 2025
by
何 广一
Browse files
fix login & register page
parent
496f7668
Changes
6
Show whitespace changes
Inline
Side-by-side
chat-room/prisma/dev.db
View file @
fe1da4d0
No preview for this file type
chat-room/src/app/api/message/add/route.tsx
View file @
fe1da4d0
...
...
@@ -19,6 +19,9 @@ export async function POST(req: NextRequest): Promise<NextResponse<Ty.BackendRes
if
(
'
code
'
in
cookieData
)
return
NextResponse
.
json
(
cookieData
);
const
{
roomId
,
content
,
sender
}
=
await
req
.
json
()
as
Ty
.
MessageAddArgs
;
const
checkResult
=
await
Db
.
CheckUser
(
sender
,
cookieData
.
userToken
);
if
(
!
checkResult
)
return
Db
.
IncorrectUserResponse
;
const
response
=
await
Db
.
AddMessage
({
roomId
,
content
,
sender
});
return
NextResponse
.
json
(
response
);
}
chat-room/src/app/api/room-api.tsx
View file @
fe1da4d0
...
...
@@ -417,3 +417,17 @@ export async function VerifyCookie(cookie : RequestCookies) : Promise<Ty.Backend
};
return
cookieData
;
}
export
async
function
CheckUser
(
name
:
string
,
token
:
string
)
:
Promise
<
boolean
>
{
const
user
=
await
prisma
.
user
.
findUnique
({
where
:
{
name
},
select
:
{
userToken
:
true
}
});
if
(
!
user
)
return
false
;
return
user
.
userToken
===
token
;
}
export
const
IncorrectUserResponse
:
NextResponse
<
Ty
.
BackendResponse
<
null
>>
=
NextResponse
.
json
({
code
:
105
,
message
:
'
用户名与登录令牌不匹配
'
,
data
:
null
});
\ No newline at end of file
chat-room/src/app/api/room/add/route.tsx
View file @
fe1da4d0
...
...
@@ -16,7 +16,14 @@ interface RoomAddRes {
}
*/
export
async
function
POST
(
req
:
NextRequest
)
{
const
cookieData
=
await
Db
.
VerifyCookie
(
req
.
cookies
);
if
(
'
code
'
in
cookieData
)
return
NextResponse
.
json
(
cookieData
);
const
{
user
,
roomName
}:
Ty
.
RoomAddArgs
=
await
req
.
json
();
const
checkResult
=
await
Db
.
CheckUser
(
user
,
cookieData
.
userToken
);
if
(
!
checkResult
)
return
Db
.
IncorrectUserResponse
;
const
response
=
await
Db
.
AddRoom
({
user
,
roomName
});
return
NextResponse
.
json
(
response
);
}
\ No newline at end of file
chat-room/src/app/api/room/delete/route.tsx
View file @
fe1da4d0
...
...
@@ -19,6 +19,9 @@ export async function POST(req: NextRequest): Promise<NextResponse<Ty.BackendRes
if
(
'
code
'
in
cookieData
)
return
NextResponse
.
json
(
cookieData
);
const
{
user
,
roomId
}
=
await
req
.
json
()
as
Ty
.
RoomDeleteArgs
;
const
checkResult
=
await
Db
.
CheckUser
(
user
,
cookieData
.
userToken
);
if
(
!
checkResult
)
return
Db
.
IncorrectUserResponse
;
const
response
=
await
Db
.
DeleteRoom
({
user
,
roomId
},
cookieData
.
userToken
);
return
NextResponse
.
json
(
response
);
}
\ No newline at end of file
chat-room/src/app/page.tsx
View file @
fe1da4d0
...
...
@@ -18,6 +18,7 @@ function Home_Local(){
const
initSession
=
async
()
=>
{
//url : /api/auth/startSession
setSession
(
null
);
setPageError
(
null
);
setIsErrorInstant
(
false
);
try
{
...
...
@@ -43,7 +44,7 @@ function Home_Local(){
}
useEffect
(()
=>
{
initSession
();
},
[]);
},
[
isLogin
]);
const
updatePassword
=
(
value
:
string
,
confirm
:
string
|
null
)
=>
{
setPassword
(
value
);
...
...
@@ -106,14 +107,15 @@ function Home_Local(){
});
if
(
!
res
.
ok
)
{
throw
new
Error
(
'
登录失败,
错误码:
'
+
res
.
status
);
throw
new
Error
(
'
错误码:
'
+
res
.
status
);
}
const
data
:
Ty
.
BackendResponse
<
Auth
.
LoginResult
>
=
await
res
.
json
();
if
(
data
.
code
!==
0
)
{
throw
new
Error
(
'
登录失败,
错误信息:
'
+
data
.
message
);
throw
new
Error
(
'
错误信息:
'
+
data
.
message
);
}
setPageError
(
'
登录成功,正在跳转……
'
)
router
.
push
(
`/chat?username=
${
encodeURIComponent
(
username
)}
`
);
}
else
{
//URL : /api/auth/register
...
...
@@ -126,19 +128,21 @@ function Home_Local(){
});
if
(
!
res
.
ok
)
{
throw
new
Error
(
'
注册失败,
错误码:
'
+
res
.
status
);
throw
new
Error
(
'
错误码:
'
+
res
.
status
);
}
const
data
:
Ty
.
BackendResponse
<
Auth
.
RegisterResult
>
=
await
res
.
json
();
if
(
data
.
code
!==
0
)
{
throw
new
Error
(
'
注册失败,
错误信息:
'
+
data
.
message
);
throw
new
Error
(
'
错误信息:
'
+
data
.
message
);
}
setPageError
(
'
注册成功,正在刷新页面……
'
)
router
.
refresh
();
window
.
location
.
reload
();
}
}
catch
(
error
)
{
console
.
error
(
error
);
setPageError
((
asLogin
?
'
登录错误
:
'
:
'
注册错误
:
'
)
+
error
.
message
);
setPageError
((
asLogin
?
'
登录错误
!
'
:
'
注册错误
!
'
)
+
error
.
message
);
setIsErrorInstant
(
false
);
}
finally
{
setPending
(
false
);
...
...
@@ -236,7 +240,6 @@ function Home_External() {
}
const
HomeFn
=
useLocalBackend
?
Home_Local
:
Home_External
;
export
default
function
Home
()
{
return
HomeFn
();
}
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment