feat: add shared UI package auth DI (#5720)
* feat: add shared UI package auth DI * use refs instead of reactive * pnpm prepr * move app auth provider setup to src/providers/setup
This commit is contained in:
@@ -106,6 +106,7 @@ import {
|
|||||||
} from '@/providers/download-progress.ts'
|
} from '@/providers/download-progress.ts'
|
||||||
import { createServerInstall, provideServerInstall } from '@/providers/server-install'
|
import { createServerInstall, provideServerInstall } from '@/providers/server-install'
|
||||||
import { setupProviders } from '@/providers/setup'
|
import { setupProviders } from '@/providers/setup'
|
||||||
|
import { setupAuthProvider } from '@/providers/setup/auth'
|
||||||
import { useError } from '@/store/error.js'
|
import { useError } from '@/store/error.js'
|
||||||
import { useLoading, useTheming } from '@/store/state'
|
import { useLoading, useTheming } from '@/store/state'
|
||||||
|
|
||||||
@@ -454,6 +455,10 @@ const credentials = ref()
|
|||||||
|
|
||||||
const modrinthLoginFlowWaitModal = ref()
|
const modrinthLoginFlowWaitModal = ref()
|
||||||
|
|
||||||
|
setupAuthProvider(credentials, async (_redirectPath) => {
|
||||||
|
await signIn()
|
||||||
|
})
|
||||||
|
|
||||||
async function fetchCredentials() {
|
async function fetchCredentials() {
|
||||||
const creds = await getCreds().catch(handleError)
|
const creds = await getCreds().catch(handleError)
|
||||||
if (creds && creds.user_id) {
|
if (creds && creds.user_id) {
|
||||||
|
|||||||
29
apps/app-frontend/src/providers/setup/auth.ts
Normal file
29
apps/app-frontend/src/providers/setup/auth.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import type { Labrinth } from '@modrinth/api-client'
|
||||||
|
import { type AuthProvider, provideAuth } from '@modrinth/ui'
|
||||||
|
import { type Ref, ref, watchEffect } from 'vue'
|
||||||
|
|
||||||
|
type AppCredentials = {
|
||||||
|
session?: string | null
|
||||||
|
user?: Labrinth.Users.v2.User | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setupAuthProvider(
|
||||||
|
credentials: Ref<AppCredentials | null | undefined>,
|
||||||
|
requestSignIn: (redirectPath: string) => void | Promise<void>,
|
||||||
|
) {
|
||||||
|
const sessionToken = ref<string | null>(null)
|
||||||
|
const user = ref<Labrinth.Users.v2.User | null>(null)
|
||||||
|
|
||||||
|
const authProvider: AuthProvider = {
|
||||||
|
session_token: sessionToken,
|
||||||
|
user,
|
||||||
|
requestSignIn,
|
||||||
|
}
|
||||||
|
|
||||||
|
watchEffect(() => {
|
||||||
|
sessionToken.value = credentials.value?.session ?? null
|
||||||
|
user.value = credentials.value?.user ?? null
|
||||||
|
})
|
||||||
|
|
||||||
|
provideAuth(authProvider)
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { provideNotificationManager } from '@modrinth/ui'
|
import { provideNotificationManager } from '@modrinth/ui'
|
||||||
|
|
||||||
import { FrontendNotificationManager } from './frontend-notifications'
|
import { FrontendNotificationManager } from './frontend-notifications'
|
||||||
|
import { setupAuthProvider } from './setup/auth'
|
||||||
import { setupFilePickerProvider } from './setup/file-picker'
|
import { setupFilePickerProvider } from './setup/file-picker'
|
||||||
import { setupModrinthClientProvider } from './setup/modrinth-client'
|
import { setupModrinthClientProvider } from './setup/modrinth-client'
|
||||||
import { setupPageContextProvider } from './setup/page-context'
|
import { setupPageContextProvider } from './setup/page-context'
|
||||||
@@ -9,6 +10,7 @@ import { setupTagsProvider } from './setup/tags'
|
|||||||
export function setupProviders(auth: Awaited<ReturnType<typeof useAuth>>) {
|
export function setupProviders(auth: Awaited<ReturnType<typeof useAuth>>) {
|
||||||
provideNotificationManager(new FrontendNotificationManager())
|
provideNotificationManager(new FrontendNotificationManager())
|
||||||
|
|
||||||
|
setupAuthProvider(auth)
|
||||||
setupModrinthClientProvider(auth)
|
setupModrinthClientProvider(auth)
|
||||||
setupTagsProvider()
|
setupTagsProvider()
|
||||||
setupFilePickerProvider()
|
setupFilePickerProvider()
|
||||||
|
|||||||
29
apps/frontend/src/providers/setup/auth.ts
Normal file
29
apps/frontend/src/providers/setup/auth.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import type { Labrinth } from '@modrinth/api-client'
|
||||||
|
import { type AuthProvider, provideAuth } from '@modrinth/ui'
|
||||||
|
import { ref, watchEffect } from 'vue'
|
||||||
|
|
||||||
|
export function setupAuthProvider(auth: Awaited<ReturnType<typeof useAuth>>) {
|
||||||
|
const router = useRouter()
|
||||||
|
const sessionToken = ref<string | null>(null)
|
||||||
|
const user = ref<Labrinth.Users.v2.User | null>(null)
|
||||||
|
|
||||||
|
const authProvider: AuthProvider = {
|
||||||
|
session_token: sessionToken,
|
||||||
|
user,
|
||||||
|
requestSignIn: async (redirectPath: string) => {
|
||||||
|
await router.push({
|
||||||
|
path: '/auth/sign-in',
|
||||||
|
query: {
|
||||||
|
redirect: redirectPath,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
watchEffect(() => {
|
||||||
|
sessionToken.value = auth.value.token || null
|
||||||
|
user.value = (auth.value.user as Labrinth.Users.v2.User | null) ?? null
|
||||||
|
})
|
||||||
|
|
||||||
|
provideAuth(authProvider)
|
||||||
|
}
|
||||||
12
packages/ui/src/providers/auth.ts
Normal file
12
packages/ui/src/providers/auth.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import type { Labrinth } from '@modrinth/api-client/src/modules/labrinth/types'
|
||||||
|
import type { Ref } from 'vue'
|
||||||
|
|
||||||
|
import { createContext } from './create-context'
|
||||||
|
|
||||||
|
export interface AuthProvider {
|
||||||
|
session_token: Ref<string | null>
|
||||||
|
user: Ref<Labrinth.Users.v2.User | null>
|
||||||
|
requestSignIn: (redirectPath: string) => void | Promise<void>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const [injectAuth, provideAuth] = createContext<AuthProvider>('root', 'auth')
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
export * from './api-client'
|
export * from './api-client'
|
||||||
export * from './app-backup'
|
export * from './app-backup'
|
||||||
|
export * from './auth'
|
||||||
export * from './content-manager'
|
export * from './content-manager'
|
||||||
export { createContext } from './create-context'
|
export { createContext } from './create-context'
|
||||||
export * from './file-picker'
|
export * from './file-picker'
|
||||||
|
|||||||
Reference in New Issue
Block a user