Back

error

  • 0
  • Databases
  • Auth
  • Web
Light Yagami
29 Mar, 2024, 11:33

when i signup user gets created to auth and user added as document in user collection (saved to db) but it is not redirected to / (home) route

TypeScript
async function handleSignup(values: z.infer<typeof formSchema>) {
    try {
      
    
    const newUser =await createUserAccount(values);
    if(newUser){
      return toast({
        title: 'Sign up failed! Please try again.'
      })
    }
    const session = await signInAccount({
      email:values.email,
    password:values.password
    })
    if(!session) {
       toast({title:"sign in failed, Please try again."})
       navigate("/sign-in");
        
       return;

    }
    const isLoggedIn=await checkAuthUser();
    if(isLoggedIn){
      form.reset()
      navigate('/')
    }else{
       toast({title:"Sign up failed! Please try again"})
       return;
    }
  } catch (error) {
      console.log(error);
      
  }
my handleSignup function
TL;DR
Developers are discussing an error where the user is not redirected to the homepage after signing up. Solution involves checking if the user is signed in before navigating.
D5
29 Mar, 2024, 11:41

I think the issue is that you're not routing after createUserAccount?

Light Yagami
29 Mar, 2024, 11:42

but i have to navigate to / only when it is signedin

D5
29 Mar, 2024, 11:43

Ok. Let me do a rewrite

Light Yagami
29 Mar, 2024, 11:43

i followed a tutorial and did almost exactly as he did but still getting messed up

D5
29 Mar, 2024, 11:44

What's exactly createUserAccount(values);?

D5
29 Mar, 2024, 11:45

This is JS, right?

Light Yagami
29 Mar, 2024, 11:45

Ts

D5
29 Mar, 2024, 11:45

Okay. What's the code for such function?

D5
29 Mar, 2024, 11:45

I mean createUserAccount(values);

Light Yagami
29 Mar, 2024, 11:46

values: { username: string; email: string; name: string; password: string; }

Light Yagami
29 Mar, 2024, 11:46
TypeScript
    try {
      const newAccount = await account.create(
        ID.unique(),
        user.email,
        user.password,
        user.name
      );
  
      if (!newAccount) throw Error;
  
      const avatarUrl = avatars.getInitials(user.name);
  
      const newUser = await saveUserToDB({
        accountId: newAccount.$id,
        name: newAccount.name,
        email: newAccount.email,
        username: user.username,
        imageUrl: avatarUrl,
      });
  
      return newUser;
    } catch (error) {
      console.log(error);
      return error;
    }
  }
  
  export async function saveUserToDB(user: {
    accountId: string;
    email: string;
    name: string;
    imageUrl: URL;
    username?: string;
  }) {
    try {
      const newUser = await databases.createDocument(
        appwriteConfig.databaseId,
        appwriteConfig.userCollectionId,
        ID.unique(),
        user
      );
        console.log(newUser);
        
      return newUser;
    } catch (error) {
      console.log(error);
    }
  }```
D5
29 Mar, 2024, 11:47

Ok. That looks fine for me

D5
29 Mar, 2024, 11:54

I think the first part should look like this?

TypeScript
    const newUser = await createUserAccount(values);
    if(newUser){
  // Redirect the user here since it has been logged in:
    navigate('/')

    } else {
//Show errors here
}
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more