'use client';

import { signIn, useSession } from 'next-auth/react';
import { useEffect } from 'react';
import { useSearchParams } from 'next/navigation';

export default function OauthLogin() {
  const { status } = useSession();
  const searchParams = useSearchParams();

  useEffect(() => {
    if (!searchParams) {
      return;
    }

    if (status === 'unauthenticated') {
      signIn('oauth', {
        callbackUrl: searchParams.get('next') ?? '/'
      });
    }
  }, [status, searchParams]);

  return null;
}
