Next.js Pages Router - Client Component Hooks - PropelAuth Docs
Client Component Hooks
Hooks in this section are only usable in Next.js client side components
useUser
useUser is a React hook that returns information about the current user. See User for more information.
Return Value
Name
loadingType boolean Description Whether or not the user's information is loading. This will only be true on the first render.Name
isLoggedInType boolean Description Whether or not the user is logged in.Name
userType User | undefined Description The User object. This will be undefined if the user is not logged in.Name
accessTokenType string | undefined Description An access token for the user that can be used to make requests to your backend. This can be useful for making requests to other backends outside of Next.js.Name
setActiveOrgType (orgId: string) => UserFromToken Description A function to set the user's Active Org. Accepts an org ID as an argument.
Example
"use client";
import {useUser} from "@propelauth/nextjs/client";
const WelcomeMessage = () => {
const {loading, user} = useUser()
if (loading) {
return <div>Loading...</div>
} else if (user) {
return <div>Hello {user.email}!</div>
} else {
return <div>Please log in to be welcomed</div>
}
}
useLogoutFunction
A React hook that returns a function that logs the user out.
LogoutButton.tsx
"use client"
import {useLogoutFunction} from "@propelauth/nextjs/client";
export default function LogoutButton() {
const logoutFn = useLogoutFunction()
return <button onClick={logoutFn}>Logout</button>
}
useRedirectFunctions
useRedirectFunctions is a React hook that returns a collection of functions which redirect the user to useful locations. Each function takes in an optional options argument which can control features like where the user returns to after logging in.
Returned Functions
Name
redirectToLoginPageType (options?: RedirectToLoginOptions) => void Description Redirects the user to the login page. Additional options:- postLoginRedirectPath - The URL to redirect the user to after they log in. Set the default in your Dashboard.
- userSignupQueryParameters - Query parameters to be used with user insights.
Name
redirectToSignupPageType (options?: RedirectToSignupOptions) => void Description Redirects the user to the signup page. Additional options:- postSignupRedirectPath - The URL to redirect the user to after they sign up. Set the default in your Dashboard.
- userSignupQueryParameters - Query parameters to be used with user insights.
Name
redirectToAccountPageType () => void Description Redirects the user to the account page. Additional options:- redirectBackToUrl - The URL to redirect the user back to if they were to click back in the account pages.
Name
redirectToOrgPageType (orgId?: string) => void Description Redirects the user to the organization page for the given org ID. If an org ID is not provided, one is chosen automatically.- redirectBackToUrl - The URL to redirect the user back to if they were to click back in the account pages.
Name
redirectToOrgSettingsPageType (orgId?: string) => void Description Redirects the user to the organization settings page for the given org ID. If an org ID is not provided, one is chosen automatically.- redirectBackToUrl - The URL to redirect the user back to if they were to click back in the account pages.
Name
redirectToCreateOrgPageType () => void Description Redirects the user to the create organization page. Additional options:- redirectBackToUrl - The URL to redirect the user back to if they were to click back in the account pages.
Name
redirectToSetupSAMLPageType (orgId?: string) => void Description Redirects the user to the organization SAML setup page for the given org ID. If an org ID is not provided, one is chosen automatically.- redirectBackToUrl - The URL to redirect the user back to if they were to click back in the account pages.
Name
redirectToOrgApiKeysPageType (orgId?: string) => void Description Redirects the user to the organization API Keys page for the given org ID. If an org ID is not provided, one is chosen automatically.- redirectBackToUrl - The URL to redirect the user back to if they were to click back in the account pages.
Signature
"use client";
import {useRedirectFunctions} from "@propelauth/nextjs/client"
const {
redirectToLoginPage,
redirectToSignupPage,
redirectToAccountPage,
redirectToOrgPage,
redirectToCreateOrgPage,
} = useRedirectFunctions()
redirectToLoginPage({
postLoginRedirectPath: window.location.href,
})
useHostedPageUrls
useHostedPageUrls is a React hook that returns a collection of functions which get URLs of useful locations. Each function takes in an optional options argument which can control features like where the user returns to after logging in.
Returned Functions
Name
getLoginPageUrlType (options?: RedirectToLoginOptions) => string Description Gets the URL of the login page. Additional options:- postLoginRedirectUrl - The URL to redirect the user to after they log in. Set the default in your Dashboard.
Name
getSignupPageUrlType (options?: RedirectToSignupOptions) => string Description Gets the URL of the signup page. Additional options:- postSignupRedirectUrl - The URL to redirect the user to after they sign up. Set the default in your Dashboard.
Name
getAccountPageUrlType () => string Description Gets the URL of the account page. Additional options:- redirectBackToUrl - The URL to redirect the user back to if they were to click back in the account pages.
Name
getOrgPageUrlType (orgId?: string) => string Description Gets the URL of the organization page for the given org ID. If an org ID is not provided, one is chosen automatically.- redirectBackToUrl - The URL to redirect the user back to if they were to click back in the account pages.
Name
getOrgSettingsPageUrlType (orgId?: string) => string Description Gets the URL of the organization's settings page for the given org ID. If an org ID is not provided, one is chosen automatically.- redirectBackToUrl - The URL to redirect the user back to if they were to click back in the account pages.
Name
getCreateOrgPageUrlType () => string Description Gets the URL of the create organization page. Additional options:- redirectBackToUrl - The URL to redirect the user back to if they were to click back in the account pages.
Name
getSetupSAMLPageUrlType (orgId?: string) => string Description Gets the URL of the organization's SAML setup page for the given org ID. If an org ID is not provided, one is chosen automatically.- redirectBackToUrl - The URL to redirect the user back to if they were to click back in the account pages.
Signature
"use client";
import {useHostedPageUrls} from "@propelauth/nextjs/client"
const {
getLoginPageUrl,
getSignupPageUrl,
getAccountPageUrl,
getOrgPageUrl,
getCreateOrgPageUrl,
} = useHostedPageUrls()
getLoginPageUrl({
postLoginRedirectUrl: window.location.href,
})
useRefreshAuth
A React hook that returns a function that refreshes the user's information. This is unnecessary in most cases, as the AuthProvider will automatically refresh the user's information periodically and on key events, but it can be useful in some cases. It returns a user object, if the user is logged in. Otherwise, it returns undefined.
Signature
"use client";
import { useRefreshAuth } from '@propelauth/nextjs/client'
const refreshAuth = useRefreshAuth()
const user = await refreshAuth()
if (user) {
console.log("You are logged in as", user.email)
} else {
console.log("You are not logged in")
}