Skip to content

Commit

Permalink
feat: github oauth (#22)
Browse files Browse the repository at this point in the history
close #22
  • Loading branch information
aahna-ashina committed Sep 22, 2023
1 parent 491fbb4 commit f0e4f78
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .env.local.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
GITHUB_CALLBACK_BASE_URL=http://localhost:3000
GITHUB_CLIENT_ID=
GITHUB_CLIENT_ID=a01fc1f8afba1c7b4be9
GITHUB_CLIENT_SECRET=
3 changes: 1 addition & 2 deletions src/components/ProfileDetailsGitHub.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export default function ProfileDetailsGitHub({ citizen }: any) {
return (
<>
<code>Not linked</code>
<Link href={`https://etherscan.io/address/0xb989c0c17a3bce679d7586d9e55b6eab11c18687#writeContract#F1`} target="_blank" className="border rounded-full px-2 ml-8 font-bold text-transparent bg-clip-text bg-gradient-to-br from-sky-400 to-green-400">
{/* <Link href={`/${citizen.passportId}/auth/github`} className="border rounded-full px-2 ml-8 font-bold text-transparent bg-clip-text bg-gradient-to-br from-sky-400 to-green-400"> */}
<Link href={`/${citizen.passportId}/auth/github`} className="border rounded-full px-2 ml-8 font-bold text-transparent bg-clip-text bg-gradient-to-br from-sky-400 to-green-400">
Link my GitHub account 🔗
</Link>
</>
Expand Down
21 changes: 21 additions & 0 deletions src/pages/api/[passportId]/auth/github-callback.ts
Original file line number Diff line number Diff line change
@@ -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
11 changes: 6 additions & 5 deletions src/pages/api/[passportId]/auth/github.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -16,19 +21,15 @@ 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)
}
))

// Redirect to GitHub authentication
const handler = nc()
.get(
passport.authenticate('github', {
scope: ['user:email']
})
passport.authenticate('github', {})
)

export default handler

0 comments on commit f0e4f78

Please sign in to comment.