Skip to content

Commit 88bc42a

Browse files
committed
Auth mgmt
Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com>
1 parent 30d3be6 commit 88bc42a

File tree

9 files changed

+196
-15
lines changed

9 files changed

+196
-15
lines changed

src/apis/Contract.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ export const web3 = new Web3(Web3.givenProvider);
1616

1717
const Contract = new web3.eth.Contract(ContractABI, contractAddress);
1818

19-
console.log("Web3", web3);
20-
console.log("Contract address: ", contractAddress);
21-
console.log("Contract deployment: ", contractDeploymentTxLink);
22-
console.log("ABI: \n", ContractABI);
23-
console.log("Contract: \n", Contract);
24-
console.log("Contract methods:", Contract.methods);
19+
// console.log("Web3", web3);
20+
// console.log("Contract address: ", contractAddress);
21+
// console.log("Contract deployment: ", contractDeploymentTxLink);
22+
// console.log("ABI: \n", ContractABI);
23+
// console.log("Contract: \n", Contract);
24+
// console.log("Contract methods:", Contract.methods);
2525

2626
if(typeof window != 'undefined'){
2727
window.Contract = Contract;

src/components/Loader/Loader.jsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react'
2+
import styled, { keyframes } from 'styled-components'
3+
4+
const Spin = keyframes`
5+
0% { transform: rotate(0deg); }
6+
100% { transform: rotate(360deg); }
7+
`
8+
9+
const Spinner = styled.div`
10+
position: absolute;
11+
display: flex;
12+
justify-content: center;
13+
border: 3px solid #f3f3f3;
14+
border-radius: 50%;
15+
border-top: 3px solid #1977F2;
16+
width: ${(props) => props.size ? props.size : "18px"};
17+
height: ${(props) => props.size ? props.size : "18px"};
18+
-webkit-animation: ${Spin} 1s linear infinite;
19+
animation: ${Spin} 1s linear infinite;
20+
`
21+
22+
const Loader = ({size}) => {
23+
return (
24+
<div className='w-10 h-10 flex justify-center' >
25+
<Spinner size={size} />
26+
</div>
27+
)
28+
}
29+
30+
export default Loader

src/components/Navbar/Navbar.jsx

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,54 @@
1+
import Link from "next/link";
12
import { useRouter } from "next/router";
3+
import { toast } from "react-toastify";
4+
import { useMetamaskAuth } from "../../auth/authConfig";
5+
import { LANDING_PAGE, ONBOARDING_ROUTE } from "../../constants/routes";
26

37
const Navbar = () => {
8+
const { connect, isLoggedIn, metaState, isProcessingLogin } = useMetamaskAuth();
9+
const router = useRouter();
10+
11+
function disconnect() {
12+
// Not sure how to do this yet :/
13+
toast("Disconnected 🚧", { type: "error" });
14+
}
415

516
return (
6-
<div className="flex flex-row items-center w-[100%] h-[55px] px-[50px] box-border border-b-[1.2px] font-inter text-[24px] border-[#E1E1E1] text-black cursor-pointer">
17+
<div className="flex flex-row items-center w-[100%] h-[55px] px-[50px] box-border border-b-[1.2px] font-inter text-[24px] border-[#E1E1E1] bg-[#12131A] text-white cursor-pointer">
718
<div className="h-[30px] w-[30px] bg-yellow-200 mr-[15px]"></div>
8-
<span>DeFi</span>
19+
<Link href={LANDING_PAGE}>AppName</Link>
20+
921
<div className="flex flex-row gap-x-[60px] ml-auto text-[14px] font-medium font-inter text-white">
10-
Connect
22+
{!isProcessingLogin ? (
23+
isLoggedIn ? (
24+
<button
25+
className="bg-white py-[8px] px-[24px] rounded-lg text-gray-600"
26+
onClick={disconnect}
27+
>
28+
Disconnect
29+
</button>
30+
) : metaState?.isConnected ? (
31+
<Link
32+
href={ONBOARDING_ROUTE}
33+
className="bg-green-700 py-[8px] px-[24px] rounded-lg text-white"
34+
onClick={connect}
35+
>
36+
Complete Registration
37+
</Link>
38+
) : (
39+
<button
40+
className="bg-blue py-[8px] px-[24px] rounded-lg text-white"
41+
onClick={connect}
42+
>
43+
Connect
44+
</button>
45+
)
46+
) : (
47+
<></>
48+
)}
1149
</div>
1250
</div>
1351
);
1452
};
1553

16-
export default Navbar;
54+
export default Navbar;

src/constants/routes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const LANDING_PAGE = '/';
2+
export const NEW_COMER = LANDING_PAGE;
3+
4+
export const ONBOARDING_ROUTE = 'registration';
5+
export const JUST_LOGGED_IN = 'dashboard'
6+
7+
export const DASHBOARD = 'dashboard'

src/constants/routes.ts

Whitespace-only changes.

src/pages/_app.jsx

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,59 @@ import 'regenerator-runtime/runtime';
33
import { useRouter } from "next/router";
44
import { toast, ToastContainer } from "react-toastify";
55

6+
import { JUST_LOGGED_IN, ONBOARDING_ROUTE } from "../constants/routes";
7+
import { MetamaskAuthProvider } from "../auth/authConfig";
8+
import Navbar from "../components/Navbar/Navbar";
9+
610
import "./styles/globals.css";
711
import "react-toastify/dist/ReactToastify.css";
812
import '@fontsource/roboto/300.css';
913
import '@fontsource/roboto/400.css';
1014
import '@fontsource/roboto/500.css';
1115
import '@fontsource/roboto/700.css';
12-
import Navbar from "../components/Navbar/Navbar";
16+
import { getProfile } from "../apis/user";
1317

1418
export default function App({ Component, pageProps }) {
1519
const { push } = useRouter();
1620
// const {} = useToast();
1721

22+
const TestProps = {
23+
hasAccount: (address) => {
24+
return new Promise((resolve, reject) => {
25+
console.log(`Checking if ${address} has an account ....`);
26+
27+
getProfile(address)
28+
.then(profile => {
29+
console.log("Profile", profile);
30+
resolve({
31+
loggedIn: true,
32+
profile: {
33+
name: profile.name,
34+
address: profile.walletAddress,
35+
}
36+
});
37+
})
38+
.catch(err => {
39+
console.log(err);
40+
resolve({
41+
loggedIn: false,
42+
profile: null
43+
});
44+
});
45+
});
46+
},
47+
onConnected: () => {
48+
// Redirect to onboarding page here
49+
push(ONBOARDING_ROUTE);
50+
},
51+
onLoggedIn: () => {
52+
push(JUST_LOGGED_IN);
53+
},
54+
onCancelledConnection: () => {
55+
toast("Connection request cancelled !", { type: "error" });
56+
}
57+
};
58+
1859
return (
1960
<>
2061
<Head>
@@ -23,8 +64,10 @@ export default function App({ Component, pageProps }) {
2364
<meta name="viewport" content="width=device-width, initial-scale=1" />
2465
<link rel="icon" href="/favicon.ico" />
2566
</Head>
26-
<Navbar />
27-
<Component {...pageProps} />
67+
<MetamaskAuthProvider {...TestProps}>
68+
<Navbar />
69+
<Component {...pageProps} />
70+
</MetamaskAuthProvider>
2871
<ToastContainer />
2972
</>
3073
);

src/pages/dashboard.jsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import { useMetamaskAuth, withAuthenticatedRoute } from "../auth/authConfig";
12
import DashLeft from "../components/DashLeft/DashLeft"
23
import DashRight from "../components/DashRight/DashRight"
4+
import Loader from "../components/Loader/Loader";
35

46
const Dashboard = () => {
7+
const { profile, isProcessingLogin } = useMetamaskAuth();
8+
9+
// if(isProcessingLogin || !profile){
10+
// return <Loader size='80px' />
11+
// }
12+
513
return (
614
<div className="flex flex-row h-[calc(100vh-55px)] bg-[#12131A]">
715
<div className="w-[40%]">
@@ -16,4 +24,5 @@ const Dashboard = () => {
1624
)
1725
}
1826

19-
export default Dashboard
27+
export default Dashboard
28+
// export default withAuthenticatedRoute(Dashboard);

src/pages/dashtest.jsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React, { useEffect } from "react";
2+
import Head from "next/head";
3+
import { useMetamaskAuth, withAuthenticatedRoute } from "../auth/authConfig";
4+
import { CallerFn, SenderFn } from "../apis/factory";
5+
import { toast } from "react-toastify";
6+
7+
const Dashboard = () => {
8+
const { isLoggedIn, isProcessingLogin, profile, refreshAuthStatus } = useMetamaskAuth();
9+
10+
useEffect(() => {
11+
if(!profile) return;
12+
fetchPosts();
13+
// eslint-disable-next-line react-hooks/exhaustive-deps
14+
}, [profile]);
15+
16+
function deleteAccount(){
17+
localStorage.removeItem(profile.address);
18+
refreshAuthStatus();
19+
}
20+
21+
return (
22+
<div className="p-4">
23+
<Head>
24+
<title>Dashboard</title>
25+
</Head>
26+
<div>
27+
<h1 className="text-3xl font-bold">Dashboard</h1>
28+
29+
{isProcessingLogin ? (
30+
<>Loading ....</>
31+
) : (
32+
isLoggedIn &&
33+
profile && (
34+
<div>
35+
Logged in as {profile.name} <br />
36+
Role: {profile.role} <br/>
37+
@ {profile.address}
38+
39+
<br/>
40+
<br/>
41+
<br/>
42+
<button className="bg-red-500 py-[8px] px-[24px] rounded-lg text-white" onClick={deleteAccount}>
43+
Delete my account
44+
</button>
45+
<br/>
46+
</div>
47+
)
48+
)}
49+
</div>
50+
</div>
51+
);
52+
};
53+
54+
export default withAuthenticatedRoute(Dashboard);

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"next-env.d.ts",
2323
"**/*.ts",
2424
"**/*.tsx"
25-
],
25+
, "src/constants/routes.js" ],
2626
"exclude": [
2727
"node_modules"
2828
]

0 commit comments

Comments
 (0)