Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

4.2 KB
Raw Blame
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

'use client'

import Image from 'next/image'
import { useSearchParams } from 'next/navigation'
import { Roboto } from 'next/font/google'
import { Alert, Card, Flex, Typography, Spin } from 'antd'
import { cn } from '@/lib/utils/tailwind'
import { useEffect, useState, Suspense } from 'react'

import OidcLogin from './components/OidcLogin'
import DefaultLogin from './components/DefaultLogin'
import BasicLogin from './components/BasicLogin'
import SimpleLogin from './components/SimpleLogin'
import { oauthProviderFactory } from '@/lib/auth/providers/factory'
import { resetMetalakeStore } from '@/lib/store/metalakes'
import { useAppDispatch, useAppSelector } from '@/lib/hooks/useStore'

const fonts = Roboto({ subsets: ['latin'], weight: ['400'], display: 'swap' })

const { Title } = Typography

const LoginContent = () => {
  const searchParams = useSearchParams()
  const inactiveReason = searchParams.get('reason') === 'inactive'
  const maxDurationReason = searchParams.get('reason') === 'max_duration'
  const [providerType, setProviderType] = useState(null)
  const dispatch = useAppDispatch()
  const authType = useAppSelector(state => state.auth.authType)

  useEffect(() => {
    dispatch(resetMetalakeStore())

    if (authType !== 'oauth') {
      return
    }

    const detectProviderType = async () => {
      try {
        const detectedType = await oauthProviderFactory.getProviderType()
        setProviderType(detectedType)
      } catch (error) {
        setProviderType('default')
      }
    }

    detectProviderType()
  }, [authType, dispatch])

  const renderLogin = () => {
    switch (authType) {
      case 'basic':
        return <BasicLogin />

      case 'simple':
        return <SimpleLogin />

      case 'oauth':
        if (providerType === null)
          return (
            <div style={{ display: 'flex', justifyContent: 'center', padding: '2rem' }}>
              <Spin />
            </div>
          )
        if (providerType === 'oidc') return <OidcLogin />

        return <DefaultLogin />

      default:
        return (
          <div style={{ display: 'flex', justifyContent: 'center', padding: '2rem' }}>
            <Spin />
          </div>
        )
    }
  }

  return (
    <Flex justify='center' align='center' style={{ minHeight: 'calc(100vh - 7rem)' }}>
      <Card style={{ width: 480 }} styles={{ body: { padding: 48 } }}>
        <Flex justify='center' align='center' className='mb-8'>
          <Image
            src={`${process.env.NEXT_PUBLIC_BASE_PATH ?? ''}/icons/gravitino.svg`}
            width={24}
            height={24}
            alt='Gravitino Logo'
          />
          <Title level={4} className={cn('!mb-0 ml-2 text-black', fonts.className)}>
            Gravitino
          </Title>
        </Flex>

        {inactiveReason && (
          <Alert
            message='Your session has expired due to inactivity. Please sign in again.'
            type='info'
            showIcon
            closable
            className='mb-6'
          />
        )}

        {maxDurationReason && (
          <Alert
            message='Your session has reached its maximum duration. Please sign in again.'
            type='warning'
            showIcon
            closable
            className='mb-6'
          />
        )}

        {renderLogin()}
      </Card>
    </Flex>
  )
}

const LoginPage = () => {
  return (
    <Suspense>
      <LoginContent />
    </Suspense>
  )
}

export default LoginPage