├── src
├── index.css
├── pages
│ ├── Home.jsx
│ ├── Account.jsx
│ └── Signin.jsx
├── index.js
├── components
│ ├── Protected.js
│ └── Navbar.jsx
├── firebase.js
├── App.js
└── context
│ └── AuthContext.js
├── public
├── robots.txt
├── favicon.ico
├── logo192.png
├── logo512.png
├── manifest.json
└── index.html
├── postcss.config.js
├── tailwind.config.js
├── .gitignore
├── package.json
└── README.md
/src/index.css:
--------------------------------------------------------------------------------
1 | @tailwind base;
2 | @tailwind components;
3 | @tailwind utilities;
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fireclint/google-auth-firebase/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fireclint/google-auth-firebase/HEAD/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fireclint/google-auth-firebase/HEAD/public/logo512.png
--------------------------------------------------------------------------------
/postcss.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | plugins: {
3 | tailwindcss: {},
4 | autoprefixer: {},
5 | },
6 | }
7 |
--------------------------------------------------------------------------------
/tailwind.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | content: ["./src/**/*.{js,jsx,ts,tsx}",],
3 | theme: {
4 | extend: {},
5 | },
6 | plugins: [],
7 | }
8 |
--------------------------------------------------------------------------------
/src/pages/Home.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 |
3 | const Home = () => {
4 | return (
5 |
6 |
Home Page
7 |
8 | )
9 | }
10 |
11 | export default Home
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom/client';
3 | import './index.css';
4 | import App from './App';
5 | import { BrowserRouter } from 'react-router-dom';
6 |
7 | const root = ReactDOM.createRoot(document.getElementById('root'));
8 | root.render(
9 |
10 |
11 |
12 | );
13 |
--------------------------------------------------------------------------------
/src/components/Protected.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { Navigate } from 'react-router-dom';
3 | import { UserAuth } from '../context/AuthContext';
4 |
5 | const Protected = ({ children }) => {
6 | const { user } = UserAuth();
7 | if (!user) {
8 | return ;
9 | }
10 |
11 | return children;
12 | };
13 |
14 | export default Protected;
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/src/pages/Account.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { UserAuth } from '../context/AuthContext';
3 |
4 | const Account = () => {
5 | const { logOut, user } = UserAuth();
6 |
7 | const handleSignOut = async () => {
8 | try {
9 | await logOut();
10 | } catch (error) {
11 | console.log(error);
12 | }
13 | };
14 |
15 | return (
16 |
17 |
Account
18 |
19 |
Welcome, {user?.displayName}
20 |
21 |
24 |
25 | );
26 | };
27 |
28 | export default Account;
29 |
--------------------------------------------------------------------------------
/src/firebase.js:
--------------------------------------------------------------------------------
1 | // Import the functions you need from the SDKs you need
2 | import { initializeApp } from "firebase/app";
3 | import { getAuth } from "firebase/auth";
4 |
5 | // TODO: Add SDKs for Firebase products that you want to use
6 | // https://firebase.google.com/docs/web/setup#available-libraries
7 |
8 | // Your web app's Firebase configuration
9 | const firebaseConfig = {
10 | apiKey: "YOUR FIREBASE API KEY",
11 | authDomain: "YOUR FIREBASE AUTHDOMAIN",
12 | projectId: "YOUR FIREBASE PROJECTID",
13 | storageBucket: "YOUR FIREBASE STORAGE BUCKET",
14 | messagingSenderId: "YOUR FIREBASE MESSAGESENDER ID",
15 | appId: "YOUR FIREBASE APPID"
16 | };
17 |
18 | // Initialize Firebase
19 | const app = initializeApp(firebaseConfig);
20 | export const auth = getAuth(app);
21 |
--------------------------------------------------------------------------------
/src/components/Navbar.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { Link } from 'react-router-dom';
3 | import { UserAuth } from '../context/AuthContext';
4 |
5 | const Navbar = () => {
6 | const { user, logOut } = UserAuth();
7 |
8 | const handleSignOut = async () => {
9 | try {
10 | await logOut()
11 | } catch (error) {
12 | console.log(error)
13 | }
14 | }
15 |
16 | return (
17 |
18 |
19 | Firebase Google Auth & Context
20 |
21 | {user?.displayName ? (
22 |
23 | ) : (
24 | Sign in
25 | )}
26 |
27 | );
28 | };
29 |
30 | export default Navbar;
31 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { Route, Routes } from 'react-router-dom';
3 | import Navbar from './components/Navbar';
4 | import Protected from './components/Protected';
5 | import { AuthContextProvider } from './context/AuthContext';
6 | import Account from './pages/Account';
7 | import Home from './pages/Home';
8 | import Signin from './pages/Signin';
9 |
10 | function App() {
11 | return (
12 |
13 |
14 |
15 |
16 | } />
17 | } />
18 |
22 |
23 |
24 | }
25 | />
26 |
27 |
28 |
29 | );
30 | }
31 |
32 | export default App;
33 |
--------------------------------------------------------------------------------
/src/pages/Signin.jsx:
--------------------------------------------------------------------------------
1 | import React, { useEffect } from 'react';
2 | import { GoogleButton } from 'react-google-button';
3 | import { UserAuth } from '../context/AuthContext';
4 | import { useNavigate } from 'react-router-dom';
5 |
6 | const Signin = () => {
7 | const { googleSignIn, user } = UserAuth();
8 | const navigate = useNavigate();
9 |
10 | const handleGoogleSignIn = async () => {
11 | try {
12 | await googleSignIn();
13 | } catch (error) {
14 | console.log(error);
15 | }
16 | };
17 |
18 | useEffect(() => {
19 | if (user != null) {
20 | navigate('/account');
21 | }
22 | }, [user]);
23 |
24 | return (
25 |
26 |
Sign in
27 |
28 |
29 |
30 |
31 | );
32 | };
33 |
34 | export default Signin;
35 |
--------------------------------------------------------------------------------
/src/context/AuthContext.js:
--------------------------------------------------------------------------------
1 | import { useContext, createContext, useEffect, useState } from 'react';
2 | import {
3 | GoogleAuthProvider,
4 | signInWithPopup,
5 | signInWithRedirect,
6 | signOut,
7 | onAuthStateChanged,
8 | } from 'firebase/auth';
9 | import { auth } from '../firebase';
10 |
11 | const AuthContext = createContext();
12 |
13 | export const AuthContextProvider = ({ children }) => {
14 | const [user, setUser] = useState({});
15 |
16 | const googleSignIn = () => {
17 | const provider = new GoogleAuthProvider();
18 | // signInWithPopup(auth, provider);
19 | signInWithRedirect(auth, provider)
20 | };
21 |
22 | const logOut = () => {
23 | signOut(auth)
24 | }
25 |
26 | useEffect(() => {
27 | const unsubscribe = onAuthStateChanged(auth, (currentUser) => {
28 | setUser(currentUser);
29 | console.log('User', currentUser)
30 | });
31 | return () => {
32 | unsubscribe();
33 | };
34 | }, []);
35 |
36 | return (
37 |
38 | {children}
39 |
40 | );
41 | };
42 |
43 | export const UserAuth = () => {
44 | return useContext(AuthContext);
45 | };
46 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "google-auth-yt",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.14.1",
7 | "@testing-library/react": "^13.0.0",
8 | "@testing-library/user-event": "^13.2.1",
9 | "firebase": "^9.6.11",
10 | "react": "^18.1.0",
11 | "react-dom": "^18.1.0",
12 | "react-google-button": "^0.7.2",
13 | "react-router-dom": "^6.3.0",
14 | "react-scripts": "5.0.1",
15 | "web-vitals": "^2.1.0"
16 | },
17 | "scripts": {
18 | "start": "react-scripts start",
19 | "build": "react-scripts build",
20 | "test": "react-scripts test",
21 | "eject": "react-scripts eject"
22 | },
23 | "eslintConfig": {
24 | "extends": [
25 | "react-app",
26 | "react-app/jest"
27 | ]
28 | },
29 | "browserslist": {
30 | "production": [
31 | ">0.2%",
32 | "not dead",
33 | "not op_mini all"
34 | ],
35 | "development": [
36 | "last 1 chrome version",
37 | "last 1 firefox version",
38 | "last 1 safari version"
39 | ]
40 | },
41 | "devDependencies": {
42 | "autoprefixer": "^10.4.5",
43 | "postcss": "^8.4.12",
44 | "tailwindcss": "^3.0.24"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `yarn start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13 |
14 | The page will reload when you make changes.\
15 | You may also see any lint errors in the console.
16 |
17 | ### `yarn test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `yarn build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `yarn eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!**
35 |
36 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39 |
40 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `yarn build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------