Quickstart Frontend Guide

(withand)

One of our goals with PropelAuth is to get you up and running as quickly as possible. You shouldn't have to worry about managing users, passwords, reset flows, 2FA, or anything like that, you should just be able to get information on your users and get back to building your product.

In this guide, we'll set up a simple frontend that displays our users information:

logged in example

Set up Signup, Login, and Account pages

After signing up for PropelAuth, create a project. A project includes everything you need to set up authentication, like UIs, a dashboard for managing your users, transactional emails, and more.

create project

Once we've created a project, we're actually done with this step. PropelAuth hosts UIs for you to get you live as quickly as possible. We do also provide APIs for you to build your own UIs, but we'll cover that later.

You can see the UIs we host for you by clicking the Preview button on the top right of the dashboard.

preview button

If you click login, you'll see the default login page:

default login page
This is what your users will see when they go to log in to your product

Everything about this page is configurable, from the color scheme and logo, to the login options we present to the user (passwords, passwordless, SSO, etc.). We'll worry about configuring it later - instead lets connect our frontend.

Installation

Missing Frameworks
The next section is personalized to your frameworks (but will default to React & FastAPI). Please select the frameworks you use and we'll only show you relevant content.

First, install the @propelauth/react library in your existing React application. If you don't have a React application yet, follow these instructions.

npm install @propelauth/react

Configuration

We now need to tell PropelAuth where our application is running so that it will allow requests from our application. Go to the Frontend Integration section of your PropelAuth dashboard, and enter http://localhost:3000 into the Application URL field:

primary fe location

While we're here, we'll also copy the Auth URL into an .env file, which we'll use in a second:

.env

# Test environment only, in production, we'll use our own domain
REACT_APP_AUTH_URL=https://something.propelauthtest.com

# If you are using Vite:
# VITE_AUTH_URL=https://something.propelauthtest.com

Initialization

At the root of your application, wrap your app in an AuthProvider component.

index.js

import { AuthProvider } from "@propelauth/react";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
    <AuthProvider authUrl={process.env.REACT_APP_AUTH_URL}>
        <YourApp />
    </AuthProvider>,
    document.getElementById("root")
);

The AuthProvider is responsible for fetching the current user's authentication information. If your entire application requires the user to be logged in (for example, for a dashboard), use RequiredAuthProvider instead and you'll never have to check isLoggedIn.

Display user information

We now have everything set up to display our user's information. Lets create a simple component that displays the user's email address:

components/YourApp.jsx

import { withAuthInfo } from '@propelauth/react'

const YourApp = withAuthInfo((props) => {
    // isLoggedIn and user are injected automatically from withAuthInfo
    if (props.isLoggedIn) {
        return <p>You are logged in as {props.user.email}</p>
    } else {
        return <p>You are not logged in</p>
    }
})

export default YourApp

And that's pretty much it. Since we haven't logged in yet, we'll see:

not logged in example

But, speaking of logging in... if a user lands on this page, there's not a lot they can do. Let's update our component to display Signup and Login buttons if the user isn't logged in yet. While we are here, we'll also add Logout and Account buttons if the user is logged in.

components/YourApp.jsx

import { withAuthInfo, useRedirectFunctions, useLogoutFunction } from '@propelauth/react'

const YourApp = withAuthInfo((props) => {
    const logoutFunction = useLogoutFunction()
    const { redirectToLoginPage, redirectToSignupPage, redirectToAccountPage } = useRedirectFunctions()
    // Or if you want to make links instead
    // const { getLoginPageUrl, getSignupPageUrl, getAccountPageUrl } = useHostedPageUrls()

    if (props.isLoggedIn) {
        return (
            <div>
                <p>You are logged in as {props.user.email}</p>
                <button onClick={() => redirectToAccountPage()}>Account</button>
                <button onClick={() => logoutFunction(true)}>Logout</button>
            </div>
        )
    } else {
        return (
            <div>
                <p>You are not logged in</p>
                <button onClick={() => redirectToLoginPage()}>Login</button>
                <button onClick={() => redirectToSignupPage()}>Signup</button>
            </div>
        )
    }
})

export default YourApp

And now we can see our login/signup buttons since we still aren't logged in:

not logged in but with login buttons

Let's click the Signup button and then create an account. Afterwards, we'll be redirected back to our application and we can see our email address, as well as the Logout and Account buttons:

logged in example

Next Steps

Great! We now have a powerful, flexible authentication experience for our users. And for us, we have all the building blocks we need to set up our product.

In our next guide, we'll see how we can protect our backend APIs.