@@ -41,8 +41,6 @@ const errors = {
41
41
tokenMissingOrInvalid : 'either no token found or invalid for this purpose' ,
42
42
} ;
43
43
44
- type MaybeUserPromise = Promise < User | undefined > ;
45
-
46
44
export type ReactNetlifyIdentityAPI = {
47
45
user : User | undefined ;
48
46
/** not meant for normal use! you should mostly use one of the other exported methods to update the user instance */
@@ -54,32 +52,33 @@ export type ReactNetlifyIdentityAPI = {
54
52
password : string ,
55
53
data : Object ,
56
54
directLogin : boolean
57
- ) => MaybeUserPromise ;
55
+ ) => Promise < User | undefined > ;
58
56
loginUser : (
59
57
email : string ,
60
58
password : string ,
61
59
remember ?: boolean
62
- ) => MaybeUserPromise ;
63
- logoutUser : ( ) => MaybeUserPromise ;
60
+ ) => Promise < User | undefined > ;
61
+ logoutUser : ( ) => Promise < User | undefined > ;
64
62
requestPasswordRecovery : ( email : string ) => Promise < void > ;
65
- recoverAccount : ( remember ?: boolean ) => MaybeUserPromise ;
66
- updateUser : ( fields : object ) => MaybeUserPromise ;
67
- getFreshJWT : ( ) => Promise < string > ;
63
+ recoverAccount : ( remember ?: boolean ) => Promise < User | undefined > ;
64
+ updateUser : ( fields : object ) => Promise < User | undefined > ;
65
+ getFreshJWT : ( ) => Promise < string > | undefined ;
68
66
authedFetch : {
69
- get : ( endpoint : string , obj ?: { } ) => Promise < any > ;
70
- post : ( endpoint : string , obj ?: { } ) => Promise < any > ;
71
- put : ( endpoint : string , obj ?: { } ) => Promise < any > ;
72
- delete : ( endpoint : string , obj ?: { } ) => Promise < any > ;
67
+ get : ( endpoint : string , obj ?: RequestInit ) => Promise < any > ;
68
+ post : ( endpoint : string , obj ?: RequestInit ) => Promise < any > ;
69
+ put : ( endpoint : string , obj ?: RequestInit ) => Promise < any > ;
70
+ delete : ( endpoint : string , obj ?: RequestInit ) => Promise < any > ;
73
71
} ;
74
72
_goTrueInstance : GoTrue ;
75
73
_url : string ;
76
74
loginProvider : ( provider : Provider ) => void ;
77
75
acceptInviteExternalUrl : (
78
76
provider : Provider ,
79
77
autoRedirect : boolean
80
- ) => void | string ;
78
+ ) => string | undefined ;
81
79
settings : Settings ;
82
80
param : TokenParam ;
81
+ verifyToken : ( ) => Promise < User | undefined > ;
83
82
} ;
84
83
85
84
const [ _useIdentityContext , _IdentityCtxProvider ] = createCtx <
@@ -184,7 +183,8 @@ export function useNetlifyIdentity(
184
183
const acceptInviteExternalUrl = useCallback (
185
184
( provider : Provider , autoRedirect : boolean = true ) => {
186
185
if ( ! param . token || param . type !== 'invite' ) {
187
- throw new Error ( errors . tokenMissingOrInvalid ) ;
186
+ console . error ( errors . tokenMissingOrInvalid ) ;
187
+ return ;
188
188
}
189
189
190
190
const url = goTrueInstance . acceptInviteExternalUrl ( provider , param . token ) ;
@@ -201,6 +201,22 @@ export function useNetlifyIdentity(
201
201
[ goTrueInstance , param ]
202
202
) ;
203
203
204
+ /**
205
+ * @see https://github.com/netlify/gotrue-js/blob/master/src/index.js#L123
206
+ */
207
+ const verifyToken = useCallback ( ( ) => {
208
+ if ( ! param . type || ! param . token ) {
209
+ return Promise . reject ( errors . tokenMissingOrInvalid ) ;
210
+ }
211
+
212
+ return goTrueInstance . verify ( param . type , param . token ) . then ( user => {
213
+ // cleanup consumed token
214
+ setParam ( defaultParam ) ;
215
+
216
+ return user ;
217
+ } ) ;
218
+ } , [ goTrueInstance , param ] ) ;
219
+
204
220
/******* email auth */
205
221
/**
206
222
* @see https://github.com/netlify/gotrue-js/blob/master/src/index.js#L50
@@ -245,7 +261,7 @@ export function useNetlifyIdentity(
245
261
const recoverAccount = useCallback (
246
262
( remember ?: boolean ) => {
247
263
if ( ! param . token || param . type !== 'recovery' ) {
248
- throw new Error ( errors . tokenMissingOrInvalid ) ;
264
+ return Promise . reject ( errors . tokenMissingOrInvalid ) ;
249
265
}
250
266
251
267
return goTrueInstance
@@ -267,7 +283,7 @@ export function useNetlifyIdentity(
267
283
const updateUser = useCallback (
268
284
( fields : object ) => {
269
285
if ( ! user ) {
270
- throw new Error ( errors . noUserFound ) ;
286
+ return Promise . reject ( errors . noUserFound ) ;
271
287
}
272
288
273
289
return user !
@@ -282,7 +298,7 @@ export function useNetlifyIdentity(
282
298
*/
283
299
const getFreshJWT = useCallback ( ( ) => {
284
300
if ( ! user ) {
285
- throw new Error ( errors . noUserFound ) ;
301
+ return Promise . reject ( errors . noUserFound ) ;
286
302
}
287
303
288
304
return user . jwt ( ) ;
@@ -293,18 +309,18 @@ export function useNetlifyIdentity(
293
309
*/
294
310
const logoutUser = useCallback ( ( ) => {
295
311
if ( ! user ) {
296
- throw new Error ( errors . noUserFound ) ;
312
+ return Promise . reject ( errors . noUserFound ) ;
297
313
}
298
314
299
315
return user . logout ( ) . then ( ( ) => _setUser ( undefined ) ) ;
300
316
} , [ user ] ) ;
301
317
302
- const genericAuthedFetch = ( method : string ) => (
318
+ const genericAuthedFetch = ( method : RequestInit [ 'method' ] ) => (
303
319
endpoint : string ,
304
320
options : RequestInit = { }
305
321
) => {
306
322
if ( ! user ?. token ?. access_token ) {
307
- throw new Error ( errors . noUserTokenFound ) ;
323
+ return Promise . reject ( errors . noUserFound ) ;
308
324
}
309
325
310
326
const defaultObj = {
@@ -349,6 +365,7 @@ export function useNetlifyIdentity(
349
365
acceptInviteExternalUrl,
350
366
settings,
351
367
param,
368
+ verifyToken,
352
369
} ;
353
370
}
354
371
0 commit comments