generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add web3auth and basic route auth guard
- Loading branch information
1 parent
de8a996
commit 8e6ce2d
Showing
9 changed files
with
2,125 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { web3auth } from "~~/auth/web3auth"; | ||
|
||
export default function checkUserAuthentication(path: string) { | ||
console.log("from checkUserAuthentication - Path = ", path); | ||
|
||
const protectedRoutes = [ | ||
"/dapp/dashboard", | ||
"/dapp/welcome", | ||
"/dapp/leads", | ||
"/dapp/settings-team", | ||
"/dapp/calendar", | ||
"/dapp/transactions", | ||
"/dapp/settings-profile", | ||
"/dapp/settings-billing", | ||
"/dapp/charts", | ||
"/dapp/integration", | ||
]; | ||
|
||
if (!web3auth.connected && protectedRoutes.includes(path)) { | ||
return false; | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Web3Auth } from "@web3auth/modal"; | ||
|
||
export const web3auth = new Web3Auth({ | ||
clientId: "BM0SLNkhMCfIygw0Xi79dG6qbWGMN0o0mEeDjRT0dxlP3BEok9pnu5aqxCNfj2TZ9XT7sQaXm0ltuWbCQ1tsRNI", // Get your Client ID from the Web3Auth Dashboard | ||
web3AuthNetwork: "sapphire_devnet", // Web3Auth Network | ||
chainConfig: { | ||
chainNamespace: "eip155", | ||
chainId: "0x13881", // Mumbai | ||
rpcTarget: "https://rpc.ankr.com/polygon_mumbai", | ||
displayName: "Mumbai Testnet", | ||
blockExplorer: "https://mumbai.polygonscan.com/", | ||
ticker: "MATIC", | ||
tickerName: "Polygon", | ||
}, | ||
}); | ||
|
||
export async function web3AuthInit() { | ||
try { | ||
await web3auth.initModal(); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"use client"; | ||
|
||
import React, { useEffect } from "react"; | ||
import { usePathname } from "next/navigation"; | ||
import { useRouter } from "next/router"; | ||
import checkUserAuthentication from "~~/auth/checkUserAuthentication"; | ||
|
||
export default function WatchPathname() { | ||
const pathname = usePathname(); | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
console.log(`Route changed to: ${pathname}`); | ||
if (pathname !== null) { | ||
const userIsAuthenticated = checkUserAuthentication(pathname); | ||
if (!userIsAuthenticated) { | ||
router.replace("/login"); | ||
} | ||
} | ||
}, [pathname, router]); | ||
|
||
return <></>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.