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

Commit

Permalink
feat: parse resources and scopes if they are strings to allow setting…
Browse files Browse the repository at this point in the history
… them by env
  • Loading branch information
ZerNico committed May 27, 2023
1 parent 0ee140f commit 68eb774
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
3 changes: 2 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineNuxtConfig({
basePath: '/api/logto',
cookieSecret: 'complex_password_at_least_32_characters_long',
cookieSecure: process.env.NODE_ENV === 'production',
resources: [],
resources: '',
scopes: '',
},
})
9 changes: 7 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import { defineNuxtModule, addPlugin, createResolver, addTemplate, addImports } from '@nuxt/kit'
import { defu } from 'defu'
import { LogtoNuxtConfig } from './runtime/types'
import { LogtoNuxtModuleConfig } from './runtime/types'

export default defineNuxtModule<LogtoNuxtConfig>({
export default defineNuxtModule<LogtoNuxtModuleConfig>({
meta: {
name: '@zernico/nuxt-logto',
configKey: 'logto',
},
setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)

// @ts-ignore
nuxt.options.runtimeConfig.logto = defu(
nuxt.options.runtimeConfig.logto,
{ ...options },
{ coookieSecure: true, basePath: '/api/logto' }
)

// @ts-ignore
nuxt.options.runtimeConfig.public.logto = defu(
nuxt.options.runtimeConfig.public.logto,
{
appId: options.appId,
basePath: options.basePath,
},
{
appId: nuxt.options.runtimeConfig.logto.appId,
},
{ basePath: '/api/logto' }
)

Expand Down
27 changes: 27 additions & 0 deletions src/runtime/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useRuntimeConfig } from '#imports'
import type { LogtoNuxtConfig } from './types'

export const useConfig = () => {
const config = useRuntimeConfig().logto


let resources = config.resources
if (resources && typeof resources === 'string') {
try {
resources = JSON.parse(config.resources)
} catch (e) {
console.warn("Failed to parse resources string")
}
}

let scopes = config.scopes
if (scopes && typeof scopes === 'string') {
try {
scopes = JSON.parse(config.scopes)
} catch (e) {
console.warn("Failed to parse scopes string")
}
}

return { ...config, scopes, resources } as unknown as LogtoNuxtConfig
}
4 changes: 2 additions & 2 deletions src/runtime/logto/client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { GetContextParameters, InteractionMode } from '@logto/node'
import { H3Event, eventHandler, sendRedirect, getQuery } from 'h3'
import { joinURL } from 'ufo'
import { useRuntimeConfig } from '#imports'
import { LogtoNuxtConfig } from '../types'
import { createLogtoEvent } from './event'
import { useConfig } from "../config"

export class LogtoClient {
protected logtoConfig: LogtoNuxtConfig
constructor() {
this.logtoConfig = useRuntimeConfig().logto
this.logtoConfig = useConfig()
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface LogtoNuxtConfig extends LogtoConfig {
basePath: string
}

export type LogtoNuxtModuleConfig = Omit<LogtoNuxtConfig, 'resources' | 'scopes'> & {
resources: string | string[]
scopes: string | string[]
}

export type Adapters = {
NodeClient: typeof NodeClient
}
Expand Down

0 comments on commit 68eb774

Please sign in to comment.