Because of specific authentication requirements, my app needs to redirect to an external site. What's the best way to do that? And, I also need to pass a "return URL" so that service can send me back where it started. How do I get the current "window.location.href" equivalent that works with SSR? Specifically, I need the full HOST url.
#Need to execute code in browser, can I do SSR? or should I give up?
3 messages · Page 1 of 1 (latest)
I've implemented something similar, so I'll try to help you. Yes you can do SSR, and there are a couple of ways you can about it. Simplest way is to run your server code in the vue lifecycle hook onServerPrefetch
You can return a redirect in this manner :
<script setup>
import { callWithNuxt } from "nuxt/app";
const nuxtApp = useNuxtApp();
onServerPrefetch(async () => {
// run your setup for redirecting (building the query params to the auth service etc.)
const url = "https://yourauthservice.com" + query;
await callWithNuxt(nuxtApp, navigateTo, [url]);
});
<script/>
and then you can receive from your auth service from a "callback" url of sorts. Somthing like "/auth/callback" which completes your auth flow and can redirect to your app perhaps.
Note : You could also use an API route for the callback.
And as for current url, You're almost always going to be sure where your server is going to be running so you could use env variables and runtime config from nuxt for that purpose. Hope that helped