www/apps/resources/app/commerce-modules/auth/auth-providers/google/page.mdx
import { Table, Prerequisites } from "docs-ui"
export const metadata = {
title: Google Auth Module Provider,
}
In this guide, you’ll learn about the Google Auth Module Provider and how to install and use it in the Auth Module.
The Google Auth Module Provider allows you to authenticate users with their Google account.
<Prerequisites items={[ { text: "Create a project in Google Cloud.", link: "https://cloud.google.com/resource-manager/docs/creating-managing-projects" }, { text: "Create Oauth client ID credentials. When setting the Redirect Uri, set it to a URL in your frontend (for example, storefront) that later uses Medusa's callback route to validate the authentication.", link: "https://developers.google.com/identity/protocols/oauth2/web-server#creatingcred" } ]} />
To use the Google Auth Module Provider, add the module to the array of providers passed to the Auth Module in your medusa-config.ts:
import { Modules, ContainerRegistrationKeys } from "@medusajs/framework/utils"
// ...
module.exports = defineConfig({
// ...
modules: [
// ...
{
resolve: "@medusajs/medusa/auth",
dependencies: [Modules.CACHE, ContainerRegistrationKeys.LOGGER],
options: {
providers: [
// other providers...
{
resolve: "@medusajs/medusa/auth-emailpass",
id: "emailpass",
},
{
resolve: "@medusajs/medusa/auth-google",
id: "google",
options: {
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackUrl: process.env.GOOGLE_CALLBACK_URL,
},
},
],
},
},
],
})
Make sure to add the necessary environment variables for the above options in .env:
GOOGLE_CLIENT_ID=<YOUR_GOOGLE_CLIENT_ID>
GOOGLE_CLIENT_SECRET=<YOUR_GOOGLE_CLIENT_SECRET>
GOOGLE_CALLBACK_URL=<YOUR_GOOGLE_CALLBACK_URL>
`clientId`
</Table.Cell>
<Table.Cell>
A string indicating the [Google API Client ID](https://developers.google.com/identity/oauth2/web/guides/get-google-api-clientid).
</Table.Cell>
<Table.Cell>
Yes
</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>
`clientSecret`
</Table.Cell>
<Table.Cell>
A string indicating the [Google Client Secret](https://support.google.com/cloud/answer/6158849?hl=en#zippy=%2Cstep-create-a-new-client-secret).
</Table.Cell>
<Table.Cell>
Yes
</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>
`callbackUrl`
</Table.Cell>
<Table.Cell>
A string indicating the URL to redirect to in your frontend after the user completes their authentication in Google.
At this URL, the frontend will receive a `code` query parameter. It then sends that `code` query parameter to the Medusa application's `/auth/{actor_type}/google/callback` route.
</Table.Cell>
<Table.Cell>
Yes
</Table.Cell>
</Table.Row>
</Table.Body>
</Table>In many cases, you may have different callback URLs for actor types. For example, you may redirect admin users to a different URL than customers after authentication.
The Authenticate or Login API Route can accept a callback_url body parameter to override the provider's callbackUrl option. Learn more in this documentation.