diff --git a/src/app/register/page.jsx b/src/app/register/page.jsx index d633c27e..51a8b17b 100644 --- a/src/app/register/page.jsx +++ b/src/app/register/page.jsx @@ -7,7 +7,6 @@ import { useRouter } from 'next/navigation'; import toast from 'react-hot-toast'; import { v4 } from 'uuid'; -import Loader from '@/components/Loading/Loader'; import CampusAmbassador from '@/components/Register/CampusAmbassador/CampusAmbassador'; import FileInput from '@/components/Register/FileInput/FileInput'; import CheckBox from '@/components/Register/InputCheckBox/CheckBox'; @@ -251,13 +250,22 @@ function Page() { }, }, }); + + const newCookies = JSON.parse(Cookies.get('userData')); + Cookies.set('userData', JSON.stringify({ ...newCookies, isNitR }), { + expires: 7, + sameSite: 'strict', + }); + Cookies.set('userDataDB', res.data.createUser.id, { expires: 7, sameSite: 'strict', }); toast.success( - 'Registration successful! You will recieve confirmation email within 4-5 days!', + isNitR + ? 'Registration successful!' + : 'Registration successful! You will recieve confirmation email within 4-5 days!', { duration: 5000, }, @@ -290,45 +298,39 @@ function Page() { {isLoggedIn ? ( - {loading ? ( - - ) : ( - <> - Register - - {formFields.map((field) => { - if (isNitR && notNitrFields.includes(field.id)) { - return null; - } - return returnFormFields(field); - })} - - - {!isNitR && ( - <> - - {undertakingContent.text} - - - - Please review the Payment Policy before registering. - - - NOTE: Registration Fees (₹899) - - - > - )} + Register + + {formFields.map((field) => { + if (isNitR && notNitrFields.includes(field.id)) { + return null; + } + return returnFormFields(field); + })} + - - {loading ? 'Loading...' : 'Register'} - + {!isNitR && ( + <> + + {undertakingContent.text} + + + + Please review the Payment Policy before registering. + + + NOTE: Registration Fees (₹899) + + > )} + + + {loading ? 'Loading...' : 'Register'} + ) : ( diff --git a/src/components/ProfileMenu/ProfileMenu.jsx b/src/components/ProfileMenu/ProfileMenu.jsx index c3fa3598..21edcb53 100644 --- a/src/components/ProfileMenu/ProfileMenu.jsx +++ b/src/components/ProfileMenu/ProfileMenu.jsx @@ -30,11 +30,7 @@ function ProfileMenu({ handleProfileToggle, handleNavClose }) { const user = getUserDetails(); const router = useRouter(); const path = usePathname(); - - // const { data: userDataInDb } = useSuspenseQuery( - // GET_USER_BY_UID, - // user.uid ? { variables: { uid: user.uid } } : skipToken, - // ); + const isNitr = getUserDetails()?.isNitR; const handleLogout = () => { handleSignOut(); @@ -49,9 +45,6 @@ function ProfileMenu({ handleProfileToggle, handleNavClose }) { useEffect(() => { const mongoId = Cookies.get('userDataDB'); - - console.log('mongoId:', mongoId); - // userDataInDb?.user.data.length > 0; if (mongoId) { if (path === '/register') { toast.success('You are already registered!'); @@ -61,8 +54,6 @@ function ProfileMenu({ handleProfileToggle, handleNavClose }) { } else { setIsRegistered(false); } - - // console.log('userDataInDb:', userDataInDb); }, []); return ( @@ -81,7 +72,7 @@ function ProfileMenu({ handleProfileToggle, handleNavClose }) { {user?.email} {isRegistered ? ( - Your payment is being verified! You will be mailed shortly + !isNitr && Your payment is being verified! You will be mailed shortly ) : ( Complete Your Registration diff --git a/src/context/auth-context.jsx b/src/context/auth-context.jsx index 02e7f732..4d73c526 100644 --- a/src/context/auth-context.jsx +++ b/src/context/auth-context.jsx @@ -10,20 +10,17 @@ export const AuthContext = createContext(); export const AuthProvider = ({ children }) => { const [userInfo, setUserInfo] = useState({}); const [authLoading, setAuthLoading] = useState(false); - const [userMongoId, setUserMongoId] = useState(''); const handleGoogleSignIn = async () => { setAuthLoading(true); try { const user = await signInWithGoogle(); - const token = `Bearer ${user.accessToken}`; if (user) { const userData = { name: user.displayName, email: user.email, uid: user.uid, - token, photoUrl: user.photoURL, }; Cookies.set('userData', JSON.stringify(userData), { @@ -64,8 +61,6 @@ export const AuthProvider = ({ children }) => { handleGoogleSignIn, handleSignOut, authLoading, - userMongoId, - setUserMongoId, }} > {children} diff --git a/src/lib/apollo-client.js b/src/lib/apollo-client.js index 37d159df..f4b1df9c 100644 --- a/src/lib/apollo-client.js +++ b/src/lib/apollo-client.js @@ -1,5 +1,4 @@ -import Cookies from 'js-cookie'; - +import generateToken from '@/utils/generateToken'; import { ApolloClient, from, HttpLink, InMemoryCache } from '@apollo/client'; import { setContext } from '@apollo/client/link/context'; import { onError } from '@apollo/client/link/error'; @@ -44,24 +43,12 @@ const httpLink = new HttpLink({ fetch: reconnectFetch, }); -const authLink = setContext((_, { headers }) => { - let token; - - if (typeof window !== 'undefined') { - try { - const userDataFromCookie = Cookies.get('userData'); - const parsedUserData = JSON.parse(userDataFromCookie); - token = parsedUserData.token; - } catch (error) { - console.error(error); - throw new Error('Error parsing userData from cookie'); - } - } - +const authLink = setContext(async (_, { headers }) => { + const token = await generateToken(); return { headers: { ...headers, - Authorization: token || '', + Authorization: `Bearer ${token}` || '', }, }; }); diff --git a/src/utils/generateToken.js b/src/utils/generateToken.js new file mode 100644 index 00000000..8aa57610 --- /dev/null +++ b/src/utils/generateToken.js @@ -0,0 +1,22 @@ +import { getAuth } from 'firebase/auth'; + +const generateToken = async () => { + try { + const auth = getAuth(); + const user = auth.currentUser; + + if (user) { + const token = await user.getIdToken(); + console.log('Generated Token:', token); + return token; + } else { + console.error('No user is signed in.'); + return null; + } + } catch (error) { + console.error('Error generating token:', error); + return null; + } +}; + +export default generateToken;
Your payment is being verified! You will be mailed shortly