Back to blogs
Blog
Writing in public
Longer posts — published here first, then cross-posted to Dev.to with a canonical URL back to this site.
Blog
Longer posts — published here first, then cross-posted to Dev.to with a canonical URL back to this site.
Back to blogs
Back to blogs
Postfirebase, react, reactquery, authentication
this is part 2 to the set up process here This tutorial's course files on the initial commit +...
Date published

Why You Might Still Pick Next.js Over TanStack Start From a TanStack Start...

A complete theme management system for React applications with SSR support! ✨ 🌟...

slides used in the presentation building the generic component In this example we'll use...
this is part 2 to the set up process here
This tutorial's course files on the initial commit + adding auth course files
First step is to set up react-router-dom routes,
1import React from 'react';2import './App.css';3import { Routes, Route } from "react-router-dom";4import { BrowserRouter } from 'react-router-dom';5import { Toolbar } from './components/Toolbar/Toolbar';6import { Project } from './components/Projects/Project';7import { Home } from './components/Home/Home';89function App() {10 return (11 <div className="h-screen w-screen overflow-x-hidden ">12 <BrowserRouter>13 <div className="fixed top-[0px] right-1 w-full z-30">14 <Toolbar />15 </div>16 <div className="w-full h-full mt-16 ">17 <Routes>18 <Route path="/" element={<Home />} />19 <Route path="/project" element={<Project />} />20 </Routes>21 </div>22 </BrowserRouter>23 </div>24 );25}2627export default App;
adding two routes , home.tsx and projects.tsx . notice toolbar.txt doesn't need a route since it'll be available evey where making iy a good place to place the user profile pic
12import React from 'react'3import { GrHome } from "react-icons/gr";4import { IconContext } from "react-icons/lib";5import { Link} from "react-router-dom";6import { FaUserCircle } from 'react-icons/fa';7import { useAuthSignOut} from '@react-query-firebase/auth';8import { auth } from '../../firebase/firebaseConfig';9import { User } from 'firebase/auth';10interface ToolbarProps {11user?:User|null12}1314export const Toolbar: React.FC<ToolbarProps> = ({user}) => {15const mutation = useAuthSignOut(auth);16const userImg =user?.photoURL;17const image =userImg?userImg:'https://picsum.photos/id/237/100/100'18return (19 <div className='w-full bg-slate-500 h-16'>20<IconContext.Provider value={{ size: "25px", className: "table-edit-icons" }} >21 <div className='flex flex-grow flex-3'>22<div className='m-1 w-full p-3 bg-slate-800'>23 <Link to="/"><GrHome /></Link>24 </div>25 <div className='m-1 w-full p-3 bg-slate-600'>26 <Link to="/project">Project</Link>27 </div>28 <div className='m-1 w-fit p-3 bg-slate-700'>29 {!user?<FaUserCircle />30 :<img31 // @ts-ignore32 src={image}33 alt={'no'}34 onClick={()=>{mutation.mutate()}}35 className="rounded-[50%] h-full w-[70px]"36 />}37 </div>3839</div>40</IconContext.Provider>41 </div>42);43}44
since this app will need to know who is who to display relevant data , we'll need to auth guard the routes to only show when authed else redirect to a login page
1import { Navigate } from 'react-router-dom';23//@ts-ignore4export const ProtectedRoute = ({ user, children }) => {5 if (!user) {6 return <Navigate to="/login" replace />;7 }89 return children;10 };
so we'll wrap our routes with the above component
1import React from "react";2import "./App.css";3import { Routes, Route} from "react-router-dom";4import { BrowserRouter} from "react-router-dom";5import { Toolbar } from "./components/Toolbar/Toolbar";6import { Project } from "./components/Projects/Project";7import { Home } from "./components/Home/Home";8import { useAuthUser } from "@react-query-firebase/auth";9import { auth } from "./firebase/firebaseConfig";10import { Login } from "./components/auth/Login";11import { ProtectedRoute } from "./components/auth/PrivateRoutes";1213function App() {14const user= {}15 return (16 <div className="h-screen w-screen overflow-x-hidden ">17 <BrowserRouter>18 <div className="fixed top-[0px] right-1 w-full z-30">19 <Toolbar user={user} />20 </div>21 <div className="w-full h-full mt-16 ">22 <Routes>23 <Route24 path="/"25 element={26 <ProtectedRoute user={user}>27 <Home />28 </ProtectedRoute>29 }30 />3132 <Route33 path="/project"34 element={35 <ProtectedRoute user={user}>36 <Project />37 </ProtectedRoute>38 }39 />40 {/* @ts-ignore */}41 <Route path="/login" element={<Login user={user} />} />42 </Routes>43 </div>44 </BrowserRouter>45 </div>46 );47}4849export default App;
we also added a login route .
setting up the authentication part is pretty straight forward itself , in your firebaseConfig.ts file export the auth from getAuth()
1import { initializeApp } from "firebase/app";2import { getAuth} from "firebase/auth";345const firebaseConfig = {6//your firebase app credentials go here7 };89const app = initializeApp(firebaseConfig);1011export const provider = new GoogleAuthProvider();12export const auth = getAuth(app)
and using the react-query-firebase hooks we can query the auth state with ust one line
1 const query = useAuthUser("user", auth);2 const user = query.data;3 if (query.isFetching) {4return <div className="w-full h-full flex-center ">Loading...</div>;5 }6
and pass in the user object into any child components that need it in app.tsx
as for adding user i chose google signinwithRedirect
1import { GoogleAuthProvider, signInWithRedirect } from "firebase/auth";2import { auth} from "../../firebase/firebaseConfig";34const provider = new GoogleAuthProvider();5provider.addScope('https://mail.google.com/');6export const loginUser= () => {7signInWithRedirect(auth, provider)8.then((result:any) => {9console.log("sign in successful result === ",result)10// The signed-in user info.11 const user = result.user;12 }).catch((error) => {13// Handle Errors here.14console.log("sign in error === ",error)1516 });17181920}
in the repo you'll also notice that you can log out on profile pic click
using the hook const mutation = useAuthSignOut(auth);
then calling mutation.mutate();
on profile pic clicked
and thats it for authentication in te next part we'll begin the functionalty part with firestore