2024-08-19 15:22:57 +02:00
|
|
|
<template>
|
2025-01-27 18:24:27 +01:00
|
|
|
<div class="flex flex-grow mt-6 mb-10">
|
|
|
|
|
<div class="w-full md:w-2/3 md:mx-auto md:max-w-md px-4">
|
|
|
|
|
<div
|
|
|
|
|
v-if="loading"
|
|
|
|
|
class="m-10"
|
|
|
|
|
>
|
|
|
|
|
<h3 class="my-6 text-center">
|
|
|
|
|
Please wait...
|
|
|
|
|
</h3>
|
|
|
|
|
<Loader class="h-6 w-6 mx-auto m-10" />
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
v-else
|
|
|
|
|
class="m-6 flex flex-col items-center space-y-4"
|
|
|
|
|
>
|
|
|
|
|
<p class="text-center">
|
2025-03-25 10:41:11 +01:00
|
|
|
Unable to sign in at the moment.
|
2025-01-27 18:24:27 +01:00
|
|
|
</p>
|
|
|
|
|
<v-button
|
|
|
|
|
:to="{ name: 'login' }"
|
2024-08-28 15:20:39 +02:00
|
|
|
>
|
2025-01-27 18:24:27 +01:00
|
|
|
Back to login
|
|
|
|
|
</v-button>
|
2024-08-28 15:20:39 +02:00
|
|
|
</div>
|
2024-08-19 15:22:57 +02:00
|
|
|
</div>
|
2025-01-27 18:24:27 +01:00
|
|
|
</div>
|
2024-08-19 15:22:57 +02:00
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2025-01-27 18:24:27 +01:00
|
|
|
import { useNuxtApp } from "nuxt/app"
|
2025-03-25 10:41:11 +01:00
|
|
|
import { WindowMessageTypes } from "~/composables/useWindowMessage"
|
2024-09-18 18:50:04 +02:00
|
|
|
|
2025-01-27 18:24:27 +01:00
|
|
|
const { $utm } = useNuxtApp()
|
2024-08-19 15:22:57 +02:00
|
|
|
const router = useRouter()
|
|
|
|
|
const route = useRoute()
|
2024-08-28 15:20:39 +02:00
|
|
|
const loading = ref(true)
|
2024-08-19 15:22:57 +02:00
|
|
|
|
|
|
|
|
definePageMeta({
|
2024-08-28 15:20:39 +02:00
|
|
|
alias: '/oauth/:provider/callback'
|
2024-08-19 15:22:57 +02:00
|
|
|
})
|
|
|
|
|
|
2025-03-25 10:41:11 +01:00
|
|
|
const handleCallback = async () => {
|
|
|
|
|
const auth = useAuth()
|
|
|
|
|
const provider = route.params.provider
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const { isNewUser } = await auth.handleSocialCallback(
|
|
|
|
|
provider,
|
|
|
|
|
route.query.code,
|
|
|
|
|
$utm.value
|
|
|
|
|
)
|
2024-08-19 15:22:57 +02:00
|
|
|
|
2025-03-25 10:41:11 +01:00
|
|
|
if (!isNewUser) {
|
|
|
|
|
// Handle existing user login
|
|
|
|
|
if (window.opener) {
|
|
|
|
|
try {
|
|
|
|
|
// Use the WindowMessage composable for more reliable communication
|
|
|
|
|
const windowMessage = useWindowMessage(WindowMessageTypes.LOGIN_COMPLETE)
|
|
|
|
|
|
|
|
|
|
// Send the login-complete message and wait for acknowledgment
|
|
|
|
|
await windowMessage.send(window.opener, {
|
|
|
|
|
waitForAcknowledgment: true,
|
|
|
|
|
timeout: 500
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Now we can safely close the window
|
|
|
|
|
window.close()
|
|
|
|
|
|
|
|
|
|
// If window doesn't close (some browsers prevent it), show a message
|
|
|
|
|
loading.value = false
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error in social callback:", err)
|
|
|
|
|
loading.value = false
|
2024-08-19 15:22:57 +02:00
|
|
|
}
|
2025-03-25 10:41:11 +01:00
|
|
|
} else {
|
|
|
|
|
// No opener, redirect to home
|
|
|
|
|
router.push({ name: "home" })
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Handle new user registration
|
|
|
|
|
router.push({ name: "forms-create" })
|
|
|
|
|
useAlert().success("Success! You're now registered with your Google account! Welcome to OpnForm.")
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Social login error:", error)
|
|
|
|
|
useAlert().error(error.response?._data?.message || "Authentication failed")
|
|
|
|
|
loading.value = false
|
|
|
|
|
}
|
2024-08-19 15:22:57 +02:00
|
|
|
}
|
2025-03-25 10:41:11 +01:00
|
|
|
|
2024-08-19 15:22:57 +02:00
|
|
|
onMounted(() => {
|
2025-03-25 10:41:11 +01:00
|
|
|
// Set a timeout to ensure we don't get stuck in loading state
|
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
|
if (loading.value) {
|
|
|
|
|
loading.value = false
|
|
|
|
|
console.error("Social login timed out")
|
|
|
|
|
}
|
|
|
|
|
}, 10000) // 10 second timeout
|
|
|
|
|
|
|
|
|
|
handleCallback().finally(() => {
|
|
|
|
|
clearTimeout(timeoutId)
|
|
|
|
|
})
|
2024-08-19 15:22:57 +02:00
|
|
|
})
|
|
|
|
|
</script>
|