Skip to content

Commit

Permalink
feat: 提供authjs模式下signIn, signOut方法返回类型
Browse files Browse the repository at this point in the history
  • Loading branch information
gxmari007 committed Sep 20, 2024
1 parent b626ce6 commit a458f81
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
6 changes: 5 additions & 1 deletion playground-authjs/pages/sign-in.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const model = ref({
})
const login = async () => {
await signIn('credentials', { ...model.value, redirect: false })
const result = await signIn('credentials', { ...model.value, redirect: true })
if (result) {
console.log(result.error)
}
}
</script>
2 changes: 1 addition & 1 deletion playground-authjs/server/api/auth/[...].ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const options: AuthConfig = {
}
}

return null
throw new Error('用户名密码错误')
},
}),
],
Expand Down
31 changes: 23 additions & 8 deletions src/runtime/composables/authjs/use-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import {
} from '../../utils/url'
import { request } from '../../utils/request'
import { type SessionData, useAuthState } from './use-auth-state'
import { reloadNuxtApp, useNuxtApp, useRequestHeaders, useRuntimeConfig } from '#imports'
import {
reloadNuxtApp,
useNuxtApp,
useRequestHeaders,
useRequestURL,
useRuntimeConfig,
} from '#imports'

/**
* 获取设置的 provider 配置对象
Expand Down Expand Up @@ -60,12 +66,18 @@ const getSession: GetSession<SessionData | null> = async (options) => {
}

type SignInAuthorizationParams = string | string[][] | Record<string, string> | URLSearchParams
type SignInReturn = Promise<{
error: string | null
status: number
ok: boolean
url: string | null
} | void>

export const signIn = async (
provider?: SupportedProviders,
signInOptions: SignOptions = {},
authorizationParams?: SignInAuthorizationParams,
) => {
): SignInReturn => {
const nuxtApp = useNuxtApp()

// 1. 检查 NuxtAuthHandler providers 策略配置
Expand All @@ -90,8 +102,10 @@ export const signIn = async (
let { callbackUrl } = signInOptions

if (typeof callbackUrl === 'undefined') {
const requestCallbackUrl = useRequestURL().searchParams.get('callbackUrl') ?? ''

callbackUrl = await nuxtApp.runWithContext(() =>
getDefaultCallbackUrl(useRuntimeConfig().public.auth, () => getRequestUrl()),
getDefaultCallbackUrl(useRuntimeConfig().public.auth, () => requestCallbackUrl),
)
}

Expand Down Expand Up @@ -143,8 +157,11 @@ export const signIn = async (
body: params,
})

if (isCredentials && !redirect) {
const error = new URL(response.url).searchParams.get('error')

if (!error && isCredentials && !redirect) {
reloadNuxtApp({ persistState: true, force: true })
return
}

if (redirect || !isSupportReturn) {
Expand All @@ -153,9 +170,6 @@ export const signIn = async (
return await nuxtApp.runWithContext(() => navigateToAuthPage(href))
}

const error = new URL(response.url).searchParams.get('error')
await getSession()

return {
error,
status: 200,
Expand All @@ -165,8 +179,9 @@ export const signIn = async (
}

type SignOutOptions = Pick<SignOptions, 'redirect' | 'callbackUrl'>
type SignOutReturn = Promise<{ url: string } | void>

const signOut = async (options?: SignOutOptions) => {
const signOut = async (options?: SignOutOptions): SignOutReturn => {
const nuxtApp = useNuxtApp()

const requestUrl = getRequestUrl()
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { navigateTo, reloadNuxtApp, useRequestURL, useRuntimeConfig } from '#imp
* @param path 路径
* @returns 完整的 auth url
*/
export const getAuthApiUrl = (path: string) =>
joinURL(useRuntimeConfig().public.auth.params.fullBaseURL, path)
export const getAuthApiUrl = (path: string) => {
return joinURL(useRuntimeConfig().public.auth.params.fullBaseURL, path)
}

/**
* 获取当前请求的完整 url
Expand Down

0 comments on commit a458f81

Please sign in to comment.