Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
project_ss23_gameunity
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Yusuf Akgül
project_ss23_gameunity
Commits
7f3522c9
Commit
7f3522c9
authored
1 year ago
by
Yusuf Akgül
Browse files
Options
Downloads
Patches
Plain Diff
implemented email already in use and username generation
parent
fe53d5fd
No related branches found
No related tags found
1 merge request
!19
Feat.auth fixes
Pipeline
#36288
failed
1 year ago
Stage: build
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/api/signup/route.ts
+42
-23
42 additions, 23 deletions
app/api/signup/route.ts
components/user-auth-form.tsx
+6
-1
6 additions, 1 deletion
components/user-auth-form.tsx
with
48 additions
and
24 deletions
app/api/signup/route.ts
+
42
−
23
View file @
7f3522c9
...
...
@@ -3,17 +3,43 @@ import { hash } from 'bcrypt'
import
{
NextResponse
}
from
'
next/server
'
export
async
function
POST
(
req
:
Request
)
{
console
.
log
(
"
aaaa
"
,
req
)
try
{
const
{
username
,
email
,
password
}
=
await
req
.
json
()
const
hashed
=
await
hash
(
password
,
12
)
let
usernameCheck
=
username
.
toLowerCase
()
const
emailCheck
=
email
.
toLowerCase
()
const
existingUser
=
await
db
.
user
.
findUnique
({
where
:
{
email
:
emailCheck
}
})
if
(
existingUser
)
{
throw
new
Error
(
'
email already exists
'
)
}
let
isUnique
=
false
;
while
(
!
isUnique
)
{
const
existingUserName
=
await
db
.
user
.
findUnique
({
where
:
{
username
:
usernameCheck
}
})
if
(
existingUserName
)
{
usernameCheck
=
`
${
username
}${
Math
.
floor
(
Math
.
random
()
*
1000
)}
`
}
else
{
isUnique
=
true
;
}
}
const
user
=
await
db
.
user
.
create
({
data
:
{
name
:
username
,
username
:
username
.
toLowerCase
()
,
email
:
email
.
toLowerCase
()
,
username
:
username
Check
,
email
:
email
Check
,
password
:
hashed
}
})
...
...
@@ -22,24 +48,17 @@ export async function POST(req: Request) {
usernameOrEmail
:
user
.
email
})
}
catch
(
err
:
any
)
{
return
new
NextResponse
(
JSON
.
stringify
({
error
:
err
.
message
}),
{
status
:
500
}
if
(
err
.
message
===
'
email already exists
'
)
{
return
new
NextResponse
(
JSON
.
stringify
({
error
:
err
.
message
}),
{
status
:
422
}
)
}
return
new
NextResponse
(
JSON
.
stringify
({
error
:
err
.
message
}),
{
status
:
500
}
)
}
}
// let isUnique = false;
// while (!isUnique) {
// const existingUserName = await db.user.findUnique({
// where: {
// username: credentials.username
// }
// })
// if (existingUserName) {
// credentials.username = `${credentials.username}${Math.floor(Math.random() * 1000)}`
// } else {
// isUnique = true;
// }
// }
\ No newline at end of file
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
components/user-auth-form.tsx
+
6
−
1
View file @
7f3522c9
...
...
@@ -25,6 +25,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
const
{
register
,
handleSubmit
,
setError
,
formState
:
{
errors
},
}
=
useForm
<
FormData
>
({
resolver
:
zodResolver
(
userAuthSchema
),
...
...
@@ -49,8 +50,12 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
})
if
(
!
res
.
ok
)
{
if
(
res
.
status
===
422
)
{
setError
(
'
email
'
,
{
type
:
'
manual
'
,
message
:
'
This email is already in use. Please choose another one.
'
});
}
setIsLoading
(
false
)
toast
({
return
toast
({
variant
:
"
destructive
"
,
title
:
"
Uh oh! Something went wrong.
"
,
description
:
"
Your sign up request failed. Please try again.
"
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment