forked from gladly-team/next-firebase-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
155 lines (137 loc) · 4.53 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import * as Cookies from 'cookies'
import type Firebase from 'firebase'
import * as firebaseAdmin from 'firebase-admin'
import type {
GetServerSidePropsContext,
GetServerSidePropsResult,
NextApiRequest,
NextApiResponse,
} from 'next'
import type { ComponentType } from 'react'
import type { ParsedUrlQuery } from 'querystring'
export enum AuthAction {
RENDER = 'render',
SHOW_LOADER = 'showLoader',
RETURN_NULL = 'returnNull',
REDIRECT_TO_LOGIN = 'redirectToLogin',
REDIRECT_TO_APP = 'redirectToApp',
}
export interface AuthUser {
id: string | null
email: string | null
emailVerified: boolean
phoneNumber: string | null
displayName: string | null
photoURL: string | null
claims: Record<string, string | boolean>
tenantId: string
getIdToken: () => Promise<string | null>
clientInitialized: boolean
firebaseUser: Firebase.User | null
signOut: () => Promise<void>
}
export type SSRPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> =
GetServerSidePropsContext<Q> & { AuthUser: AuthUser }
export type SSRPropGetter<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery
> = (context: SSRPropsContext<Q>) => Promise<GetServerSidePropsResult<P>>
interface AuthUserContext extends AuthUser {
serialize: (opts?: { includeToken?: boolean }) => string
}
type URLResolveFunction = (obj: {
ctx: GetServerSidePropsContext<ParsedUrlQuery>
AuthUser: AuthUser
}) => string
type PageURL = string | URLResolveFunction
interface InitConfig {
authPageURL?: PageURL
appPageURL?: PageURL
loginAPIEndpoint?: string
logoutAPIEndpoint?: string
tokenChangedHandler?: (user: AuthUser) => void
firebaseAdminInitConfig?: {
credential: {
projectId: string
clientEmail: string
privateKey: string
}
databaseURL: string
}
firebaseAuthEmulatorHost?: string
firebaseClientInitConfig: {
apiKey: string
projectId?: string
appId?: string
// "PROJECT_ID.firebaseapp.com"
authDomain?: string
// "https://PROJECT_ID.firebaseio.com"
databaseURL?: string
// "PROJECT_ID.appspot.com"
storageBucket?: string
// "SENDER_ID"
messagingSenderId?: string
// "G-MEASUREMENT_ID"
measurementId?: string
tenantId?: string
}
cookies: Cookies.Option &
Cookies.SetOption & {
name: string
}
debug?: boolean
}
export const init: (config: InitConfig) => void
// We construct an interface for the `firebase-admin` module because
// it's not clear how to get the typing for the top-level admin export.
// If there's a proper way to get the type, we should use it/
// https://firebase.google.com/docs/reference/admin/node/admin
// We extend from the App interface, which is similar but:
// * it contains a "delete" method
// * it does not contain an "app" or "credential" property
// https://firebase.google.com/docs/reference/admin/node/admin.app.App-1
interface FirebaseAdminType extends firebaseAdmin.app.App {
app: firebaseAdmin.app.App
delete: undefined
credential: firebaseAdmin.credential.Credential
}
export const getFirebaseAdmin: () => FirebaseAdminType
export const setAuthCookies: (
req: NextApiRequest,
res: NextApiResponse
) => Promise<{
idToken: string
refreshToken: string
AuthUser: AuthUser
}>
export const unsetAuthCookies: (
req: NextApiRequest,
res: NextApiResponse
) => Promise<void>
export const useAuthUser: () => AuthUserContext
export const verifyIdToken: (token: string) => Promise<AuthUser>
export const withAuthUser: <P = unknown>(options?: {
whenAuthed?: AuthAction.RENDER | AuthAction.REDIRECT_TO_APP
whenUnauthedBeforeInit?:
| AuthAction.RENDER
| AuthAction.REDIRECT_TO_LOGIN
| AuthAction.SHOW_LOADER
| AuthAction.RETURN_NULL
whenUnauthedAfterInit?: AuthAction.RENDER | AuthAction.REDIRECT_TO_LOGIN
appPageURL?: PageURL
authPageURL?: PageURL
LoaderComponent?: ComponentType | null
}) => (component: ComponentType<P>) => ComponentType<P>
export const withAuthUserTokenSSR: (options?: {
whenAuthed?: AuthAction.RENDER | AuthAction.REDIRECT_TO_APP
whenUnauthed?: AuthAction.RENDER | AuthAction.REDIRECT_TO_LOGIN
appPageURL?: PageURL
authPageURL?: PageURL
}) => (propGetter?: SSRPropGetter) => ReturnType<SSRPropGetter>
export const withAuthUserSSR: (options?: {
whenAuthed?: AuthAction.RENDER | AuthAction.REDIRECT_TO_APP
whenUnauthed?: AuthAction.RENDER | AuthAction.REDIRECT_TO_LOGIN
appPageURL?: PageURL
authPageURL?: PageURL
}) => (propGetter?: SSRPropGetter) => ReturnType<SSRPropGetter>