Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
feat(nuxt): Lucia module configuration and add GitHub OAuth support
Browse files Browse the repository at this point in the history
productdevbook committed Feb 19, 2024
1 parent 444350f commit 6f9562b
Showing 10 changed files with 111 additions and 11 deletions.
7 changes: 7 additions & 0 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
@@ -227,6 +227,13 @@ export default defineNuxtModule<PergelOptions>({
}],
})

useNitroImports(nuxt, {
presets: [{
from: _resolver.resolve('./runtime/core/utils/usePergelRuntime'),
imports: ['usePergelRuntime'],
}],
})

nuxt.options.alias['#pergel'] = _resolver.resolve('./runtime')

if (nuxt._pergel.exitPergelFolder) {
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { eventHandler } from 'h3'
import type { NitroApp } from 'nitropack'
import { defineNitroPlugin } from 'nitropack/dist/runtime/plugin'
import type { GraphQLYogaOptions, ResolvedGraphQLYogaConfig } from '../../types'
import { nitroGraphqlYogaPlugin } from './nitroGraphqlYogaPlugin'
import { useRuntimeConfig } from '#imports'
import type { PergelGlobalContextOmitModule } from '#pergel/types'

export type NitroAppPlugin = (nitro: NitroApp) => void

export function defineNitroPlugin(def: NitroAppPlugin): NitroAppPlugin {
return def
}

export function definePergelGraphQLYogaPlugin<Context extends Record<string, any> = object>(
this: PergelGlobalContextOmitModule,
data: GraphQLYogaOptions<Context>,
21 changes: 21 additions & 0 deletions packages/nuxt/src/runtime/modules/lucia/arcticLists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const oauthLists = {
github: {
clientId: '',
clientSecret: '',
},
google: {
clientId: '',
clientSecret: '',
redirectURI: '',
},
twitter: {
clientId: '',
clientSecret: '',
redirectURI: '',
},
gitlab: {
clientId: '',
clientSecret: '',
redirectURI: '',
},
}
14 changes: 13 additions & 1 deletion packages/nuxt/src/runtime/modules/lucia/index.ts
Original file line number Diff line number Diff line change
@@ -6,8 +6,10 @@ import { useNitroImports } from '../../core/utils/useImports'
import { createFolderModule } from '../../core/utils/createFolderModule'
import { writeFilePergel } from '../../core/utils/writeFilePergel'
import { generatorFunctionName } from '../../core/utils/generatorNames'
import type { LuciaModuleOptions, ResolvedLuciaModuleOptions } from './types'
import { generateModuleRuntimeConfig } from '../../core/utils/moduleRuntimeConfig'
import type { LuciaModuleOptions, LuciaRuntimeConfig, ResolvedLuciaModuleOptions } from './types'
import { setupDrizzle } from './drizzle'
import { oauthLists } from './arcticLists'

export default definePergelModule<LuciaModuleOptions, ResolvedLuciaModuleOptions>({
meta: {
@@ -65,6 +67,7 @@ export default definePergelModule<LuciaModuleOptions, ResolvedLuciaModuleOptions
return {
...options,
driver: rootOptions.driver ?? 'drizzle:postgre',
oauth: rootOptions.oauth ?? ['github'],
}
},
async setup({ nuxt, options }) {
@@ -80,6 +83,15 @@ export default definePergelModule<LuciaModuleOptions, ResolvedLuciaModuleOptions
use: '',
}

const reducedOAuthConfig = options.oauth.reduce((acc, provider) => {
(acc as any)[provider] = oauthLists[provider]
return acc
}, {}) as LuciaRuntimeConfig

generateModuleRuntimeConfig<LuciaRuntimeConfig>(nuxt, options, {
...reducedOAuthConfig,
})

if (driver === 'drizzle') {
const { driver } = setupDrizzle(db, resolver)
_setupDrizzle.use = driver
42 changes: 42 additions & 0 deletions packages/nuxt/src/runtime/modules/lucia/types.ts
Original file line number Diff line number Diff line change
@@ -10,8 +10,50 @@ export interface LuciaModuleOptions extends PergelModuleOptions {
* @description The driver to use for the lucia instance.
*/
driver: 'drizzle:postgre'

/**
* @see https://github.com/pilcrowOnPaper/arctic
* @see https://arctic.js.org/providers/github
* @description The arctic instance to use for the lucia instance.
* @default 'github'
*/
oauth?: (
'github'
| 'google'
| 'twitter'
| 'gitlab'
)[]
}

export interface ResolvedLuciaModuleOptions extends ResolvedPergelModuleOptions {
driver: 'drizzle:postgre'

oauth: (
'github'
| 'google'
| 'twitter'
| 'gitlab'
)[]
}

export interface LuciaRuntimeConfig {
github?: {
clientId: string
clientSecret: string
}
google?: {
clientId: string
clientSecret: string
redirectURI: string
}
twitter?: {
clientId: string
clientSecret: string
redirectURI: string
}
gitlab?: {
clientId: string
clientSecret: string
redirectURI: string
}
}
4 changes: 4 additions & 0 deletions themes/pergel-auth/pergel/.env.example
Original file line number Diff line number Diff line change
@@ -6,6 +6,10 @@ NUXT_CHANGE_NAME_URQL_ENDPOINT=http://localhost:3000/api/graphql
# changeName/graphqlYoga
NUXT_CHANGE_NAME_GRAPHQL_YOGA_ORIGIN=http://localhost:3000,http://localhost:3001

# changeName/lucia
NUXT_CHANGE_NAME_LUCIA_GITHUB_CLIENT_ID=
NUXT_CHANGE_NAME_LUCIA_GITHUB_CLIENT_SECRET=

# changeName/drizzle
NUXT_CHANGE_NAME_DRIZZLE_URL=postgres://postgres:postgres@localhost:5432/postgres
NUXT_CHANGE_NAME_DRIZZLE_HOST=localhost
8 changes: 6 additions & 2 deletions themes/pergel-auth/pergel/README.json
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
"pergel": {
"meta": {
"description": "This file is generated by pergel. Do not edit it manually.",
"version": "0.16.4"
"version": "0.16.11"
}
},
"changeName": {
@@ -52,13 +52,17 @@
}
},
"lucia": {
"env": {
"NUXT_CHANGE_NAME_LUCIA_GITHUB_CLIENT_ID": "",
"NUXT_CHANGE_NAME_LUCIA_GITHUB_CLIENT_SECRET": ""
},
"comment-block": "If pergel cli is installed, you can run `pergel install` automatically to install",
"packageJson": {
"dependencies": {
"lucia": "3.0.1",
"oslo": "^1.1.1",
"@lucia-auth/oauth": "^3.5.3",
"arctic": "^1.1.6",
"arctic": "^1.2.0",
"@lucia-auth/adapter-drizzle": "1.0.1",
"@lucia-auth/adapter-postgresql": "3.1.0"
},
2 changes: 1 addition & 1 deletion themes/pergel-auth/pergel/merged-package.json
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
"lucia": "3.0.1",
"oslo": "^1.1.1",
"@lucia-auth/oauth": "^3.5.3",
"arctic": "^1.1.6",
"arctic": "^1.2.0",
"@lucia-auth/adapter-drizzle": "1.0.1",
"@lucia-auth/adapter-postgresql": "3.1.0",
"@pergel/module-graphql": "0.4.2",
3 changes: 3 additions & 0 deletions themes/pergel-auth/server/graphqlYoga-changeName/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module '#changeName/graphqlYoga/generated/schema' {
const schema: string
}
13 changes: 13 additions & 0 deletions themes/pergel-auth/server/lucia-changeName/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GitHub } from 'arctic'

import { session, user } from '#changeName/drizzle/schema'

const connect = await pergelChangeName()
@@ -15,3 +17,14 @@ export const changeNameAuth = pergelChangeName()
session,
user,
})

const config = useRuntimeConfig()

export const github = new GitHub(
config.changeNameLucia.github.clientId,
config.changeNameLucia.github.clientSecret,
)

export const changeNameLuciaRequest = pergelChangeName().lucia().onRequestLucia({
lucia: changeNameAuth,
})

0 comments on commit 6f9562b

Please sign in to comment.