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
- type authChangeParam = ( user ?: User ) => string | void ;
5
+ type authChangeParam = ( user ?: User ) => string | void
6
6
7
7
interface NIProps {
8
- children : any ;
9
- domain : string ;
10
- onAuthChange ?: authChangeParam ;
8
+ children : any
9
+ domain : string
10
+ onAuthChange ?: authChangeParam
11
11
}
12
12
const NetlifyIdentity = ( { children, domain, onAuthChange } : NIProps ) =>
13
- children ( useNetlifyIdentity ( domain , onAuthChange ) ) ;
13
+ children ( useNetlifyIdentity ( domain , onAuthChange ) )
14
14
15
- export default NetlifyIdentity ;
16
- export function useNetlifyIdentity (
17
- domain : string ,
18
- onAuthChange : authChangeParam = ( ) => { }
19
- ) {
15
+ export default NetlifyIdentity
16
+ export function useNetlifyIdentity ( domain : string , onAuthChange : authChangeParam = ( ) => { } ) {
20
17
const goTrueInstance = new GoTrue ( {
21
18
APIUrl : `${ domain } /.netlify/identity` ,
22
19
setCookie : true
23
- } ) ;
20
+ } )
24
21
25
- const [ user , setUser ] = React . useState < User | undefined > ( undefined ) ;
22
+ const [ user , setUser ] = React . useState < User | undefined > ( goTrueInstance . currentUser ( ) || undefined )
26
23
const _setUser = ( _user : User | undefined ) => {
27
- setUser ( _user ) ;
28
- onAuthChange ( _user ) ; // if someone's subscribed to auth changes, let 'em know
29
- return _user ; // so that we can continue chaining
30
- } ;
24
+ setUser ( _user )
25
+ onAuthChange ( _user ) // if someone's subscribed to auth changes, let 'em know
26
+ return _user // so that we can continue chaining
27
+ }
31
28
/******* OPERATIONS */
32
29
// make sure the Registration preferences under Identity settings in your Netlify dashboard are set to Open.
33
30
const signupUser = ( email : string , password : string , data : Object ) =>
34
- goTrueInstance . signup ( email , password , data ) . then ( _setUser ) ; // TODO: make setUser optional?
35
- const loginUser = ( email : string , password : string ) =>
36
- goTrueInstance . login ( email , password ) . then ( _setUser ) ;
37
- const requestPasswordRecovery = ( email : string ) =>
38
- goTrueInstance . requestPasswordRecovery ( email ) ;
39
- const recoverAccount = ( token : string , remember ?: boolean | undefined ) =>
40
- goTrueInstance . recover ( token , remember ) ;
31
+ goTrueInstance . signup ( email , password , data ) . then ( _setUser ) // TODO: make setUser optional?
32
+ const loginUser = ( email : string , password : string ) => goTrueInstance . login ( email , password ) . then ( _setUser )
33
+ const requestPasswordRecovery = ( email : string ) => goTrueInstance . requestPasswordRecovery ( email )
34
+ const recoverAccount = ( token : string , remember ?: boolean | undefined ) => goTrueInstance . recover ( token , remember )
41
35
const updateUser = ( fields : Object ) => {
42
36
if ( user == null ) {
43
- throw new Error ( 'No current user found - are you logged in?' ) ;
37
+ throw new Error ( 'No current user found - are you logged in?' )
44
38
} else {
45
39
return user !
46
40
. update ( fields ) // e.g. { email: "example@example.com", password: "password" }
47
- . then ( _setUser ) ;
41
+ . then ( _setUser )
48
42
}
49
- } ;
43
+ }
50
44
const getFreshJWT = ( ) => {
51
- if ( ! user ) throw new Error ( 'No current user found - are you logged in?' ) ;
52
- return user . jwt ( ) ;
53
- } ;
45
+ if ( ! user ) throw new Error ( 'No current user found - are you logged in?' )
46
+ return user . jwt ( )
47
+ }
54
48
const logoutUser = ( ) => {
55
- if ( ! user ) throw new Error ( 'No current user found - are you logged in?' ) ;
56
- return user . logout ( ) . then ( ( ) => _setUser ( undefined ) ) ;
57
- } ;
49
+ if ( ! user ) throw new Error ( 'No current user found - are you logged in?' )
50
+ return user . logout ( ) . then ( ( ) => _setUser ( undefined ) )
51
+ }
58
52
59
- const genericAuthedFetch = ( method : string ) => (
60
- endpoint : string ,
61
- obj = { }
62
- ) => {
63
- if ( ! user || ! user . token || ! user . token . access_token )
64
- throw new Error ( 'no user token found' ) ;
53
+ const genericAuthedFetch = ( method : string ) => ( endpoint : string , obj = { } ) => {
54
+ if ( ! user || ! user . token || ! user . token . access_token ) throw new Error ( 'no user token found' )
65
55
const defaultObj = {
66
56
headers : {
67
57
Accept : 'application/json' ,
68
58
'Content-Type' : 'application/json' ,
69
59
Authorization : 'Bearer ' + user . token . access_token
70
60
}
71
- } ;
72
- const finalObj = Object . assign ( defaultObj , { method } , obj ) ;
61
+ }
62
+ const finalObj = Object . assign ( defaultObj , { method } , obj )
73
63
return fetch ( endpoint , finalObj ) . then ( res =>
74
64
finalObj . headers [ 'Content-Type' ] === 'application/json' ? res . json ( ) : res
75
- ) ;
76
- } ;
65
+ )
66
+ }
77
67
const authedFetch = {
78
68
get : genericAuthedFetch ( 'GET' ) ,
79
69
post : genericAuthedFetch ( 'POST' ) ,
80
70
put : genericAuthedFetch ( 'PUT' ) ,
81
71
delete : genericAuthedFetch ( 'DELETE' )
82
- } ;
72
+ }
83
73
84
74
// // confirmation
85
75
// http://lea.verou.me/2011/05/get-your-hash-the-bulletproof-way/
86
76
React . useEffect ( ( ) => {
87
- const hash = window . location . hash . substring ( 1 ) ;
77
+ const hash = window . location . hash . substring ( 1 )
88
78
if ( hash . slice ( 0 , 19 ) === 'confirmation_token=' ) {
89
79
// we are in a confirmation!
90
- const token = hash . slice ( 19 ) ;
80
+ const token = hash . slice ( 19 )
91
81
goTrueInstance
92
82
. confirm ( token )
93
83
. then ( _setUser )
94
- . catch ( console . error ) ;
84
+ . catch ( console . error )
95
85
// .then(
96
86
// () =>
97
87
// (window.location =
98
88
// window.location.origin + window.location.pathname) // strip hash
99
89
// )
100
90
}
101
- } , [ ] ) ;
91
+ } , [ ] )
102
92
103
93
/******* hook API */
104
94
return {
@@ -116,5 +106,5 @@ export function useNetlifyIdentity(
116
106
authedFetch,
117
107
_goTrueInstance : goTrueInstance ,
118
108
_domain : domain
119
- } ;
109
+ }
120
110
}
0 commit comments