W
Wasp4w ago
cmilazzo

redirect after login

After logging in, I'd like to route to a profile page if the user has not yet finished profile setup. Otherwise, they should go to the dashboard page. How do I fork this routing?
4 Replies
kapa.ai
kapa.ai4w ago
Hi @cmilazzo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To implement custom routing after login based on the user's profile setup status, you can use the useAuth() hook and conditional rendering in your React component. Here's how you can achieve this: 1. First, ensure you have a field in your User entity to track whether the profile setup is complete. For example, you could add an isProfileComplete boolean field:
entity User {=psl
id Int @id @default(autoincrement())
username String? @unique
isProfileComplete Boolean @default(false)
psl=}
entity User {=psl
id Int @id @default(autoincrement())
username String? @unique
isProfileComplete Boolean @default(false)
psl=}
2. In your main page component (e.g., HomePage), use the useAuth() hook to get the user data and check the isProfileComplete status:
import { useAuth } from 'wasp/client/auth'
import { Redirect } from 'react-router-dom'

export function HomePage() {
const { data: user } = useAuth()

if (!user.isProfileComplete) {
return <Redirect to="/profile-setup" />
} else {
return <Redirect to="/dashboard" />
}

// ...
}
import { useAuth } from 'wasp/client/auth'
import { Redirect } from 'react-router-dom'

export function HomePage() {
const { data: user } = useAuth()

if (!user.isProfileComplete) {
return <Redirect to="/profile-setup" />
} else {
return <Redirect to="/dashboard" />
}

// ...
}
This approach is similar to the example provided in the Wasp documentation for social auth (https://wasp-lang.dev/docs/auth/social-auth/overview#3-showing-the-correct-state-on-the-client), but adapted for your specific use case. Note that you'll need to create the appropriate routes for /profile-setup and /dashboard in your Wasp configuration file. Additionally, you might want to consider using the onAuthSucceededRedirectTo option in your Wasp configuration to set a default redirect path after login. However, this won't allow for conditional routing based on the user's profile status, which is why the approach above using useAuth() and conditional rendering is recommended for your specific requirement. Remember to update the isProfileComplete field in your database when the user completes their profile setup, so that subsequent logins will redirect them to the dashboard instead of the profile setup page. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
cmilazzo
cmilazzoOP4w ago
This almost works. @kapa.ai Redirect has been replaced with Navigate in modern react-router-dom versions.
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- You're correct, and I apologize for the outdated information in my previous response. In modern versions of react-router-dom, Redirect has indeed been replaced with Navigate. Thank you for pointing this out. Let's update the example to use Navigate instead:
import { useAuth } from 'wasp/client/auth'
import { Navigate } from 'react-router-dom'

export function HomePage() {
const { data: user } = useAuth()

if (!user.isProfileComplete) {
return <Navigate to="/profile-setup" replace />
} else {
return <Navigate to="/dashboard" replace />
}

// ...
}
import { useAuth } from 'wasp/client/auth'
import { Navigate } from 'react-router-dom'

export function HomePage() {
const { data: user } = useAuth()

if (!user.isProfileComplete) {
return <Navigate to="/profile-setup" replace />
} else {
return <Navigate to="/dashboard" replace />
}

// ...
}
This updated code uses Navigate from react-router-dom, which is the current recommended way to handle programmatic navigation in React Router. It's worth noting that Wasp uses react-router-dom for routing, as evidenced by the imports in the router.tsx file (https://github.com/wasp-lang/wasp/issues/1527) generated by Wasp. However, the Wasp documentation hasn't been updated to reflect this change from Redirect to Navigate in all places. When working with Wasp, it's always a good idea to check the version of react-router-dom being used in your project and refer to the corresponding React Router documentation for the most up-to-date API usage. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
martinsos
martinsos4w ago
@cmilazzotake a look also at https://wasp-lang.dev/docs/auth/auth-hooks#the-onafterlogin-hook, it might be useful for what you are doing here.

Did you find this page helpful?