mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 20:27:44 +00:00
64 lines
2.4 KiB
Plaintext
64 lines
2.4 KiB
Plaintext
---
|
|
title: Google
|
|
description: Google Provider
|
|
---
|
|
|
|
<Steps>
|
|
<Step>
|
|
### Get your Google credentials
|
|
To use Google as a social provider, you need to get your Google credentials. You can get them by creating a new project in the [Google Cloud Console](https://console.cloud.google.com/apis/dashboard).
|
|
</Step>
|
|
|
|
<Step>
|
|
### Configure the provider
|
|
To configure the provider, you need to import the provider and pass it to the `socialProvider` option of the auth instance.
|
|
|
|
```ts title="auth.ts" twoslash
|
|
const process = {
|
|
env: {
|
|
GOOGLE_CLIENT_ID: "" as string,
|
|
GOOGLE_CLIENT_SECRET: "" as string,
|
|
}
|
|
}
|
|
// ---cut---
|
|
import { betterAuth } from "better-auth"
|
|
import { google } from "better-auth/social-providers"
|
|
|
|
export const auth = await betterAuth({
|
|
database: {
|
|
provider: "sqlite",
|
|
url: "./db.sqlite",
|
|
},
|
|
socialProvider: { // [!code highlight]
|
|
google: { // [!code highlight]
|
|
clientId: process.env.GOOGLE_CLIENT_ID as string, // [!code highlight]
|
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, // [!code highlight]
|
|
/** // [!code highlight]
|
|
* this is optional. // [!code highlight]
|
|
* only needed if you passed a custom basePath from `/api/auth` to `betterAuth` // [!code highlight]
|
|
* or if you're using a custom domain. // [!code highlight]
|
|
*/ // [!code highlight]
|
|
redirectURI: "https://example.com/api/auth/callback/apple", // [!code highlight]
|
|
}, // [!code highlight]
|
|
}, // [!code highlight]
|
|
})
|
|
```
|
|
</Step>
|
|
<Step>
|
|
### Signin with Google
|
|
To signin with Google, you can use the `signIn.social` function provided by the client. The `signIn` function takes an object with the following properties:
|
|
- `provider`: The provider to use. It should be set to `google`.
|
|
|
|
```ts title="client.ts" twoslash /
|
|
import { createAuthClient } from "better-auth/client"
|
|
const client = createAuthClient()
|
|
|
|
const signin = async () => {
|
|
const data = await client.signIn.social({
|
|
provider: "google"
|
|
})
|
|
}
|
|
```
|
|
</Step>
|
|
</Steps>
|