-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(#25): migrated to passport.js
- Loading branch information
1 parent
c36a771
commit 76581df
Showing
14 changed files
with
1,676 additions
and
534 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,89 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { unstable_getServerSession } from "next-auth"; | ||
import { generateRandomString } from "@utils"; | ||
import { NextApiRequest, NextApiResponse, NextApiHandler } from "next"; | ||
import { getServerSession } from "next-auth"; | ||
import { authOptions } from "@pages/api/auth/[...nextauth]"; | ||
|
||
import { providers } from "@services/oauth"; | ||
import actions from "@services/oauth/actions"; | ||
import passport from "passport"; | ||
import nextConnect from "next-connect"; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const session = await unstable_getServerSession(req, res, authOptions); | ||
import StackOverflowProvider from "@services/oauth/providers/stackoverflow"; | ||
import GithubProvider from "@services/oauth/providers/github"; | ||
import WakatimeProvider from "@services/oauth/providers/wakatime"; | ||
|
||
async function handler(req: NextApiRequest, res: NextApiResponse, next: any) { | ||
const session = await getServerSession(req, res, authOptions); | ||
if (!session) return res.redirect("/login"); | ||
|
||
const [action, platform]: string[] = req.query.route as string[]; | ||
|
||
if (req.method !== "GET") return res.status(405).end(); | ||
if (Object.keys(providers).indexOf(platform) === -1) | ||
return res.status(404).send("Not Found"); | ||
|
||
if (action === "callback") { | ||
const provider = providers[platform]; | ||
const params = provider.getTokenParam(provider, req.query); | ||
if (action === "connect") { | ||
return passport.authenticate(platform)(req, res); | ||
} | ||
|
||
try { | ||
const accessToken = await provider.authorization.getToken(params); | ||
if (action === "callback") { | ||
return passport.authenticate( | ||
platform, | ||
{ | ||
failureRedirect: "/login", | ||
}, | ||
async (error: Error | null, data: any) => { | ||
if (error) { | ||
return res.status(500).send(error.message); | ||
} | ||
|
||
const profile = await provider.getProfile(accessToken.token); | ||
if (!profile) return res.redirect("/login"); | ||
await actions.connect({ | ||
token: data.token, | ||
profile: data.profile, | ||
session, | ||
platformCode: platform, | ||
}); | ||
|
||
await actions.signin({ accessToken, session, provider, profile }); | ||
return res.redirect("/login"); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
console.log("Access Token Error", error.message); | ||
return res.send(error.message); | ||
return res.redirect("/"); | ||
} | ||
} | ||
} | ||
|
||
if (action === "connect") { | ||
const provider = providers[platform]; | ||
if (provider.connect_url) { | ||
return res.redirect(provider.connect_url); | ||
} | ||
|
||
const redirect_uri = provider.authorization.authorizeURL({ | ||
redirect_uri: provider.redirect_uri, | ||
scope: provider.scope, | ||
state: generateRandomString(), | ||
}); | ||
return res.redirect(redirect_uri); | ||
)(req, res, next); | ||
} | ||
|
||
if (action === "session") { | ||
try { | ||
const connection = await actions.getConnections({ session, platform }); | ||
const connection = await actions.getConnections({ | ||
session, | ||
platformCode: platform, | ||
}); | ||
if (!connection) { | ||
return res.status(403).send("Forbidden"); | ||
} | ||
return res.json(connection); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
return res.status(404).send(err.message); | ||
} | ||
return res.status(500).send("Internal Server Error"); | ||
} | ||
} | ||
|
||
if (action === "disconnect") { | ||
try { | ||
await actions.disconnect({ session, platformCode: platform }); | ||
return res.redirect("/login"); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
return res.status(404).send(err.message); | ||
} | ||
return res.status(500).send("Internal Server Error"); | ||
} | ||
} | ||
|
||
return res.status(404).send("Not Found"); | ||
} | ||
|
||
passport.use("stackoverflow", StackOverflowProvider); | ||
passport.use("github", GithubProvider); | ||
passport.use("wakatime", WakatimeProvider); | ||
|
||
export default nextConnect() | ||
.use(passport.initialize()) | ||
.use((req: NextApiRequest, res: NextApiResponse, next: any) => { | ||
return handler(req, res, next); | ||
}); |
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
Oops, something went wrong.