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 6 commits
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
23,215 changes: 16,017 additions & 7,198 deletions package-lock.json

Large diffs are not rendered by default.

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
90 changes: 90 additions & 0 deletions packages/ui/components/auth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { AppConfig, UserSession, showConnect } from "@stacks/connect";
// import { Storage } from '@stacks/storage';
import { StacksMainnet, StacksTestnet } from "@stacks/network";
import {
callReadOnlyFunction,
cvToJSON,
standardPrincipalCV,
} from "@stacks/transactions";

// Initialize Gaia hub permissions for the user
const appConfig = new AppConfig(["store_write", "publish_data"]);

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 (process.env.IS_NETWORK_MAINNET === "true") return new StacksMainnet();
else return new StacksTestnet();
}

export function getMyStxAddress(): string {
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 (process.env.IS_NETWORK_MAINNET === "true") return "mainnet";
else return "testnet";
}

export function authenticate(): void {
showConnect({
appDetails: {
name: "daoOS",
// TODO: add daoOS logo over here
icon: "../public/favicon.ico",
},
redirectTo: "/",
onFinish: () => {
// TODO: Enter miami vice's contract address
const contractAddress: string = "SP98329831323123";
// TODO: Enter miami vice's contract name
const contractName: string = "miami-vice-v1";

const functionName: string = "is-dao-member";
const stxAddress: string = getMyStxAddress();
const principalArg = standardPrincipalCV(stxAddress);

const options = {
contractAddress,
contractName,
functionName,
functionArgs: [principalArg],
network: networkType(),
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);

// TODO: check that the right type is compared
if (jsonValue.type === "(response bool)") {
// TODO: console log and check whether the right argument is used as boolean
const isMember: boolean = jsonValue.value["is-member"].value;

if (isMember) {
// Allow access to the miami dashboard
// TODO: add redirect to miami dashboard
window.location.replace(window.location.origin + "/");
} else {
// Deny access
// TODO: add redirect to register page
window.location.replace(window.location.origin + "/register");
}
}
})
.catch((error) => {
// TODO: any error handling such as the contract or the function doesn't exist
console.log(error.message);
});
*/
},
userSession: userSession,
});
}
Loading