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 * add authSlice - set auth vars on login/logout
- Loading branch information
1 parent
de8a996
commit 641159b
Showing
11 changed files
with
2,176 additions
and
45 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,37 @@ | ||
import { PayloadAction, createSlice } from "@reduxjs/toolkit"; | ||
import { IProvider } from "@web3auth/base"; | ||
|
||
export type AuthProvider = IProvider | null; | ||
|
||
export interface AuthRootState { | ||
auth: { | ||
isConnected: boolean; | ||
provider: AuthProvider; | ||
}; | ||
} | ||
|
||
interface AuthState { | ||
isConnected: boolean; | ||
provider: AuthProvider; | ||
} | ||
|
||
export const authSlice = createSlice({ | ||
name: "auth", | ||
initialState: { | ||
isConnected: false, | ||
provider: null as AuthProvider, | ||
}, | ||
reducers: { | ||
setAuthProvider: (state: AuthState, action: PayloadAction<{ provider: AuthProvider }>) => { | ||
state.provider = action.payload.provider; | ||
}, | ||
|
||
setIsConnected: (state: AuthState, action: PayloadAction<{ isConnected: boolean }>) => { | ||
state.isConnected = action.payload.isConnected; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { setAuthProvider, setIsConnected } = authSlice.actions; | ||
|
||
export default authSlice.reducer; |
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
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.