Skip to content

Commit

Permalink
feat: add Moralis as an server infrastructure and wallet authenticati…
Browse files Browse the repository at this point in the history
…on method
  • Loading branch information
damla committed Apr 19, 2022
1 parent b3f2753 commit 0fac4c7
Show file tree
Hide file tree
Showing 8 changed files with 3,990 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SERVER_URL=
APP_ID=
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- [Yarn](https://yarnpkg.com)
- [NextJS](https://nextjs.org)
- [TypeScript](https://www.typescriptlang.org)
- [Moralis](https://moralis.io)
- [web3auth](https://web3auth.io)
- [tailwindcss](https://tailwindcss.com)
- [Husky](https://github.com/typicode/husky)
- [Lint-staged](https://github.com/okonet/lint-staged)
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
"dependencies": {
"@headlessui/react": "^1.5.0",
"@heroicons/react": "^1.0.6",
"@walletconnect/web3-provider": "^1.7.7",
"@web3auth/web3auth": "^0.9.2",
"magic-sdk": "^8.1.0",
"moralis": "^1.5.9",
"next": "12.1.5",
"react": "18.0.0",
"react-dom": "18.0.0"
"react-dom": "18.0.0",
"react-moralis": "^1.3.5"
},
"devDependencies": {
"@types/node": "17.0.24",
Expand Down
14 changes: 14 additions & 0 deletions src/components/layouts/Dashboard/modules/Nav/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Disclosure } from '@headlessui/react';
import { MenuIcon, XIcon } from '@heroicons/react/outline';
import Image from 'next/image';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { useEffect } from 'react';
import { useMoralis } from 'react-moralis';

const Nav = () => {
const user = {
Expand All @@ -21,6 +24,14 @@ const Nav = () => {
// { name: 'Settings', href: '#' },
{ name: 'Sign out', href: '#' },
];

const { isAuthenticated, logout } = useMoralis();
const router = useRouter();

useEffect(() => {
if (!isAuthenticated) router.replace('/');
}, [isAuthenticated]);

return (
<Disclosure as="nav" className="bg-gray-800">
{({ open }) => (
Expand All @@ -35,6 +46,9 @@ const Nav = () => {
</a>
</Link>
</div>
<button className="hover:underline" onClick={logout}>
Sign Out
</button>
<div className="hidden md:block">
<div className="ml-10 flex items-baseline space-x-4">
{navigation.map((item) => (
Expand Down
19 changes: 18 additions & 1 deletion src/components/layouts/Landing/modules/Nav/Nav.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { useRouter } from 'next/router';
import { useEffect } from 'react';
import { useMoralis } from 'react-moralis';

const Nav = () => {
// const navigation = [
// { name: 'Profile', href: '#' },
// { name: 'Other', href: '#' },
// ];
const { isAuthenticated, authenticate, logout } = useMoralis();
const router = useRouter();

useEffect(() => {
if (isAuthenticated) router.replace('/profile');
}, [isAuthenticated]);

return (
<nav className="container flex min-w-full items-center justify-between p-7 md:px-40">
<span className=" text-gray-900 text-xl font-bold hover:cursor-default">
NextJS Starter
</span>
<button className="hover:underline">Log In</button>
<button
className="hover:underline"
onClick={() =>
authenticate({ signingMessage: 'Authorize linking of your wallet' })
}
>
Sign In
</button>
</nav>
);
};
Expand Down
15 changes: 14 additions & 1 deletion src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import type { AppProps } from 'next/app';
import 'tailwindcss/tailwind.css';
import { MoralisProvider } from 'react-moralis';

const SERVER_URL = process.env.NEXT_PUBLIC_SERVER_URL;
const APP_ID = process.env.NEXT_PUBLIC_APP_ID;

function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
if (!APP_ID || !SERVER_URL)
throw new Error(
'Missing Moralis Application ID or Server URL. Make sure to set your .env file.'
);

return (
<MoralisProvider serverUrl={SERVER_URL} appId={APP_ID}>
<Component {...pageProps} />
</MoralisProvider>
);
}

export default MyApp;
1 change: 1 addition & 0 deletions src/utils/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const classNames = (...classes: string[]) => {
// to merge conditional classes in styling
return classes.filter(Boolean).join(' ');
};

Expand Down
Loading

0 comments on commit 0fac4c7

Please sign in to comment.