-
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.
[F] Google OAuth lib parity with investigations-client
AS close to same as can be without migrating from /pages to /app
- Loading branch information
Blake Mason
committed
Feb 27, 2024
1 parent
a2b7ec6
commit 4f7f21f
Showing
8 changed files
with
204 additions
and
49 deletions.
There are no files selected for viewing
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,52 @@ | ||
import PropTypes from "prop-types"; | ||
import { useGoogleLogin } from "@react-oauth/google"; | ||
import { useRouter } from "next/router"; | ||
import { useAuthenticationContext } from "@/contexts/Authentication"; | ||
import SSOButton from "./SSOButton"; | ||
|
||
export default function GoogleSSOButton({ children, ...buttonProps }) { | ||
const { authenticateWithGoogle } = useAuthenticationContext(); | ||
const { query, push, asPath, pathname } = useRouter(); | ||
const goToGoogleSignIn = useGoogleLogin({ | ||
state: asPath, | ||
onSuccess: (response) => { | ||
push( | ||
{ pathname: asPath.split("?")[0], query: { sso: true } }, | ||
undefined, | ||
{ | ||
shallow: true, | ||
} | ||
); | ||
fetch("/api/authGoogle", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ code: response.code }), | ||
}) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
authenticateWithGoogle(data); | ||
}) | ||
.catch(console.error); | ||
}, | ||
onError: (error) => { | ||
console.error(error); | ||
}, | ||
flow: "auth-code", | ||
}); | ||
|
||
return ( | ||
<SSOButton | ||
{...buttonProps} | ||
service="google" | ||
type="button" | ||
onClick={goToGoogleSignIn} | ||
> | ||
{children} | ||
</SSOButton> | ||
); | ||
} | ||
|
||
GoogleSSOButton.propTypes = { | ||
children: PropTypes.node, | ||
service: PropTypes.oneOf(["google", "facebook", "email"]), | ||
}; |
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
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,28 @@ | ||
import { OAuth2Client } from "google-auth-library"; | ||
|
||
const GOOGLE_APP_ID = process.env.NEXT_PUBLIC_GOOGLE_APP_ID; | ||
const GOOGLE_APP_SECRET = process.env.GOOGLE_APP_SECRET; | ||
|
||
const oAuth2Client = new OAuth2Client( | ||
GOOGLE_APP_ID, | ||
GOOGLE_APP_SECRET, | ||
"postmessage" | ||
); | ||
|
||
// exchanges Auth Code for Tokens | ||
async function getTokens(code) { | ||
const { tokens } = await oAuth2Client.getToken(code); | ||
|
||
return tokens; | ||
} | ||
|
||
export default async function handler(req, res) { | ||
try { | ||
const { id_token: idToken } = await getTokens(req.body.code); | ||
// eslint-disable-next-line no-console | ||
console.log("authGoogle token", idToken); | ||
res.status(200).json({ idToken }); | ||
} catch (err) { | ||
res.status(500).json({ error: "failed to load data" }); | ||
} | ||
} |
Oops, something went wrong.