register logic
This commit is contained in:
@@ -45,35 +45,56 @@ interface GoogleToken extends JWT {
|
||||
const authOptions: NextAuthOptions = {
|
||||
providers: [
|
||||
//google login flow
|
||||
GoogleProvider({
|
||||
clientId: process.env.GOOGLE_CLIENT_ID!,
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
||||
}),
|
||||
// GoogleProvider({
|
||||
// clientId: process.env.GOOGLE_CLIENT_ID!,
|
||||
// clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
||||
// }),
|
||||
|
||||
//регистрация клиента
|
||||
CredentialsProvider({
|
||||
id: 'register-credentials',
|
||||
name: 'Register',
|
||||
credentials: {
|
||||
name: { label: 'Name', type: 'text' },
|
||||
surname: { label: 'Surname', type: 'text' },
|
||||
email: { label: 'Email', type: 'email' },
|
||||
password: { label: 'Password', type: 'password' },
|
||||
name: { label: 'Name', type: 'text' },
|
||||
phone_number: { label: 'Phone Number', type: 'tel' },
|
||||
privacy_accepted: { label: 'Privacy Accepted', type: 'boolean' },
|
||||
},
|
||||
async authorize(credentials) {
|
||||
try {
|
||||
if (
|
||||
!credentials?.email ||
|
||||
!credentials?.password ||
|
||||
!credentials?.name ||
|
||||
!credentials?.phone_number ||
|
||||
!credentials?.privacy_accepted ||
|
||||
!credentials?.surname
|
||||
) {
|
||||
throw new Error('Все поля обязательны для заполнения')
|
||||
}
|
||||
|
||||
// console.log('Registration data:', {
|
||||
// email: credentials.email,
|
||||
// name: credentials.name,
|
||||
// surname: credentials.surname,
|
||||
// phone_number: credentials.phone_number,
|
||||
// privacy_accepted: credentials.privacy_accepted,
|
||||
// })
|
||||
|
||||
const res = await fetch(
|
||||
`${process.env.BACKEND_URL}/register/clients/`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
email: credentials?.email,
|
||||
password: credentials?.password,
|
||||
name: credentials?.name,
|
||||
phone_number: credentials?.phone_number,
|
||||
privacy_accepted: credentials?.privacy_accepted === 'true',
|
||||
email: credentials.email,
|
||||
password: credentials.password,
|
||||
name: credentials.name,
|
||||
surname: credentials.surname,
|
||||
phone_number: credentials.phone_number,
|
||||
privacy_accepted: credentials.privacy_accepted === 'true',
|
||||
}),
|
||||
}
|
||||
)
|
||||
@@ -81,21 +102,25 @@ const authOptions: NextAuthOptions = {
|
||||
const data = await res.json()
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
data.error || data.details?.toString() || 'Registration failed'
|
||||
)
|
||||
console.error('Registration error response:', data)
|
||||
const errorMessage =
|
||||
typeof data === 'object'
|
||||
? data.error || Object.values(data).flat().join(', ')
|
||||
: 'Registration failed'
|
||||
throw new Error(errorMessage)
|
||||
}
|
||||
|
||||
return {
|
||||
id: data.user.id.toString(),
|
||||
email: data.user.email,
|
||||
name: data.user.firstName,
|
||||
name: data.user.name,
|
||||
surname: data.user.surname,
|
||||
accessToken: data.access,
|
||||
refreshToken: data.refresh,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Registration error:', error)
|
||||
return null
|
||||
throw error
|
||||
}
|
||||
},
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user