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] 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