Skip to content

Commit 62c6697

Browse files
committed
feat: Added Authentication module. Enabled it for Google, GitHub. SignIn and SignOut works well now.
1 parent cf08bb5 commit 62c6697

12 files changed

Lines changed: 847 additions & 11 deletions

File tree

components/core/App.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import dynamic from "next/dynamic";
55
import { harperFetch } from "../../utils/HarperFetch";
66

77
// Dummy Shape Data
8-
// import { shapes } from "../../data/shapes";
8+
import { shapes } from "../../data/shapes";
99

1010
// loader
1111
import Loader from "react-loader-spinner";
@@ -22,10 +22,10 @@ const App = (props) => {
2222
setLoading(true);
2323

2424
// fetching the shape data
25-
const shapes = await harperFetch({
25+
/*const shapes = await harperFetch({
2626
operation: "sql",
2727
sql: "SELECT * FROM tryshape.shapes",
28-
});
28+
});*/
2929
console.log(shapes);
3030
let modifiedShapes = shapes.map((shape, index) => {
3131
shape.showAdvanced = false;

components/core/Landing.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
3+
// Header
4+
import { Header } from '..'
5+
6+
const Landing = ({ setOpen, user, setUser }) => {
7+
8+
return(
9+
<div>
10+
<Header setOpen={setOpen} user={user} setUser={setUser} />
11+
<div>
12+
<h1>Try Shape</h1>
13+
</div>
14+
</div>
15+
)
16+
};
17+
18+
export default Landing;

components/core/SignInModal.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from "react";
2+
3+
// icons
4+
import { FaGithub, FaGoogle } from "react-icons/fa";
5+
6+
// Button
7+
import Button from "react-bootstrap/Button";
8+
9+
// Modal
10+
import Modal from "react-bootstrap/Modal";
11+
12+
// sign In providers
13+
import { githubProvider, googleProvider } from "../../utils/Auth/auth-methods";
14+
import socialMediaAuth from "../../utils/Auth/auth";
15+
16+
const SignInModal = ({ open, setOpen }) => {
17+
const handleOnClick = async (provider) => {
18+
const res = await socialMediaAuth(provider);
19+
await setOpen(false);
20+
};
21+
return (
22+
<Modal
23+
size="lg"
24+
aria-labelledby="contained-modal-title-vcenter"
25+
show={ open }
26+
onHide={() => setOpen(false)}
27+
centered
28+
>
29+
<Modal.Header closeButton>
30+
<Modal.Title id="contained-modal-title-vcenter">
31+
Sign In
32+
</Modal.Title>
33+
</Modal.Header>
34+
<Modal.Body>
35+
<div className="flex">
36+
<Button onClick={() => handleOnClick(googleProvider)}>
37+
Sign In with Google <FaGoogle />
38+
</Button>
39+
40+
<Button onClick={() => handleOnClick(githubProvider)}>
41+
Sign In with Github
42+
<FaGithub />
43+
</Button>
44+
</div>
45+
</Modal.Body>
46+
</Modal>
47+
);
48+
};
49+
50+
export default SignInModal;

components/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// core
12
export { default as App } from "./core/App";
3+
export { default as Landing } from "./core/Landing";
4+
export { default as SignInModal } from "./core/SignInModal";
25

6+
// utils
7+
export { default as Header } from "./utils/Header";
38
export { default as ShapeList } from './utils/ShapeList';

components/utils/Header.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React, { useState } from "react";
2+
3+
// link from next
4+
import Link from "next/link";
5+
6+
// auth for signin out
7+
import { auth } from "../../utils/firebase";
8+
9+
// toast
10+
import toast from "react-hot-toast";
11+
12+
// icon
13+
import { FaShapes } from "react-icons/fa";
14+
15+
// Button
16+
import Button from "react-bootstrap/Button";
17+
18+
const Header = ({ setOpen, user, setUser }) => {
19+
console.log(user);
20+
// sign out function
21+
const signOut = () => {
22+
auth()
23+
.signOut()
24+
.then(() => {
25+
setUser([]);
26+
toast.success("Signed Out!");
27+
})
28+
.catch((error) => {
29+
// An error happened.
30+
console.log(error);
31+
});
32+
};
33+
34+
return (
35+
<div>
36+
<Link href="/">
37+
<a>
38+
TryShape
39+
<span>
40+
<FaShapes />
41+
</span>
42+
</a>
43+
</Link>
44+
{(user.email || user.displayName) ? (
45+
<>
46+
<div>
47+
<img
48+
src={
49+
user.photoURL
50+
? user.photoURL
51+
: `https://unavatar.vercel.app/${user.email}`
52+
}
53+
alt="profile"
54+
/>
55+
{user.displayName ? user.displayName : "User"}
56+
</div>
57+
58+
<Button onClick={signOut}>
59+
<div>Sign Out</div>
60+
</Button>
61+
</>
62+
) : (
63+
<Button onClick={() => setOpen(true)}>
64+
<div>Sign In</div>
65+
</Button>
66+
)}
67+
</div>
68+
);
69+
};
70+
71+
export default Header;

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
"start": "next start"
99
},
1010
"dependencies": {
11+
"bootstrap": "4.6.0",
1112
"downloadjs": "^1.4.7",
13+
"firebase": "^8.6.7",
1214
"html-to-image": "^1.6.2",
1315
"next": "^10.0.0",
1416
"react": "17.0.1",
17+
"react-bootstrap": "^1.6.1",
1518
"react-clip-path": "^0.0.6",
1619
"react-dom": "17.0.1",
1720
"react-hot-toast": "^2.0.0",
1821
"react-icons": "^4.2.0",
22+
"react-infinite-scroll-component": "^6.1.0",
1923
"react-loader-spinner": "^4.0.0",
2024
"react-switch": "^6.0.0",
2125
"styled-components": "^5.3.0"

pages/_app.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1+
import { useEffect, useState } from "react";
12

23
// styles
34
import '../styles/global.css';
5+
import 'bootstrap/dist/css/bootstrap.min.css';
46

57
// toaster
68
import { Toaster } from "react-hot-toast";
79

10+
// firebase auth
11+
import { auth } from "../utils/firebase";
12+
13+
// SignIn modal
14+
import { SignInModal } from '../components';
15+
16+
817
export default function App({ Component, pageProps }) {
18+
// handling auth user
19+
const [user, setUser] = useState([]);
20+
const [open, setOpen] = useState(false);
21+
22+
const props = {
23+
open,
24+
setOpen,
25+
user,
26+
setUser
27+
};
28+
29+
// handling auth and storing user if found
30+
auth().onAuthStateChanged((user) => {
31+
if (user) {
32+
setUser(user);
33+
}
34+
});
35+
936
return (
1037
<>
1138
<Toaster position="top-right" reverseOrder={false} />
12-
<Component {...pageProps} />
39+
<Component {...pageProps} {...props} />
40+
<SignInModal open={open} setOpen={setOpen} />
1341
</>
1442
);
1543
}

pages/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from "react";
2-
import { App } from "../components";
2+
import { App, Landing } from "../components";
33

44
const index = (props) => {
55
return (
66
<main>
7+
<Landing {...props} />
78
<App {...props} />
89
</main>
910
);

utils/Auth/auth-methods.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { auth } from "../firebase";
2+
3+
export const googleProvider = new auth.GoogleAuthProvider();
4+
export const githubProvider = new auth.GithubAuthProvider();

utils/Auth/auth.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { auth } from "../firebase";
2+
3+
const socialMediaAuth = (provider) => {
4+
auth()
5+
.signInWithPopup(provider)
6+
.then((res) => {
7+
return res.user;
8+
})
9+
.catch((err) => {
10+
console.log(err);
11+
return err;
12+
});
13+
};
14+
15+
export default socialMediaAuth;

0 commit comments

Comments
 (0)