Files
nuxtfiber/app/composables/useRegister.ts
Beyhan Oğur 7b2b27a42c first commit
2026-04-26 22:18:17 +03:00

117 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Swal from 'sweetalert2'
import type { RegisterResponse, RegisterRequestWithTurnstile } from '~~/lib/types/auth'
import { registerSchema, getFirstZodError, getFieldErrors } from '~~/lib/validations/auth'
export function useRegister () {
const { signIn } = useAuth()
const route = useRoute()
const email = ref('')
const first_name = ref('')
const last_name = ref('')
const username = ref('')
const password = ref('')
const password_confirm = ref('')
const turnstileToken = ref<string | null>(null)
const turnstileRef = ref<{ reset: () => void } | null>(null)
const fieldError = ref<Record<string, string>>({})
const success = ref('')
const loading = ref(false)
const resendLoading = ref(false)
const resendSent = ref(false)
const callbackUrl = computed(() => (route.query.callbackUrl as string) || '/')
async function onSubmit () {
fieldError.value = {}
const parsed = registerSchema.safeParse({
email: email.value,
first_name: first_name.value,
last_name: last_name.value,
username: username.value,
password: password.value,
password_confirm: password_confirm.value,
})
if (!parsed.success) {
fieldError.value = getFieldErrors(parsed.error)
await Swal.fire({ icon: 'error', title: 'Doğrulama hatası', text: getFirstZodError(parsed.error) })
return
}
if (!turnstileToken.value) {
await Swal.fire({ icon: 'warning', title: 'Güvenlik doğrulaması', text: 'Lütfen güvenlik kutusunu işaretleyin.' })
return
}
loading.value = true
try {
const body: RegisterRequestWithTurnstile = {
email: email.value,
first_name: first_name.value,
last_name: last_name.value,
username: username.value,
password: password.value,
turnstile_token: turnstileToken.value,
}
const data = await $fetch<RegisterResponse>('/api/auth/register', { method: 'POST', body })
success.value = data.message
await Swal.fire({ icon: 'success', title: 'Kayıt başarılı', text: data.message })
} catch (e: unknown) {
const err = e as { data?: { message?: string; detail?: string | string[] }; message?: string }
const msg = err?.data?.message ?? err?.data?.detail ?? err?.message ?? 'Kayıt yapılamadı.'
const text = Array.isArray(msg) ? msg.join(' ') : String(msg)
await Swal.fire({ icon: 'error', title: 'Kayıt hatası', text })
turnstileRef.value?.reset()
turnstileToken.value = null
} finally {
loading.value = false
}
}
async function resendVerification () {
resendLoading.value = true
try {
await $fetch<{ message: string }>('/api/auth/resend-verification', {
method: 'POST',
body: { email: email.value },
})
resendSent.value = true
await Swal.fire({ icon: 'success', text: 'Doğrulama e-postası gönderildi.', timer: 2000, showConfirmButton: false })
} catch {
await Swal.fire({ icon: 'error', text: 'E-posta gönderilemedi.' })
} finally {
resendLoading.value = false
}
}
async function signInWith (provider: 'github' | 'google') {
loading.value = true
try {
await signIn(provider, { callbackUrl: callbackUrl.value })
} catch (e: unknown) {
const msg = e instanceof Error ? e.message : 'Giriş yapılamadı.'
await Swal.fire({ icon: 'error', title: 'Hata', text: msg })
} finally {
loading.value = false
}
}
return {
email,
first_name,
last_name,
username,
password,
password_confirm,
turnstileToken,
turnstileRef,
fieldError,
success,
loading,
resendLoading,
resendSent,
callbackUrl,
onSubmit,
resendVerification,
signInWith,
}
}