import { useAuth } from "../../contexts/authContext"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; import UserService from "../../services/userService"; import { User } from "../../models/user"; import { GetServerSidePropsContext } from "next"; import nookies from "nookies"; import firebaseAdmin from "firebase-admin"; import unfetch from "isomorphic-unfetch"; import { FaPeopleCarry, FaArrowRight, FaBullhorn } from "react-icons/fa"; import BackgroundLayout from "../../components/Layouts/BackgroundLayout"; import TeamService from "../../services/teamService"; import Loading from "../../components/Loading"; /** * figure out where to take the user based on everything */ const userService: UserService = new UserService(); const teamService: TeamService = new TeamService(); function RouteHandler() { const { currUser } = useAuth(); const router = useRouter(); const [loading, setLoading] = useState(true); const [user, setUser] = useState(null); useEffect(() => { (async function () { try { // if not authenticated, take user to the login if (!currUser) { console.log( "not authenticated...routing from dashboard to teams home" ); router.push("/teams/login"); } // get user const returnedUser: User | null = await userService.getUser( currUser.uid ); console.log(returnedUser); // if user has no profile, then go to create profile if ( !returnedUser || !returnedUser.firstName || !returnedUser.lastName || !returnedUser.nickName ) { console.log("no profile for the user, routing him/her there"); router.push("/teams/profile"); } setUser(returnedUser); // check if user is in a team go to the team dashboard const returnedTeamMembers = await teamService.getTeamMembersByUserId( currUser.uid ); if (returnedTeamMembers.length > 0) { console.log( "in a team! going to the team dashboard of the first one!" ); router.push("/teams/" + returnedTeamMembers[0].teamId); return; } } catch (error) { console.log(error); router.push("/teams/login"); } setLoading(false); })(); }, [currUser]); if (loading || !user) { return ; } // not in a team yet, and have not created a team yet return (
{/* header */}
πŸ‘‹Hey, {user.firstName}
Let's get you started.
{/* create team hover thing */} {/* tell your manager to add your email */} or learn more about{" "}
); } export async function getServerSideProps(context: GetServerSidePropsContext) { // const cookies = nookies.get(context); // var user: User | null = null // if (cookies.token) { // try { // const headers: HeadersInit = { // 'Content-Type': 'application/json', // Authorization: JSON.stringify({ token: cookies.token }) // }; // const result = await unfetch('/api/auth/validateToken', { headers }); // console.log(result) // // get user data // const userService: UserService = new UserService() // // the user is authenticated! // // FETCH STUFF HERE!! πŸš€ // // user = await userService.getUser("asdf") // } catch (e) { // // let exceptions fail silently // // could be invalid token, just let client-side deal with that // console.log(e) // } // } // // Pass data to the page via props // return { props: { // user // } // } return { props: {} }; } export default RouteHandler;