generated from nation3/nation3-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: authenticate with github (#22)
Add `passport-github2` strategy
- Loading branch information
1 parent
b16425b
commit 207c06a
Showing
8 changed files
with
181 additions
and
6 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Menu from "@/components/Menu" | ||
import { NextPage } from "next" | ||
import Link from "next/link" | ||
import { useRouter } from "next/router" | ||
|
||
const GitHubPage: NextPage = () => { | ||
console.info('GitHubPage') | ||
|
||
const router = useRouter() | ||
|
||
return ( | ||
<> | ||
<main className='flex-column lg:flex'> | ||
<Menu /> | ||
|
||
<div className='w-full lg:w-3/4 p-8'> | ||
<div className="flex"> | ||
<div className="font-bold"> | ||
<h2 className="text-2xl text-gray-400"> | ||
Citizen #{router.query.passportId} | ||
</h2> | ||
</div> | ||
</div> | ||
|
||
<div className='mt-8'> | ||
<h2 className="text-2xl">Link My GitHub Account</h2> | ||
<div className="mt-4"> | ||
<Link | ||
href={`/api/${router.query.passportId}/auth/github`} | ||
className="bg-sky-400 hover:bg-sky-500 p-2 px-4 rounded-lg text-white" | ||
> | ||
Authenticate With GitHub | ||
</Link> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
</> | ||
) | ||
} | ||
|
||
export default GitHubPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import nc, { createEdgeRouter } from "next-connect" | ||
import { NextFetchEvent, NextRequest } from "next/server" | ||
const passport = require('passport') | ||
const GitHubStrategy = require('passport-github2').Strategy | ||
|
||
const callbackBaseUrl = process.env['GITHUB_CALLBACK_BASE_URL'] | ||
console.info('callbackBaseUrl:', callbackBaseUrl) | ||
|
||
const callbackUrl = `${callbackBaseUrl}/api/233/auth/github-callback` // TODO: get [passportId] | ||
console.info('callbackUrl:', callbackUrl) | ||
|
||
const router = createEdgeRouter<NextRequest, NextFetchEvent>() | ||
router.use(async (request, event, next) => { | ||
// logging request example | ||
console.log(`${request.method} ${request.url}`); | ||
return next(); | ||
}); | ||
|
||
// Configure strategy | ||
passport.use(new GitHubStrategy( | ||
{ | ||
clientID: process.env['GITHUB_CLIENT_ID'], | ||
clientSecret: process.env['GITHUB_CLIENT_SECRET'], | ||
callbackURL: callbackUrl | ||
}, | ||
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'] | ||
// }) | ||
// ) | ||
|
||
// export default handler |