NuxtN
Nuxt12mo ago
8 replies
Adnan Erlansyah

How to to get the current url in NuxtJS 3

How to to get the current url from server-side in NuxtJS 3?

import { storeToRefs } from "pinia";
import authApi from "~/composables/api/authApi";

const { apiLoginGoogle } = authApi();

export default defineOAuthGoogleEventHandler({
    config: {
        authorizationURL: 'https://accounts.google.com/o/oauth2/v2/auth',
        tokenURL: 'https://oauth2.googleapis.com/token',
        userURL: 'https://www.googleapis.com/oauth2/v3/userinfo',
        authorizationParams: {},
    },
    // @ts-ignore
    async onSuccess(event, { user, tokens }) {
        // Check if the data user is matched from the database
        // If it's matched then then make user login else redirect user to register page
        const payload = {
            code: tokens?.access_token,
            redirectUri: 
        }
        const response = await apiLoginGoogle(payload);
        console.log(response)
        // Construct a script to close the popup and notify the parent
        const closeScript = `
            <script>
                if (window.opener) {
                    window.opener.postMessage({ success: true, user: ${JSON.stringify(user)}, tokens: ${JSON.stringify(tokens)} },  '*');
                    window.close();
                }
            </script>
        `;

        // Return a response that runs the script
        // return sendRedirect(event, '/auth/register')
        return send(event, closeScript, 'text/html');
    },
    onError(event, error) {
        console.error('Google OAuth error:', error);
        // You can choose to send an error response instead of redirecting
        return sendRedirect(event, '/')
    },
})
Was this page helpful?