adrianhajdin / social_media_app

Build a modern social app with a stunning UI with a native mobile feel, a special tech stack, an infinite scroll feature, and amazing performance using React JS, Appwrite, TypeScript, and more.
https://jsmastery.pro
1.21k stars 364 forks source link

not getting redirected upon signing up #6

Open utkarshkrishna2004 opened 11 months ago

utkarshkrishna2004 commented 11 months ago

I did each and everything same till 2 hrs and 06 mins, everything on the appwrite side is working properly, the database is being created but only upon creating a user, I'm not getting redirected to the homepage.

AuthContext.tsx

import { getCurrentUser } from "@/lib/appwrite/api";
import { IContextType, IUser } from "@/types";
import { createContext, useContext, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";

export const INITIAL_USER = {
   id: "",
   name: "",
   username: "",
   email: "",
   imageUrl: "",
   bio: "",
};

const INITIAL_STATE = {
   user: INITIAL_USER,
   isLoading: false,
   isAuthenticated: false,
   setUser: () => {},
   setIsAuthenticated: () => {},
   checkAuthUser: async () => false as boolean,
};

const AuthContext = createContext<IContextType>(INITIAL_STATE);

const AuthProvider = ({ children }: { children: React.ReactNode }) => {
   const [user, setUser] = useState<IUser>(INITIAL_USER);
   const [isLoading, setIsLoading] = useState(false);
   const [isAuthenticated, setIsAuthenticated] = useState(false);

   const navigate = useNavigate();

   const checkAuthUser = async () => {
      setIsLoading(true);
      try {
         const currentAccount = await getCurrentUser();

         if (currentAccount) {
            setUser({
               id: currentAccount.$id,
               name: currentAccount.name,
               username: currentAccount.username,
               email: currentAccount.email,
               imageUrl: currentAccount.imageUrl,
               bio: currentAccount.bio,
            });

            setIsAuthenticated(true);

            return true;
         }

         return false;
      } catch (error) {
         console.log(error);
         return false;
      } finally {
         setIsLoading(false);
      }
   };

   useEffect(() => {
      // localStorage.getItem('cookieFallback') === null
      if (
         localStorage.getItem('cookieFallback') === '[]'
      ) navigate("/sign-in");

      checkAuthUser();
   }, []);

   const value = {
      user,
      setUser,
      isLoading,
      isAuthenticated,
      setIsAuthenticated,
      checkAuthUser,
   };

   return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
};

export default AuthProvider;

export const useUserContext = () => useContext(AuthContext);
MohammadHarish52 commented 11 months ago

Their might be error in api.ts related to console.error

MohammadHarish52 commented 11 months ago

before the chechkauthuser function you have setisloading to true,that might also cause the issue to persists, remove that line and try again

Soron007 commented 11 months ago

Did the solution work?

MohammadHarish52 commented 11 months ago

Did the solution work?

for me the redirecting is working fine

raphaelluersen commented 11 months ago

All good here too, just a few hiccups on dimension & sizes for vh %, which could implicate on other issues on less stable systems. review set up section, and react router dom installation perhaps