Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added recoil logic #152

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"react-icons": "^5.3.0",
"react-router-dom": "^6.27.0",
"react-toastify": "^9.1.3",
"recoil": "^0.7.7",
"uuid": "^9.0.1"
},
"devDependencies": {
Expand Down
27 changes: 27 additions & 0 deletions src/atoms/authState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// authState.js
import { atom } from "recoil";

export const currentUserState = atom({
key: "currentUserState",
default: null,
});

export const userLoggedInState = atom({
key: "userLoggedInState",
default: false,
});

export const isEmailUserState = atom({
key: "isEmailUserState",
default: false,
});

export const isGoogleUserState = atom({
key: "isGoogleUserState",
default: false,
});

export const authLoadingState = atom({
key: "authLoadingState",
default: true,
});
7 changes: 4 additions & 3 deletions src/components/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from "react";
import { useNavigate } from "react-router-dom";
import { useAuth } from "../contexts/authContext/index";
import UserAccount from "./UserAccount";
import { useRecoilValue } from "recoil";
import { currentUserState, userLoggedInState } from "../atoms/authState";

const Navbar = () => {
const { currentUser } = useAuth();
const { userLoggedIn } = useAuth();
const currentUser = useRecoilValue(currentUserState);
const userLoggedIn = useRecoilValue(userLoggedInState);
const [isVisible, setIsVisible] = React.useState(false);
const navigate = useNavigate();
// function to download passwords
Expand Down
6 changes: 4 additions & 2 deletions src/components/SignIn.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useState } from 'react';
import { Link, Navigate } from 'react-router-dom';
import { doSignInWithEmailAndPassword, doSignInWithGoogle } from '../firebase/auth';
import { useAuth } from '../contexts/authContext/index';

import { AiFillEye, AiFillEyeInvisible } from 'react-icons/ai'; // Importing React Icons
import { useRecoilValue } from 'recoil';
import { userLoggedInState } from '../atoms/authState';

const SignIn = () => {
const { userLoggedIn } = useAuth();
const { userLoggedIn } = useRecoilValue(userLoggedInState);

const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
Expand Down
5 changes: 3 additions & 2 deletions src/components/SignUp.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useState } from 'react'
import { AiFillEye, AiFillEyeInvisible } from 'react-icons/ai'
import { Link, Navigate } from 'react-router-dom'
import { useAuth } from '../contexts/authContext/index'
import { doCreateUserWithEmailAndPassword } from '../firebase/auth'
import { useRecoilValue } from 'recoil'
import { userLoggedInState } from '../atoms/authState'

const Register = () => {

Expand All @@ -14,7 +15,7 @@ const Register = () => {
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);

const { userLoggedIn } = useAuth()
const { userLoggedIn } = useRecoilValue(userLoggedInState)

const onSubmit = async (e) => {
e.preventDefault()
Expand Down
50 changes: 17 additions & 33 deletions src/contexts/authContext/index.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import React, { useContext, useState, useEffect } from "react";
import { auth } from "../../firebase/firebase";
// import { GoogleAuthProvider } from "firebase/auth";
import React, { useEffect } from "react";
import { useRecoilValue, useSetRecoilState } from "recoil";
import { onAuthStateChanged } from "firebase/auth";

const AuthContext = React.createContext();

export function useAuth() {
return useContext(AuthContext);
}
import { auth } from "../../firebase/firebase";
import { authLoadingState, currentUserState, isEmailUserState, isGoogleUserState, userLoggedInState } from "../../atoms/authState";

export function AuthProvider({ children }) {
const [currentUser, setCurrentUser] = useState(null);
const [userLoggedIn, setUserLoggedIn] = useState(false);
const [isEmailUser, setIsEmailUser] = useState(false);
const [isGoogleUser, setIsGoogleUser] = useState(false);
const [loading, setLoading] = useState(true);
const setCurrentUser = useSetRecoilState(currentUserState);
const setUserLoggedIn = useSetRecoilState(userLoggedInState);
const setIsEmailUser = useSetRecoilState(isEmailUserState);
const setIsGoogleUser = useSetRecoilState(isGoogleUserState);
const setLoading = useSetRecoilState(authLoadingState);

const loading = useRecoilValue(authLoadingState); // Retrieve the loading state

useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, initializeUser);
Expand All @@ -23,20 +20,19 @@ export function AuthProvider({ children }) {

async function initializeUser(user) {
if (user) {

setCurrentUser({ ...user });

// check if provider is email and password login
// Check if provider is email and password login
const isEmail = user.providerData.some(
(provider) => provider.providerId === "password"
);
setIsEmailUser(isEmail);

// check if the auth provider is google or not
// const isGoogle = user.providerData.some(
// Uncomment and add GoogleAuthProvider if needed
// const isGoogle = user.providerData.some(
// (provider) => provider.providerId === GoogleAuthProvider.PROVIDER_ID
// );
// setIsGoogleUser(isGoogle);
// );
// setIsGoogleUser(isGoogle);

setUserLoggedIn(true);
} else {
Expand All @@ -47,17 +43,5 @@ export function AuthProvider({ children }) {
setLoading(false);
}

const value = {
userLoggedIn,
isEmailUser,
isGoogleUser,
currentUser,
setCurrentUser
};

return (
<AuthContext.Provider value={value}>
{!loading && children}
</AuthContext.Provider>
);
return <>{!loading && children}</>;
}
9 changes: 6 additions & 3 deletions src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
import { AuthProvider } from './contexts/authContext/index.jsx'
import { RecoilRoot } from 'recoil'

ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<AuthProvider>
<App />
</AuthProvider>
<RecoilRoot>
<AuthProvider>
<App />
</AuthProvider>
</RecoilRoot>
</React.StrictMode>,
)
Loading