diff --git a/docs/guide/authentication.md b/docs/guide/authentication.md index 88aeee4..2176346 100644 --- a/docs/guide/authentication.md +++ b/docs/guide/authentication.md @@ -200,4 +200,42 @@ export default defineEventHandler(async (event) => { - [Password Reset](/guide/password-reset) - Add password reset functionality - [Components](/components/) - Learn about available Vue components -- [API Reference](/api/) - Explore all available API endpoints \ No newline at end of file +- [API Reference](/api/) - Explore all available API endpoints + +## Social Login with Google + +This module also supports social login with Google. + +### Configuration + +First, you need to configure the Google OAuth credentials in your `nuxt.config.ts` file: + +```ts +export default defineNuxtConfig({ + modules: ['nuxt-users'], + nuxtUsers: { + oauth: { + google: { + clientId: 'your-google-client-id', + clientSecret: 'your-google-client-secret', + redirectUri: 'http://localhost:3000/api/auth/google/callback', + scope: ['email', 'profile'], + }, + }, + }, +}) +``` + +### Usage + +The module provides a `GoogleLoginButton` component that you can use in your login page: + +```vue + +``` + +The `LoginForm` component will automatically display the `GoogleLoginButton` if the Google OAuth credentials are configured. + +When a user clicks the "Login with Google" button, they will be redirected to Google for authentication. After successful authentication, they will be redirected back to your application at the `redirectUri` you configured. The module will then create a new user if one doesn't exist and create a session for the user. \ No newline at end of file diff --git a/package.json b/package.json index 4263095..52b9eb7 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "docs:preview": "vitepress preview docs" }, "dependencies": { + "@lucia-auth/oauth": "^4.0.0", "@nuxt/kit": "^3.17.6", "@types/bcrypt": "^5.0.2", "bcrypt": "^6.0.0", diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 8a37641..7bf06bd 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -2,5 +2,14 @@ export default defineNuxtConfig({ modules: ['../src/module', '@formkit/nuxt'], devtools: { enabled: true }, compatibilityDate: '2025-07-08', - nuxtUsers: {}, + nuxtUsers: { + oauth: { + google: { + clientId: 'your-google-client-id', + clientSecret: 'your-google-client-secret', + redirectUri: 'http://localhost:3000/api/auth/google/callback', + scope: ['email', 'profile'], + }, + }, + }, }) diff --git a/src/module.ts b/src/module.ts index be60924..7b7588f 100644 --- a/src/module.ts +++ b/src/module.ts @@ -5,6 +5,14 @@ import { getAppliedMigrations } from './runtime/server/utils/migrate' import type { ModuleOptions } from './types' export const defaultOptions: ModuleOptions = { + oauth: { + google: { + clientId: '', + clientSecret: '', + redirectUri: '', + scope: [], + }, + }, connector: { name: 'sqlite', options: { @@ -121,6 +129,12 @@ export default defineNuxtModule({ handler: resolver.resolve('./runtime/server/api/auth/reset-password.post') }) + addServerHandler({ + route: '/api/auth/google', + method: 'post', + handler: resolver.resolve('./runtime/server/api/auth/google/login.post') + }) + addPlugin(resolver.resolve('./runtime/plugin')) // Register the LoginForm component @@ -140,5 +154,11 @@ export default defineNuxtModule({ name: 'ResetPasswordForm', filePath: resolver.resolve('./runtime/components/ResetPasswordForm.vue') }) + + // Register the GoogleLoginButton component + addComponent({ + name: 'GoogleLoginButton', + filePath: resolver.resolve('./runtime/components/GoogleLoginButton.vue') + }) }, }) diff --git a/src/runtime/components/GoogleLoginButton.vue b/src/runtime/components/GoogleLoginButton.vue new file mode 100644 index 0000000..a503db1 --- /dev/null +++ b/src/runtime/components/GoogleLoginButton.vue @@ -0,0 +1,7 @@ + diff --git a/src/runtime/components/LoginForm.vue b/src/runtime/components/LoginForm.vue index 4b52049..21c8937 100644 --- a/src/runtime/components/LoginForm.vue +++ b/src/runtime/components/LoginForm.vue @@ -1,6 +1,7 @@