@@ -5,27 +5,68 @@ import { runRoutes } from "./runRoutes"
5
5
6
6
type authChangeParam = ( user ?: User ) => string | void
7
7
8
- interface NIProps {
9
- children : any
10
- domain : string
11
- onAuthChange ?: authChangeParam
12
- }
13
-
14
8
export type Settings = Settings
15
9
export type User = User
16
- export default function NetlifyIdentity ( { children, domain, onAuthChange } : NIProps ) {
17
- return children ( useNetlifyIdentity ( domain , onAuthChange ) )
10
+
11
+ export type ReactNetlifyIdentityAPI = {
12
+ user : User | undefined
13
+ /** not meant for normal use! you should mostly use one of the other exported methods to update the user instance */
14
+ setUser : React . Dispatch < React . SetStateAction < User | undefined > >
15
+ isConfirmedUser : boolean
16
+ isLoggedIn : boolean
17
+ signupUser : ( email : string , password : string , data : Object ) => Promise < User | undefined >
18
+ loginUser : ( email : string , password : string , remember ?: boolean ) => Promise < User | undefined >
19
+ logoutUser : ( ) => Promise < User | undefined >
20
+ requestPasswordRecovery : ( email : string ) => Promise < void >
21
+ recoverAccount : ( token : string , remember ?: boolean | undefined ) => Promise < User >
22
+ updateUser : ( fields : Object ) => Promise < User | undefined >
23
+ getFreshJWT : ( ) => Promise < string >
24
+ authedFetch : {
25
+ get : ( endpoint : string , obj ?: { } ) => Promise < any >
26
+ post : ( endpoint : string , obj ?: { } ) => Promise < any >
27
+ put : ( endpoint : string , obj ?: { } ) => Promise < any >
28
+ delete : ( endpoint : string , obj ?: { } ) => Promise < any >
29
+ }
30
+ _goTrueInstance : GoTrue
31
+ _url : string
32
+ loginProvider : ( provider : "bitbucket" | "github" | "gitlab" | "google" ) => void
33
+ acceptInviteExternalUrl : ( provider : "bitbucket" | "github" | "gitlab" | "google" , token : string ) => string
34
+ settings : ( ) => Promise < Settings >
18
35
}
19
- export function useNetlifyIdentity ( domain : string , onAuthChange : authChangeParam = ( ) => { } ) {
36
+
37
+ export const IdentityContext = React . createContext < ReactNetlifyIdentityAPI | undefined > ( undefined )
38
+ export function IdentityContextProvider ( {
39
+ url,
40
+ children,
41
+ onAuthChange = ( ) => { }
42
+ } : {
43
+ url : string
44
+ children : React . ReactNode
45
+ onAuthChange : authChangeParam
46
+ } ) {
20
47
/******** SETUP */
21
- if ( ! domain || ! validateUrl ( domain ) ) {
48
+ if ( ! url || ! validateUrl ( url ) ) {
22
49
// just a safety check in case a JS user tries to skip this
23
50
throw new Error (
24
- "invalid netlify instance URL: " + domain + ". Please check the docs for proper usage or file an issue."
51
+ "invalid netlify instance URL: " + url + ". Please check the docs for proper usage or file an issue."
25
52
)
26
53
}
54
+ const identity = React . useMemo ( ( ) => useNetlifyIdentity ( url , onAuthChange ) , [ url , onAuthChange ] )
55
+ return < IdentityContext . Provider value = { identity } > { children } </ IdentityContext . Provider >
56
+ }
57
+
58
+ // // Deprecated for now
59
+ // interface NIProps {
60
+ // children: any
61
+ // url: string
62
+ // onAuthChange?: authChangeParam
63
+ // }
64
+ // export default function NetlifyIdentity({ children, url, onAuthChange }: NIProps) {
65
+ // return children(useNetlifyIdentity(url, onAuthChange))
66
+ // }
67
+ export function useNetlifyIdentity ( url : string , onAuthChange : authChangeParam = ( ) => { } ) : ReactNetlifyIdentityAPI {
27
68
const goTrueInstance = new GoTrue ( {
28
- APIUrl : `${ domain } /.netlify/identity` ,
69
+ APIUrl : `${ url } /.netlify/identity` ,
29
70
setCookie : true
30
71
} )
31
72
@@ -48,7 +89,7 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
48
89
49
90
const loginProvider = ( provider : Provider ) => {
50
91
const url = goTrueInstance . loginExternalUrl ( provider )
51
- if ( window ) window . location . href = url
92
+ window . location . href = url
52
93
}
53
94
const acceptInviteExternalUrl = ( provider : Provider , token : string ) =>
54
95
goTrueInstance . acceptInviteExternalUrl ( provider , token )
@@ -89,7 +130,7 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
89
130
}
90
131
}
91
132
const finalObj = Object . assign ( defaultObj , { method } , obj )
92
- return fetch ( endpoint , finalObj ) . then ( res =>
133
+ return fetch ( endpoint , finalObj ) . then ( ( res ) =>
93
134
finalObj . headers [ "Content-Type" ] === "application/json" ? res . json ( ) : res
94
135
)
95
136
}
@@ -103,7 +144,8 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
103
144
/******* hook API */
104
145
return {
105
146
user,
106
- setUser, // use carefully!! mostly you should use the methods below
147
+ /** not meant for normal use! you should mostly use one of the other exported methods to update the user instance */
148
+ setUser,
107
149
isConfirmedUser : ! ! ( user && user . confirmed_at ) ,
108
150
isLoggedIn : ! ! user ,
109
151
signupUser,
@@ -115,7 +157,7 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
115
157
getFreshJWT,
116
158
authedFetch,
117
159
_goTrueInstance : goTrueInstance ,
118
- _domain : domain ,
160
+ _url : url ,
119
161
loginProvider,
120
162
acceptInviteExternalUrl,
121
163
settings
0 commit comments