Skip to content

Commit

Permalink
feat(auth): allow directly passing the auth instance
Browse files Browse the repository at this point in the history
See #1459
  • Loading branch information
posva committed Dec 15, 2023
1 parent 8814646 commit d5d5e1b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
Auth,
} from 'firebase/auth'
import { type App, ref, inject } from 'vue-demi'
import { useFirebaseApp } from '../app'
import { getGlobalScope } from '../globals'
import { isClient, _Nullable } from '../shared'
import { authUserMap, setupOnAuthStateChanged } from './user'
Expand Down Expand Up @@ -39,6 +38,17 @@ export interface VueFireAuthOptions {
dependencies: AuthDependencies
}

/**
* Options for VueFire Auth module when passing the auth instance directly.
*/
export interface VueFireAuthOptionsFromAuth
extends Pick<VueFireAuthOptions, 'initialUser'> {
/**
* Auth instance to use.
*/
auth: Auth
}

/**
* VueFire Auth Module to be added to the `VueFire` Vue plugin options. This calls the `VueFireAuthWithDependencies()`
* with **all** the dependencies, increasing bundle size. Consider using `VueFireAuthWithDependencies()` instead to
Expand Down Expand Up @@ -81,6 +91,29 @@ export function VueFireAuth(initialUser?: _Nullable<User>): VueFireModule {
*/
export const _VueFireAuthKey = Symbol('VueFireAuth')

/**
* VueFire Auth Module to be added to the `VueFire` Vue plugin options. It accepts an auth instance rather than the
* dependencies. It allows manually calling emulators and other advanced use cases. Prefer using
* `VueFireAuthWithDependencies()` and `VueFireAuth()` for most use cases.
*
* @param options - auth instance and initial user
*/
export function VueFireAuthOptionsFromAuth({
auth,
initialUser,
}: VueFireAuthOptionsFromAuth): VueFireModule {
return (firebaseApp: FirebaseApp, app: App) => {
const [user, _auth] = _VueFireAuthInit(
firebaseApp,
app,
initialUser,
undefined,
auth
)
setupOnAuthStateChanged(user, _auth)
}
}

/**
* VueFire Auth Module to be added to the `VueFire` Vue plugin options. It accepts dependencies to pass to
* `initializeAuth()` to better control the bundle size.
Expand Down Expand Up @@ -110,14 +143,14 @@ export function _VueFireAuthInit(
firebaseApp: FirebaseApp,
app: App,
initialUser: _Nullable<User>,
dependencies: AuthDependencies
dependencies?: AuthDependencies,
auth = initializeAuth(firebaseApp, dependencies)
) {
const user = getGlobalScope(firebaseApp, app).run(() =>
ref<_Nullable<User>>(initialUser)
)!
// TODO: Is it okay to have it both server and client?
authUserMap.set(firebaseApp, user)
const auth = initializeAuth(firebaseApp, dependencies)
app.provide(_VueFireAuthKey, auth)

return [user, auth] as const
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export {
type VueFireAuthOptions,
VueFireAuth,
VueFireAuthWithDependencies,
VueFireAuthOptionsFromAuth,
_VueFireAuthInit,
useFirebaseAuth,
_VueFireAuthKey,
Expand Down

2 comments on commit d5d5e1b

@harwoodspike
Copy link

@harwoodspike harwoodspike commented on d5d5e1b Dec 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@posva The interface and the function have the same name, is that valid? I have never done that before.

I am not sure that VueFireAuthOptionsFromAuth is a really good name for the function because it isn't pulling options, but use whatever name you like.

Edit: After looking into it you can have the same name. I guess you learn something everyday. But the module name is still odd to me.

@posva
Copy link
Member Author

@posva posva commented on d5d5e1b Dec 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the name of the function is wrong. I will have to change it. Thanks!

Please sign in to comment.