Coder Social home page Coder Social logo

Comments (3)

litewarp avatar litewarp commented on May 21, 2024 9

Just to chime in with an example Login Page I'm using in an app. If a user attempts to hit an authenticated endpoint but is not authenticated, the app will redirect to the login page, but append the original route to the login url, e.g., /login?next=/dashboard?id=1234. So in the login flow, if the user is successful in authenticating, we redirect them back to the page.

/**
 * @/pages/login
 */
import dynamic from 'next/dynamic'
import { NextPage } from 'next'
import { withAuthUser, AuthAction } from 'next-firebase-auth'
import { SimplePageWrapper } from '@/components/layout/PageWrapper'
import FullComponentLoader from '@/components/layout/FullComponentLoader'

const FirebaseAuth = dynamic(() => import('@/components/auth/FirebaseAuth', {
  ssr: false,
})

const Login: NextPage = () => (
  <SimplePageWrapper title="Login">
    <h1>Welcome to My New App!</h1>
    <br />
   <FirebaseAuth />
  </SimplePageWrapper>
)

export default withAuthUser({
  whenAuthed: AuthAction.REDIRECT_TO_APP,
  whenUnauthedBeforeInit: AuthAction.SHOW_LOADER,
  whenUnauthedAfterInit: AuthAction.RENDER,
  LoaderComponent: FullComponentLoader,
  appPageURL: ({ ctx, AuthUser }) => {
    const params =
      typeof window === 'undefined'
        ? new URL(ctx.req.url).searchParams
        : new URLSearchParams(window.location.search)
    const next = params.get('next')
    return next ?? '/dashboard' // return default if no param exists
})(Login)

from next-firebase-auth.

perjonsson avatar perjonsson commented on May 21, 2024

@litewarp That's exactly what I was looking for. If someone else wants to try this out before the stable release, you need the latest alpha to allow appPageURL to accept a function and not just a string.

from next-firebase-auth.

grahaml avatar grahaml commented on May 21, 2024

Ahh the dynamic import for the FirebaseAuth component is a great idea. I will have to try that. I hacked a way to get it not render server side, but this feels much cleaner. Thanks for sharing!

from next-firebase-auth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.