From f0e4f7893b2c6e35403511a229df93f29676426c Mon Sep 17 00:00:00 2001 From: Aahna Ashina <95955389+aahna-ashina@users.noreply.github.com> Date: Fri, 22 Sep 2023 16:36:18 +0700 Subject: [PATCH 1/7] feat: github oauth (#22) close #22 --- .env.local.sample | 2 +- src/components/ProfileDetailsGitHub.tsx | 3 +-- .../api/[passportId]/auth/github-callback.ts | 21 +++++++++++++++++++ src/pages/api/[passportId]/auth/github.ts | 11 +++++----- 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 src/pages/api/[passportId]/auth/github-callback.ts diff --git a/.env.local.sample b/.env.local.sample index bb1a700..149f0ea 100644 --- a/.env.local.sample +++ b/.env.local.sample @@ -1,3 +1,3 @@ GITHUB_CALLBACK_BASE_URL=http://localhost:3000 -GITHUB_CLIENT_ID= +GITHUB_CLIENT_ID=a01fc1f8afba1c7b4be9 GITHUB_CLIENT_SECRET= diff --git a/src/components/ProfileDetailsGitHub.tsx b/src/components/ProfileDetailsGitHub.tsx index f2477eb..02e91f0 100644 --- a/src/components/ProfileDetailsGitHub.tsx +++ b/src/components/ProfileDetailsGitHub.tsx @@ -29,8 +29,7 @@ export default function ProfileDetailsGitHub({ citizen }: any) { return ( <> Not linked - - {/* */} + Link my GitHub account 🔗 diff --git a/src/pages/api/[passportId]/auth/github-callback.ts b/src/pages/api/[passportId]/auth/github-callback.ts new file mode 100644 index 0000000..4b32814 --- /dev/null +++ b/src/pages/api/[passportId]/auth/github-callback.ts @@ -0,0 +1,21 @@ +import nc from "next-connect" +const passport = require('passport') + +const failurePath = `/233/auth/github` // TODO: get [passportId] +console.info('failurePath:', failurePath) + +// Handle GitHub response +const handler = nc() + .get( + passport.authenticate('github', { + failureRedirect: failurePath + }), + (req, res) => { + // Successful authentication, redirect to /[passportId]/auth/github-success + // TODO + console.info('req:', req) + console.info('res:', res) + } + ) + +export default handler diff --git a/src/pages/api/[passportId]/auth/github.ts b/src/pages/api/[passportId]/auth/github.ts index be9e6aa..478467e 100644 --- a/src/pages/api/[passportId]/auth/github.ts +++ b/src/pages/api/[passportId]/auth/github.ts @@ -1,7 +1,12 @@ import nc from "next-connect" +import { useRouter } from "next/router" const passport = require('passport') const GitHubStrategy = require('passport-github2').Strategy +// const router = useRouter() +// const passportId = router.query.passportId +// console.info('passportId:', passportId) + // Configure strategy const callbackBaseUrl = process.env['GITHUB_CALLBACK_BASE_URL'] console.info('callbackBaseUrl:', callbackBaseUrl) @@ -16,9 +21,7 @@ passport.use(new GitHubStrategy( function(accessToken: any, refreshToken: any, profile: any, done: any) { console.info('accessToken:', accessToken) console.info('refreshToken:', refreshToken) - console.info('profile:', profile) console.info('profile.username:', profile.username) - console.info('done:', done) return done(null, profile) } )) @@ -26,9 +29,7 @@ passport.use(new GitHubStrategy( // Redirect to GitHub authentication const handler = nc() .get( - passport.authenticate('github', { - scope: ['user:email'] - }) + passport.authenticate('github', {}) ) export default handler From 7d13d01b5fe1ee07bb169bc3986555e4f58d4330 Mon Sep 17 00:00:00 2001 From: Aahna Ashina <95955389+aahna-ashina@users.noreply.github.com> Date: Fri, 22 Sep 2023 16:41:44 +0700 Subject: [PATCH 2/7] feat: github oauth disable session (#22) Fix `Login sessions require session support. Did you forget to use `express-session` middleware?` --- src/pages/api/[passportId]/auth/github-callback.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/api/[passportId]/auth/github-callback.ts b/src/pages/api/[passportId]/auth/github-callback.ts index 4b32814..c6a22dc 100644 --- a/src/pages/api/[passportId]/auth/github-callback.ts +++ b/src/pages/api/[passportId]/auth/github-callback.ts @@ -8,6 +8,7 @@ console.info('failurePath:', failurePath) const handler = nc() .get( passport.authenticate('github', { + session: false, failureRedirect: failurePath }), (req, res) => { From 11a194c4033d29d2b05be57ea6c4fd92fafbd327 Mon Sep 17 00:00:00 2001 From: Aahna Ashina <95955389+aahna-ashina@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:01:02 +0700 Subject: [PATCH 3/7] feat: display passport status --- .../ProfileDetailsPassportStatus.tsx | 47 +++++++++++++++++++ src/pages/[passportId].tsx | 5 ++ 2 files changed, 52 insertions(+) create mode 100644 src/components/ProfileDetailsPassportStatus.tsx diff --git a/src/components/ProfileDetailsPassportStatus.tsx b/src/components/ProfileDetailsPassportStatus.tsx new file mode 100644 index 0000000..876ab8b --- /dev/null +++ b/src/components/ProfileDetailsPassportStatus.tsx @@ -0,0 +1,47 @@ +import { useContractRead } from "wagmi" +import LoadingIndicator from "./LoadingIndicator" +import VotingEscrow from "../../abis/VotingEscrow.json" +import { useIsMounted } from "@/hooks/useIsMounted" +import Link from "next/link" +import { formatEther } from "viem" + +export default function ProfileDetailsPassportStatus({ citizen }: any) { + console.info('ProfileDetailsPassportStatus') + + const { data, isError, isLoading } = useContractRead({ + address: '0xf7def1d2fbda6b74bee7452fdf7894da9201065d', + abi: VotingEscrow.abi, + functionName: 'balanceOf', + args: [ citizen.ownerAddress ] + }) + console.info('data:', data) + console.info('isError:', isError) + console.info('isLoading:', isLoading) + + if (!useIsMounted() || isLoading) { + return + } else { + let veNationBalance: any = null + if (data) { + veNationBalance = data + } + console.info('veNationBalance:', veNationBalance) + const veNationBalanceNumber: number = Number(formatEther(veNationBalance)) + console.info('veNationBalanceNumber:', veNationBalanceNumber) + if (veNationBalanceNumber < 1.5) { + return ( + <> + + EXPIRED ⌛ + + + ) + } else { + return ( + + ACTIVE 🥳 + + ) + } + } +} diff --git a/src/pages/[passportId].tsx b/src/pages/[passportId].tsx index 5604ea3..6281199 100644 --- a/src/pages/[passportId].tsx +++ b/src/pages/[passportId].tsx @@ -15,6 +15,7 @@ import VeNationLockDetails from '@/components/VeNationLockDetails' import ProfileDetailsGitHub from '@/components/ProfileDetailsGitHub' import Link from 'next/link' import Head from 'next/head' +import ProfileDetailsPassportStatus from '@/components/ProfileDetailsPassportStatus' const Chart = dynamic(() => import('react-apexcharts'), { ssr: false }) export default function ProfilePage({ citizen, nationCred, veNation, dework, sourceCred }: any) { @@ -86,6 +87,10 @@ export default function ProfilePage({ citizen, nationCred, veNation, dework, sou {citizen.ownerAddress} +
  • + Passport status
    + +
  • GitHub account
    From ee57fcd1f898a21c611354a946ebe4f06f7a9e69 Mon Sep 17 00:00:00 2001 From: Aahna Ashina <95955389+aahna-ashina@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:30:08 +0700 Subject: [PATCH 4/7] style: adjust vote escrow location --- src/pages/[passportId].tsx | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/pages/[passportId].tsx b/src/pages/[passportId].tsx index 6281199..84bc73b 100644 --- a/src/pages/[passportId].tsx +++ b/src/pages/[passportId].tsx @@ -120,6 +120,24 @@ export default function ProfilePage({ citizen, nationCred, veNation, dework, sou +
    +

    🗳️ Voting Escrow

    +
    + {router.isFallback ? ( + + ) : ( + + )} +
    +
    + {router.isFallback ? ( + + ) : ( + + )} +
    +
    +

    🎗️ NationCred

    @@ -174,24 +192,6 @@ export default function ProfilePage({ citizen, nationCred, veNation, dework, sou )}
    - -
    -

    🗳️ Voting Escrow

    -
    - {router.isFallback ? ( - - ) : ( - - )} -
    -
    - {router.isFallback ? ( - - ) : ( - - )} -
    -
    From 19abb53c6c725ce0171f4d32fea5d2c84bec21f4 Mon Sep 17 00:00:00 2001 From: Aahna Ashina <95955389+aahna-ashina@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:46:16 +0700 Subject: [PATCH 5/7] refactor: passport details --- ...leDetailsPassportStatus.tsx => PassportStatus.tsx} | 4 ++-- src/pages/[passportId].tsx | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) rename src/components/{ProfileDetailsPassportStatus.tsx => PassportStatus.tsx} (92%) diff --git a/src/components/ProfileDetailsPassportStatus.tsx b/src/components/PassportStatus.tsx similarity index 92% rename from src/components/ProfileDetailsPassportStatus.tsx rename to src/components/PassportStatus.tsx index 876ab8b..49839e2 100644 --- a/src/components/ProfileDetailsPassportStatus.tsx +++ b/src/components/PassportStatus.tsx @@ -5,8 +5,8 @@ import { useIsMounted } from "@/hooks/useIsMounted" import Link from "next/link" import { formatEther } from "viem" -export default function ProfileDetailsPassportStatus({ citizen }: any) { - console.info('ProfileDetailsPassportStatus') +export default function PassportStatus({ citizen }: any) { + console.info('PassportStatus') const { data, isError, isLoading } = useContractRead({ address: '0xf7def1d2fbda6b74bee7452fdf7894da9201065d', diff --git a/src/pages/[passportId].tsx b/src/pages/[passportId].tsx index 84bc73b..b2d09b6 100644 --- a/src/pages/[passportId].tsx +++ b/src/pages/[passportId].tsx @@ -15,7 +15,7 @@ import VeNationLockDetails from '@/components/VeNationLockDetails' import ProfileDetailsGitHub from '@/components/ProfileDetailsGitHub' import Link from 'next/link' import Head from 'next/head' -import ProfileDetailsPassportStatus from '@/components/ProfileDetailsPassportStatus' +import PassportStatus from '@/components/PassportStatus' const Chart = dynamic(() => import('react-apexcharts'), { ssr: false }) export default function ProfilePage({ citizen, nationCred, veNation, dework, sourceCred }: any) { @@ -87,10 +87,6 @@ export default function ProfilePage({ citizen, nationCred, veNation, dework, sou {citizen.ownerAddress}
  • -
  • - Passport status
    - -
  • GitHub account
    @@ -126,7 +122,10 @@ export default function ProfilePage({ citizen, nationCred, veNation, dework, sou {router.isFallback ? ( ) : ( - + <> +
    + Passport status: + )}
    From 59c6c26b8331eed6bce42c92cbaf2497085c9225 Mon Sep 17 00:00:00 2001 From: Aahna Ashina <95955389+aahna-ashina@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:49:59 +0700 Subject: [PATCH 6/7] refactor: passport details --- src/components/PassportStatus.tsx | 10 +++++++--- src/pages/[passportId].tsx | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/components/PassportStatus.tsx b/src/components/PassportStatus.tsx index 49839e2..618dbb5 100644 --- a/src/components/PassportStatus.tsx +++ b/src/components/PassportStatus.tsx @@ -31,6 +31,7 @@ export default function PassportStatus({ citizen }: any) { if (veNationBalanceNumber < 1.5) { return ( <> + Passport status: EXPIRED ⌛ @@ -38,9 +39,12 @@ export default function PassportStatus({ citizen }: any) { ) } else { return ( - - ACTIVE 🥳 - + <> + Passport status: + + ACTIVE 🥳 + + ) } } diff --git a/src/pages/[passportId].tsx b/src/pages/[passportId].tsx index b2d09b6..54ec159 100644 --- a/src/pages/[passportId].tsx +++ b/src/pages/[passportId].tsx @@ -124,7 +124,7 @@ export default function ProfilePage({ citizen, nationCred, veNation, dework, sou ) : ( <>
    - Passport status: + )}
    From 50eecb9fc270bbf3d410a29fd0a0a6db43482adf Mon Sep 17 00:00:00 2001 From: Aahna Ashina <95955389+aahna-ashina@users.noreply.github.com> Date: Wed, 27 Dec 2023 16:14:41 +0700 Subject: [PATCH 7/7] feat: auth (#22) #22 --- .../api/[passportId]/auth/github-callback.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/pages/api/[passportId]/auth/github-callback.ts b/src/pages/api/[passportId]/auth/github-callback.ts index c6a22dc..d39f6ff 100644 --- a/src/pages/api/[passportId]/auth/github-callback.ts +++ b/src/pages/api/[passportId]/auth/github-callback.ts @@ -11,11 +11,16 @@ const handler = nc() session: false, failureRedirect: failurePath }), - (req, res) => { - // Successful authentication, redirect to /[passportId]/auth/github-success - // TODO - console.info('req:', req) - console.info('res:', res) + (req: any, res: any) => { + // Successful authentication + console.info('Successful authentication') + + // Get GitHub username + const username: string = req.user.username + console.info('username', username) + + // Redirect to /[passportId]/auth/github-success + res.redirect(`/233/auth/github-success?username=${username}`) } )