desh
desh

Reputation: 689

Laravel Inertia pops up iframe instead of redirecting

I am using laravel passport's Authorization Code Grant.

  1. Client Application exists in http://localhost:3005 (Next JS App)
  2. Callback URL exists in http://localhost:3005/auth/callback
  3. Laravel Application exists in http://localhost:5001 (Laravel App)

After prompting user to login , Inertia pops up a Iframe Modal instead of redirecting completely to http://localhost:3005/auth/callback

Attached is the screenshot

https://prnt.sc/9SWiLgCepxPy

This is my code for Callback which exists in my client application. http://localhost:3005/auth/callback

import React, { useEffect } from "react";
import { useRouter } from "next/router";
import axios from "axios";

const getToken = async (code) => {
  try {
    const response = await axios.post(
      `${process.env.NEXT_PUBLIC_OAUTH_AUTH_SERVER}/oauth/token`,
      {
        grant_type: "authorization_code",
        client_id: process.env.NEXT_PUBLIC_OAUTH_CLIENT_ID,
        client_secret: process.env.NEXT_PUBLIC_OAUTH_CLIENT_SECRET,
        redirect_uri: process.env.NEXT_PUBLIC_OAUTH_CLIENT_REDIRECT,
        code: code,
      }
    );
    return response;
  } catch (error) {
    return error;
  }
};
const callback = () => {
  const router = useRouter();
  const { code } = router.query;
  useEffect(() => {
    if (code) {
      getToken(code)
        .then((response) => {
          console.log(response);
        })
        .catch((error) => {
          console.log(error);
        });
    }
  }, [code]);

  return <div>Logging you in ....</div>;
};

export default callback;

This is my Login Controller, I am using Laravel Breeze's Login Route. app\Http\Auth\AuthenticatedSessionController

public function store(LoginRequest $request): RedirectResponse
{
    $request->authenticate();

    $request->session()->regenerate();
    $intendedUrl = session('url.intended');
    if ($intendedUrl) {
        return redirect($intendedUrl);
    }
    return redirect()->intended(RouteServiceProvider::HOME);
}

Please help.

Thank you.

Upvotes: 2

Views: 1119

Answers (1)

Rodrigo Klim
Rodrigo Klim

Reputation: 126

This worked for me, saw it on: Laravel Passport login not redirecting properly with Jetstream Inertia

in file AuthenticatedSessionController.php

/**
 * Handle an incoming authentication request.
 *
 * @param  \App\Http\Requests\Auth\LoginRequest  $request
 * @return \Illuminate\Http\RedirectResponse
 */
public function store(LoginRequest $request)
{
    $request->authenticate();

    $request->session()->regenerate();

    if ($request->session()->has('url.intended')) {
        return Inertia::location(session('url.intended'));
    }

    return redirect()->intended(RouteServiceProvider::HOME);
}

Upvotes: 1

Related Questions