Skip to content

Commit

Permalink
refactor: clean up config handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ZerNico committed May 28, 2023
1 parent ae3275d commit b9e040a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ export default defineNuxtModule<LogtoNuxtModuleConfig>({
setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)

const { basePath, appId, origin, ...privateOptions } = options

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

// @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,
appId: appId,
basePath: basePath,
origin: origin,
},
{ basePath: '/api/logto' }
)
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/composables/useLogto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface UseLogtoReturn {
*/
export const useLogto = (): UseLogtoReturn => {
const basePath = useRuntimeConfig().public.logto.basePath
const origin = useRuntimeConfig().public.logto.origin

const { data } = useLogtoState()

Expand All @@ -35,7 +36,7 @@ export const useLogto = (): UseLogtoReturn => {
* It can be used to refresh the user context.
*/
const fetchContext = async () => {
const contextUrl = `${basePath}/context`
const contextUrl = joinURL(origin, basePath, 'context')
const headers = useRequestHeaders(['cookie']) as HeadersInit
data.value = await $fetch<LogtoContext>(contextUrl, { headers })
}
Expand Down Expand Up @@ -88,7 +89,7 @@ export const useLogto = (): UseLogtoReturn => {
* @returns The sign in url.
*/
const getSigInUrl = (redirectTo?: string) => {
return withQuery(joinURL(basePath, 'sign-in'), { redirectTo })
return withQuery(joinURL(origin, basePath, 'sign-in'), { redirectTo })
}

/**
Expand All @@ -105,7 +106,7 @@ export const useLogto = (): UseLogtoReturn => {
* @returns The sign up url.
*/
const getSignUpUrl = (redirectTo?: string) => {
return withQuery(joinURL(basePath, 'sign-up'), { redirectTo })
return withQuery(joinURL(origin, basePath, 'sign-up'), { redirectTo })
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import type { LogtoNuxtConfig } from './types'

export const useConfig = () => {
const config = useRuntimeConfig().logto
const publicConfig = useRuntimeConfig().public.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")
console.warn('Failed to parse resources string')
}
}

Expand All @@ -19,9 +19,9 @@ export const useConfig = () => {
try {
scopes = JSON.parse(config.scopes)
} catch (e) {
console.warn("Failed to parse scopes string")
console.warn('Failed to parse scopes string')
}
}

return { ...config, scopes, resources } as unknown as LogtoNuxtConfig
return { ...config, ...publicConfig, scopes, resources } as unknown as LogtoNuxtConfig
}
6 changes: 3 additions & 3 deletions src/runtime/logto/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { H3Event, eventHandler, sendRedirect, getQuery } from 'h3'
import { joinURL } from 'ufo'
import { LogtoNuxtConfig } from '../types'
import { createLogtoEvent } from './event'
import { useConfig } from "../config"
import { useConfig } from '../config'

export class LogtoClient {
protected logtoConfig: LogtoNuxtConfig
Expand All @@ -20,7 +20,7 @@ export class LogtoClient {
* @returns An event handler to handle sign in / sign up.
*/
handleSignIn = (
redirectUri = `${this.logtoConfig.origin}${this.logtoConfig.basePath}/sign-in-callback`,
redirectUri = joinURL(this.logtoConfig.origin, this.logtoConfig.basePath, 'sign-in-callback'),
interactionMode?: InteractionMode
) =>
eventHandler(async (event) => {
Expand Down Expand Up @@ -59,7 +59,7 @@ export class LogtoClient {
const url = event.node.req.url

if (url) {
await logtoEvent.nodeClient.handleSignInCallback(`${this.logtoConfig.origin}${url}`)
await logtoEvent.nodeClient.handleSignInCallback(joinURL(this.logtoConfig.origin, url))
await logtoEvent.storage.save()
return sendRedirect(event, redirect)
}
Expand Down

0 comments on commit b9e040a

Please sign in to comment.