1
- import React from ' react'
1
+ import React from " react"
2
2
3
- import GoTrue , { User } from ' gotrue-js'
3
+ import GoTrue , { User } from " gotrue-js"
4
4
5
5
type authChangeParam = ( user ?: User ) => string | void
6
6
@@ -12,6 +12,7 @@ interface NIProps {
12
12
const NetlifyIdentity = ( { children, domain, onAuthChange } : NIProps ) =>
13
13
children ( useNetlifyIdentity ( domain , onAuthChange ) )
14
14
15
+ export type User = User
15
16
export default NetlifyIdentity
16
17
export function useNetlifyIdentity ( domain : string , onAuthChange : authChangeParam = ( ) => { } ) {
17
18
const goTrueInstance = new GoTrue ( {
@@ -34,48 +35,48 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
34
35
const recoverAccount = ( token : string , remember ?: boolean | undefined ) => goTrueInstance . recover ( token , remember )
35
36
const updateUser = ( fields : Object ) => {
36
37
if ( user == null ) {
37
- throw new Error ( ' No current user found - are you logged in?' )
38
+ throw new Error ( " No current user found - are you logged in?" )
38
39
} else {
39
40
return user !
40
41
. update ( fields ) // e.g. { email: "example@example.com", password: "password" }
41
42
. then ( _setUser )
42
43
}
43
44
}
44
45
const getFreshJWT = ( ) => {
45
- if ( ! user ) throw new Error ( ' No current user found - are you logged in?' )
46
+ if ( ! user ) throw new Error ( " No current user found - are you logged in?" )
46
47
return user . jwt ( )
47
48
}
48
49
const logoutUser = ( ) => {
49
- if ( ! user ) throw new Error ( ' No current user found - are you logged in?' )
50
+ if ( ! user ) throw new Error ( " No current user found - are you logged in?" )
50
51
return user . logout ( ) . then ( ( ) => _setUser ( undefined ) )
51
52
}
52
53
53
54
const genericAuthedFetch = ( method : string ) => ( endpoint : string , obj = { } ) => {
54
- if ( ! user || ! user . token || ! user . token . access_token ) throw new Error ( ' no user token found' )
55
+ if ( ! user || ! user . token || ! user . token . access_token ) throw new Error ( " no user token found" )
55
56
const defaultObj = {
56
57
headers : {
57
- Accept : ' application/json' ,
58
- ' Content-Type' : ' application/json' ,
59
- Authorization : ' Bearer ' + user . token . access_token
58
+ Accept : " application/json" ,
59
+ " Content-Type" : " application/json" ,
60
+ Authorization : " Bearer " + user . token . access_token
60
61
}
61
62
}
62
63
const finalObj = Object . assign ( defaultObj , { method } , obj )
63
64
return fetch ( endpoint , finalObj ) . then ( res =>
64
- finalObj . headers [ ' Content-Type' ] === ' application/json' ? res . json ( ) : res
65
+ finalObj . headers [ " Content-Type" ] === " application/json" ? res . json ( ) : res
65
66
)
66
67
}
67
68
const authedFetch = {
68
- get : genericAuthedFetch ( ' GET' ) ,
69
- post : genericAuthedFetch ( ' POST' ) ,
70
- put : genericAuthedFetch ( ' PUT' ) ,
71
- delete : genericAuthedFetch ( ' DELETE' )
69
+ get : genericAuthedFetch ( " GET" ) ,
70
+ post : genericAuthedFetch ( " POST" ) ,
71
+ put : genericAuthedFetch ( " PUT" ) ,
72
+ delete : genericAuthedFetch ( " DELETE" )
72
73
}
73
74
74
75
// // confirmation
75
76
// http://lea.verou.me/2011/05/get-your-hash-the-bulletproof-way/
76
77
React . useEffect ( ( ) => {
77
78
const hash = window . location . hash . substring ( 1 )
78
- if ( hash . slice ( 0 , 19 ) === ' confirmation_token=' ) {
79
+ if ( hash . slice ( 0 , 19 ) === " confirmation_token=" ) {
79
80
// we are in a confirmation!
80
81
const token = hash . slice ( 19 )
81
82
goTrueInstance
0 commit comments