-
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.
- Loading branch information
1 parent
ab0fa37
commit 784a398
Showing
11 changed files
with
155 additions
and
23 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
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,13 @@ | ||
import { GoogleOAuthProvider } from "@react-oauth/google"; | ||
|
||
export default function AuthLayout({ children }: Readonly<Props>) { | ||
return ( | ||
<GoogleOAuthProvider clientId={process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID}> | ||
{children} | ||
</GoogleOAuthProvider> | ||
); | ||
} | ||
|
||
type Props = { | ||
children: React.ReactNode | ||
} |
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,73 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { formatErrors } from "@/lib/utils/validator"; | ||
import { z } from "zod"; | ||
import { auth, OAuth2Client } from 'google-auth-library' | ||
import { usersCollection } from "@/lib/db/user_collection"; | ||
import { cartsCollection } from "@/lib/db/cart_collection"; | ||
import { signToken } from "@/lib/utils/jwt"; | ||
|
||
const GoogleLoginSchema = z.object({ | ||
authCode: z.string({ message: "Google Token is required" }) | ||
}) | ||
|
||
export async function POST(request: NextRequest) { | ||
try { | ||
const body = await request.json(); | ||
|
||
const { success, data, error } = GoogleLoginSchema.safeParse(body); | ||
|
||
if (!success) { | ||
return NextResponse.json({ errors: formatErrors(error) }, { status: 400 }); | ||
} | ||
|
||
const client = new OAuth2Client( | ||
process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID, | ||
process.env.GOOGLE_CLIENT_SECRET, | ||
'postmessage' | ||
); | ||
|
||
const { tokens } = await client.getToken(data.authCode) | ||
|
||
if (!tokens.id_token) { | ||
return NextResponse.json({ errors: "Invalid google token" }, { status: 400 }); | ||
} | ||
|
||
const ticket = await client.verifyIdToken({ idToken: tokens.id_token }); | ||
const payload = ticket.getPayload(); | ||
|
||
if (!payload) { | ||
return NextResponse.json({ errors: "Invalid google token" }, { status: 400 }); | ||
} | ||
|
||
let user = await usersCollection.findOne({ email: payload.email }); | ||
if (!user) { | ||
const newUser = { | ||
name: payload.name as string, | ||
email: payload.email as string, | ||
password: "", // just empty password, because using google login | ||
googleId: payload.sub as string, | ||
createdAt: new Date(), | ||
} | ||
const result = await usersCollection.insertOne(newUser); | ||
|
||
await cartsCollection.insertOne({ | ||
userId: result.insertedId, | ||
items: [], | ||
}); | ||
|
||
user = { | ||
_id: result.insertedId, | ||
...newUser | ||
} | ||
} | ||
|
||
// Generate JWT token | ||
const token = await signToken({ sub: user._id.toString() }); | ||
|
||
return NextResponse.json({ token }, { status: 200 }); | ||
} catch (error) { | ||
console.log(error); | ||
|
||
return NextResponse.json({ errors: ["An unexpected error occurred"] }, { status: 500 }); | ||
} | ||
} |
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,23 @@ | ||
'use client' | ||
import { googleLogin } from "@/lib/actions/users"; | ||
import { GoogleOAuthProvider, useGoogleLogin } from "@react-oauth/google"; | ||
import { FcGoogle } from "react-icons/fc"; | ||
|
||
export default function GoogleLogin() { | ||
const login = useGoogleLogin({ | ||
flow: 'auth-code', | ||
onSuccess: tokenResponse => { | ||
console.log(JSON.stringify(tokenResponse, null, 2)) | ||
googleLogin(tokenResponse.code) | ||
}, | ||
}); | ||
|
||
return ( | ||
<GoogleOAuthProvider clientId={process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID}> | ||
<button onClick={() => login()} className="w-full flex items-center justify-center py-3 px-4 border border-gray-300 rounded-lg hover:bg-gray-50 transition duration-300"> | ||
<FcGoogle className="mr-2" size={20} /> | ||
<span className="text-sm font-medium">Google</span> | ||
</button> | ||
</GoogleOAuthProvider> | ||
) | ||
} |
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