Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet auth #81

Open
wants to merge 7 commits into
base: member-list-ui
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/ui/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IS_NETWORK_MAINNET="true";
suleman-mahmood marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 4 additions & 1 deletion packages/ui/components/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface DrawerProps {
expanded?: boolean;
handleCloseClick?: () => void;
closeDrawer: () => void;
handleLogout?: () => void;
suleman-mahmood marked this conversation as resolved.
Show resolved Hide resolved
}

function Drawer({
Expand All @@ -26,6 +27,7 @@ function Drawer({
expanded,
handleCloseClick,
closeDrawer,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can set a default value here of closeDrawer=()=>{} so we don't need to pass in an empty function in our other components when we want to use Drawer

handleLogout
}: DrawerProps) {
return (
<div
Expand Down Expand Up @@ -75,8 +77,9 @@ function Drawer({
</NavLink>
<NavLink
icon={<ExitToAppIcon className={styles.navIcon} />}
href="/logout"
href="#"
closeDrawer={closeDrawer}
handleLogout={handleLogout}
>
Log out
</NavLink>
Expand Down
16 changes: 15 additions & 1 deletion packages/ui/components/MainNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@ import React from "react";

import styles from "./MainNav.module.css";
import Drawer from "./Drawer";
import { userSession } from "./auth";

interface MainNavProps {
children: React.ReactNode;
className?: string;
}

function MainNav({ children, className }: MainNavProps) {

const handleLogout = () => {
// Sign the user out
userSession.signUserOut();

// TODO: Redirect the user to the home/landing page
window.location.href = "/login";
}

return (
<div className={styles.contentContainer}>
{/* Need to provide closeDrawer since it's a required prop for nested components
even though it's not used in our destkop nav. This way we avoid adding conditional
logic to the NavLink component*/}
<Drawer className={className} closeDrawer={() => {}} />
<Drawer
className={className}
closeDrawer={() => {}}
handleLogout={handleLogout}
/>
<main className={styles.mainContent}>{children}</main>
</div>
);
Expand Down
10 changes: 10 additions & 0 deletions packages/ui/components/MainNavMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import styles from "./MainNavMobile.module.css";
import transitionStyles from "./transitions.module.css";
import Drawer from "./Drawer";
import { useRef } from "react";
import { userSession } from "./auth";

interface MainNavMobileProps {
children: React.ReactNode;
Expand All @@ -22,6 +23,14 @@ function MainNavMobile({ children }: MainNavMobileProps) {
}
};

const handleLogout = () => {
// Sign the user out
userSession.signUserOut();

// TODO: Redirect the user to the home/landing page
window.location.href = "/login";
}

suleman-mahmood marked this conversation as resolved.
Show resolved Hide resolved
const ref = useRef(null);
useClickAway(ref, () => {
setExpanded(false);
Expand Down Expand Up @@ -63,6 +72,7 @@ function MainNavMobile({ children }: MainNavMobileProps) {
expanded={expanded}
handleCloseClick={() => setExpanded(false)}
closeDrawer={closeDrawer}
handleLogout={handleLogout}
/>
</CSSTransition>
</div>
Expand Down
14 changes: 12 additions & 2 deletions packages/ui/components/NavLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ interface NavLinkProps {
icon: React.ReactElement;
href: string;
closeDrawer: () => void;
handleLogout?: () => void;
suleman-mahmood marked this conversation as resolved.
Show resolved Hide resolved
children: React.ReactNode;
}

function NavLink({ icon, href, closeDrawer, children }: NavLinkProps) {
function NavLink({ icon, href, closeDrawer, handleLogout, children }: NavLinkProps) {

const handleClick = () => {
closeDrawer();

// If the handleLogout function is passed then call it
if(handleLogout)
handleLogout();
}

return (
<div className={styles.subNavDiv}>
<Link href={href}>
<a className={styles.navLink} onClick={closeDrawer}>
<a className={styles.navLink} onClick={handleClick}>
{icon}
{children}
</a>
Expand Down
23 changes: 9 additions & 14 deletions packages/ui/components/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,22 @@ import {
// Initialize Gaia hub permissions for the user
const appConfig = new AppConfig(["store_write", "publish_data"]);

// Set this to true if you want to use Mainnet
// Using a boolean helps to switch the entire application to mainnet
// or testnet with only changing a single value
const isNetworkMainnet = true;

export const userSession = new UserSession({ appConfig });
// export const storage = new Storage({ userSession });
suleman-mahmood marked this conversation as resolved.
Show resolved Hide resolved

// Return the network type object for the current network in use
export function networkType() {
if (isNetworkMainnet) return new StacksMainnet();
if (process.env.IS_NETWORK_MAINNET === "true") return new StacksMainnet();
else return new StacksTestnet();
}

export function getMyStxAddress(): string {
if (isNetworkMainnet) return getUserData().profile.stxAddress.mainnet;
else return getUserData().profile.stxAddress.testnet;
if (process.env.IS_NETWORK_MAINNET === "true") return userSession.loadUserData().profile.stxAddress.mainnet;
else return userSession.loadUserData().profile.stxAddress.testnet;
}

export function getNetworkName(): string {
if (isNetworkMainnet) return "mainnet";
if (process.env.IS_NETWORK_MAINNET === "true") return "mainnet";
else return "testnet";
}

Expand Down Expand Up @@ -61,6 +56,9 @@ export function authenticate(): void {
senderAddress: stxAddress,
};

// TODO: Remove this multiline comment when the testnet contract is deployed
// and resolve the rest of the TODO in this comment block
/*
callReadOnlyFunction(options)
suleman-mahmood marked this conversation as resolved.
Show resolved Hide resolved
.then((clarityValue) => {
const jsonValue = cvToJSON(clarityValue);
Expand All @@ -85,11 +83,8 @@ export function authenticate(): void {
// TODO: any error handling such as the contract or the function doesn't exist
console.log(error.message);
});
*/
},
userSession: userSession,
});
}

export function getUserData() {
return userSession.loadUserData();
}
}