Skip to content

Commit a2435c8

Browse files
authored
Merge pull request #3 from Jgarnes/main
Removed React-Auth-kit
2 parents 6957af9 + 6a67f52 commit a2435c8

File tree

8 files changed

+1013
-330
lines changed

8 files changed

+1013
-330
lines changed

package-lock.json

Lines changed: 861 additions & 326 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
"@emotion/styled": "^11.11.0",
1818
"@mui/icons-material": "^5.15.12",
1919
"@mui/material": "^5.15.12",
20+
"firebase": "^10.9.0",
2021
"git": "^0.1.5",
2122
"node": "^21.6.2",
2223
"react": "^18.2.0",
23-
"react-auth-kit": "^3.1.2",
2424
"react-dom": "^18.2.0",
2525
"react-draggable": "^4.4.6",
2626
"react-router-dom": "^6.22.3"

src/components/login/AuthDetails.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { onAuthStateChanged, signOut, User } from "firebase/auth";
2+
import { useEffect, useState } from "react";
3+
import { auth } from '../../firebase-config';
4+
5+
const AuthDetails = () => {
6+
const [authUser, setAuthUser] = useState<User | null>(null);
7+
8+
useEffect(() => {
9+
const removeAuthListener = onAuthStateChanged(auth, (user) => {
10+
if (user) {
11+
setAuthUser(user);
12+
} else {
13+
setAuthUser(null);
14+
}
15+
})
16+
return () => removeAuthListener();
17+
}, []);
18+
19+
const userSignOut = () => {
20+
signOut(auth).then(() => {
21+
console.log('signed out worked')
22+
}).catch(error => console.log(error))
23+
}
24+
25+
return <div>{authUser ? <><p>{`Signed In as ${auth.currentUser?.email}`}</p><button onClick={userSignOut}> Sign Out</button> </> : <p>Signed Out</p>}</div>
26+
};
27+
28+
export default AuthDetails

src/components/login/Login.tsx renamed to src/components/login/loginForm.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
import './login.css';
2+
import { signInWithEmailAndPassword } from 'firebase/auth';
3+
import { useState } from 'react';
4+
import { auth } from '../../firebase-config';
25

36
function Login() {
7+
const [email, setEmail] = useState('');
8+
const [password, setPassword] = useState('');
9+
10+
const signIn = (e: { preventDefault: () => void; }) => {
11+
e.preventDefault();
12+
signInWithEmailAndPassword(auth, email, password)
13+
.then((userCredential) => {
14+
console.log(userCredential)
15+
}).catch((error) => {
16+
console.log(error)
17+
})
18+
};
19+
420
return (
521
<>
622
<div className="main-container">
@@ -20,12 +36,14 @@ function Login() {
2036

2137
<div className="_or">or</div>
2238

23-
<form action="" className="the-form">
39+
<form onSubmit={signIn} className="the-form">
2440
<label htmlFor="email">Email</label>
2541
<input
2642
type="email"
2743
name="email"
2844
id="email"
45+
value={email}
46+
onChange={(e) => setEmail(e.target.value)}
2947
placeholder="Enter your email"
3048
></input>
3149

@@ -34,6 +52,8 @@ function Login() {
3452
type="password"
3553
name="password"
3654
id="password"
55+
value={password}
56+
onChange={(e) => setPassword(e.target.value)}
3757
placeholder="Enter your password"
3858
></input>
3959

src/components/login/signUp.tsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import './login.css';
2+
import { createUserWithEmailAndPassword } from 'firebase/auth';
3+
import { useState } from 'react';
4+
import { auth } from '../../firebase-config';
5+
6+
function SignUp() {
7+
const [email, setEmail] = useState('');
8+
const [password, setPassword] = useState('');
9+
10+
const signUp = (e: { preventDefault: () => void; }) => {
11+
e.preventDefault();
12+
createUserWithEmailAndPassword(auth, email, password)
13+
.then((userCredential) => {
14+
console.log(userCredential)
15+
}).catch((error) => {
16+
console.log(error)
17+
})
18+
};
19+
20+
return (
21+
<>
22+
<div className="main-container">
23+
<div className="form-container">
24+
<div className="form-body">
25+
</div>
26+
27+
<form onSubmit={signUp} className="the-form">
28+
<h1>Sign Up</h1>
29+
<label htmlFor="email">Email</label>
30+
<input
31+
type="email"
32+
name="email"
33+
id="email"
34+
value={email}
35+
onChange={(e) => setEmail(e.target.value)}
36+
placeholder="Enter your email"
37+
></input>
38+
39+
<label htmlFor="password">Password</label>
40+
<input
41+
type="password"
42+
name="password"
43+
id="password"
44+
value={password}
45+
onChange={(e) => setPassword(e.target.value)}
46+
placeholder="Enter your password"
47+
></input>
48+
49+
<input type="submit" value="Sign Up"></input>
50+
</form>
51+
</div>
52+
</div>
53+
</>
54+
);
55+
}
56+
57+
export default SignUp;

src/firebase-auth.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { auth } from "./firebase-config";
2+
import { createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut, UserCredential } from "firebase/auth";
3+
4+
// Sign up function
5+
export const signUp = async (email: string, password: string): Promise<UserCredential> => {
6+
return createUserWithEmailAndPassword(auth, email, password);
7+
};
8+
9+
// Sign in function
10+
export const signIn = async (email: string, password: string): Promise<UserCredential> => {
11+
return signInWithEmailAndPassword(auth, email, password);
12+
};
13+
14+
// Sign out function
15+
export const signOutUser = async (): Promise<void> => {
16+
return signOut(auth);
17+
};

src/firebase-config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { initializeApp } from "firebase/app";
2+
import { getAuth } from "firebase/auth";
3+
4+
const firebaseConfig = {
5+
apiKey: "AIzaSyC4Xc8yP6YZopnPpeaYGjsjUg05oG2GT-Y",
6+
authDomain: "agile-coder-quest.firebaseapp.com",
7+
projectId: "agile-coder-quest",
8+
storageBucket: "agile-coder-quest.appspot.com",
9+
messagingSenderId: "770833514045",
10+
appId: "1:770833514045:web:e9c52325e78d9a6c5ef454",
11+
measurementId: "G-ENRRMBS1S1"
12+
};
13+
14+
//Initialize Firebase
15+
const app = initializeApp(firebaseConfig);
16+
17+
//Initializes Authentication
18+
export const auth = getAuth(app);

src/pages/LoginPage/LoginPage.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import Login from '../../components/login/Login';
1+
import Login from '../../components/login/loginForm';
2+
import SignUp from '../../components/login/signUp'
3+
import AuthDetails from '../../components/login/AuthDetails';
24

35
const LoginPage = () => {
4-
return <Login />;
6+
return (
7+
<div>
8+
<Login />
9+
<SignUp />
10+
<AuthDetails />
11+
</div>
12+
);
513
};
614

715
export default LoginPage;

0 commit comments

Comments
 (0)