Skip to content

Commit 7910797

Browse files
committed
completed the firebase auth
1 parent 5656c50 commit 7910797

File tree

10 files changed

+153
-71
lines changed

10 files changed

+153
-71
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,4 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
.env

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Project to practice the Graphql with react",
55
"main": "index.js",
66
"scripts": {
7-
"dev": "parcel src/index.html",
7+
"dev": "parcel src/index.html --open --port 5050",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
1010
"repository": {

src/App.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,33 @@ import React from 'react'
22
import ReactDOM from 'react-dom';
33
import {
44
BrowserRouter as Router,
5-
Switch,
65
Route,
7-
Link
86
} from "react-router-dom";
97

10-
118
// paths
129
import Login from './components/Login'
1310
import Register from './components/Register'
14-
11+
import AuthenticatedApp from './components/AuthenticatedApp'
1512
import './scss/main.scss'
1613

14+
import PrivateRoute from '../src/Routes/PrivateRoute'
1715

18-
// router
19-
20-
import { ApolloProvider } from '@apollo/react-hooks'
21-
import ShowCountries from '../src/components/ShowCountries'
22-
import ApolloClient from 'apollo-boost'
16+
import { AuthProvider } from './context/AuthContext'
2317

24-
const client = new ApolloClient({
25-
uri: 'https://countries-274616.ew.r.appspot.com/'
26-
})
18+
// const client = new ApolloClient({
19+
// uri: 'https://countries-274616.ew.r.appspot.com/'
20+
// })
2721

2822
const App = () => {
2923

3024
return (
31-
<Router>
32-
<Route path="/login" component={Login} />
33-
<Route path="/signup" component={Register} />
34-
</Router>
35-
36-
// <ApolloProvider client={client}>
37-
// <div className="">
38-
// {/* <ShowCountries/> */}
39-
// </div>
40-
// </ApolloProvider>
25+
<AuthProvider>
26+
<Router >
27+
<PrivateRoute exact path="/" component={AuthenticatedApp} />
28+
<Route path="/login" component={Login} />
29+
<Route path="/signup" component={Register} />
30+
</Router>
31+
</AuthProvider>
4132
)
4233
}
4334

src/Firebase/config.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
// Firebase App (the core Firebase SDK) is always required and
2-
// must be listed before other Firebase SDKs
31
import * as firebase from "firebase/app";
42

53
// Add the Firebase services that you want to use
64
import "firebase/auth";
75

86
const config = {
9-
apiKey: "AIzaSyAcPbOBP9js9jC6LGtm9ZjqwXjVpQjBQxM",
10-
authDomain: "react-firebase-fc06e.firebaseapp.com",
11-
databaseURL: "https://react-firebase-fc06e.firebaseio.com",
12-
projectId: "react-firebase-fc06e",
13-
storageBucket: "react-firebase-fc06e.appspot.com",
14-
messagingSenderId: "564100058598",
15-
appId: "1:564100058598:web:8c6ebd5ba04dc4faab1a27",
16-
measurementId: "G-GZNMEDXZPH"
7+
apiKey: process.env.FIREBASE_API_KEY,
8+
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
9+
databaseURL: process.env.FIREBASE_DATABASE_URL,
10+
projectId: process.env.FIREBASE_PROJECT_ID,
11+
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
12+
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
13+
appId: process.env.FIREBASE_APP_ID,
14+
measurementId: process.env.FIREBASE_MESUREMENT_ID
1715
};
1816

19-
firebase.initializeApp(config)
17+
const app = firebase.initializeApp(config)
2018

21-
export default config;
19+
export default app;

src/Routes/PrivateRoute.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
// import React from 'react';
2-
// import { Route, Redirect } from 'react-router-dom'
3-
// import { AuthContext } from "../context/AuthContext";
1+
import React, { useContext} from 'react';
2+
import { Route, Redirect } from 'react-router-dom'
3+
import { AuthContext } from "../context/AuthContext";
44

5-
// const PrivateRoute = ({ component: RouteComponent, ...rest }) => {
6-
// const {currentUser} = useContext(AuthContext);
7-
// return (
8-
// <Route
9-
// {...rest}
10-
// render={routeProps =>
11-
// !!currentUser ? (
12-
// <RouteComponent {...routeProps} />
13-
// ) : (
14-
// <Redirect to={"/login"} />
15-
// )
16-
// }
17-
// />
18-
// );
19-
// };
5+
const PrivateRoute = ({ component: RouteComponent, ...rest }) => {
6+
const { currentUser } = useContext(AuthContext);
7+
8+
return (
9+
<Route
10+
{...rest}
11+
render={routeProps =>
12+
!!currentUser ? (
13+
<RouteComponent {...routeProps} />
14+
) : (
15+
<Redirect to={"/login"} />
16+
)
17+
}
18+
/>
19+
);
20+
};
2021

21-
22-
// export default PrivateRoute
22+
export default PrivateRoute

src/components/AuthenticatedApp.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import app from '../firebase/config'
3+
import { useHistory } from 'react-router-dom'
4+
5+
6+
const AuthenticatedApp = () => {
7+
let history = useHistory()
8+
9+
const logOut = () => {
10+
11+
app.auth().signOut().then((response) => {
12+
console.log(response)
13+
history.push('/login');
14+
}).catch((error) =>{
15+
console.log(error)
16+
})
17+
}
18+
19+
return(
20+
<div>
21+
Authenticated
22+
<button className="" onClick={logOut}>Logout</button>
23+
</div>
24+
)
25+
}
26+
27+
export default AuthenticatedApp;

src/components/HomePage.js

Whitespace-only changes.

src/components/Login.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,56 @@
1-
import React from 'react';
2-
1+
import React, { useState } from 'react';
2+
import app from '../firebase/config'
3+
import { useHistory } from 'react-router-dom'
34
const Login = () => {
4-
return(<div>
55

6-
</div>)
6+
let history = useHistory()
7+
8+
const [email, setEmail] = useState('')
9+
const [pass, setPass] = useState('')
10+
11+
const onInputChange = (e, name) => {
12+
let inputValue = e.target.value
13+
if(name === 'pass') {
14+
setPass(inputValue)
15+
} if(name === 'email') {
16+
setEmail(inputValue)
17+
}
18+
}
19+
20+
const loginUser = () => {
21+
app.auth().signInWithEmailAndPassword(email, pass).then((response) => {
22+
history.push('/')
23+
}).catch(error => {
24+
// Handle Errors here.
25+
var errorCode = error.code;
26+
var errorMessage = error.message;
27+
console.log(errorMessage);
28+
console.log(errorCode)
29+
});
30+
}
31+
32+
const registerUser = () => {
33+
history.push('/signup')
34+
}
35+
36+
return (
37+
<div className="form">
38+
<div className="form-row">
39+
<label>Email</label>
40+
<input name="email" onChange={(e) => onInputChange(e,'email')}></input>
41+
</div>
42+
<div className="form-row">
43+
<label>Password</label>
44+
<input name="password" type="password" onChange={(e) => onInputChange(e, 'pass')}></input>
45+
</div>
46+
<button className="form-row" onClick={loginUser}>
47+
Login
48+
</button>
49+
<button className="form-row" onClick={registerUser}>
50+
Register
51+
</button>
52+
</div>
53+
)
754
}
855

956
export default Login

src/components/Register.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React, { useState } from 'react';
2+
import app from '../firebase/config';
3+
import { useHistory } from 'react-router-dom';
24

35
const Register = () => {
4-
6+
let history = useHistory()
57
const [email, setEmail] = useState('')
68
const [pass, setPass] = useState('')
79

@@ -14,9 +16,19 @@ const Register = () => {
1416
}
1517
}
1618

17-
1819
const registerUser = () => {
19-
20+
app.auth().createUserWithEmailAndPassword(email, pass).then((response) => {
21+
if(response) {
22+
console.log("----",response)
23+
history.push('/login')
24+
}
25+
}).catch(function(error) {
26+
// Handle Errors here.
27+
var errorCode = error.code;
28+
var errorMessage = error.message;
29+
// ...
30+
console.log(errorMessage, errorCode);
31+
})
2032
}
2133

2234
return(
@@ -29,10 +41,9 @@ const Register = () => {
2941
<label>Password</label>
3042
<input name="password" type="password" onChange={(e) => onInputChange(e, 'pass')}></input>
3143
</div>
32-
<button className="form-row">
33-
Login
44+
<button className="form-row" onClick={registerUser}>
45+
Register
3446
</button>
35-
3647
</div>)
3748
}
3849

src/context/AuthContext.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import React, { useEffect, useState } from 'react';
2-
import authConfig from '../Firebase/config'
2+
import app from '../firebase/config'
33

44
export const AuthContext = React.createContext();
5-
65
export const AuthProvider = ({ children }) => {
76
const [currentUser, setCurrentUser] = useState(null);
87
const [pending, setPending] = useState(true);
98

109
useEffect(() => {
11-
authConfig.auth().onAuthStateChanged((user) => {
12-
setCurrentUser(user)
13-
setPending(false)
10+
app.auth().onAuthStateChanged((user) => {
11+
setPending(true)
12+
if (user) {
13+
console.log("__",user)
14+
setCurrentUser(user)
15+
setPending(false)
16+
} else {
17+
console.log("err",user)
18+
setCurrentUser(null)
19+
setPending(false)
20+
}
1421
});
1522
}, []);
1623

1724
if(pending){
18-
return <>Loading...</>
25+
return <>Verifying Login.....</>
1926
}
2027

2128
return (

0 commit comments

Comments
 (0)