1
1
import React from "react"
2
2
3
3
import GoTrue , { User , Settings } from "gotrue-js"
4
+ import { runRoutes } from "./runRoutes"
4
5
5
6
type authChangeParam = ( user ?: User ) => string | void
6
7
@@ -16,6 +17,7 @@ export default function NetlifyIdentity({ children, domain, onAuthChange }: NIPr
16
17
return children ( useNetlifyIdentity ( domain , onAuthChange ) )
17
18
}
18
19
export function useNetlifyIdentity ( domain : string , onAuthChange : authChangeParam = ( ) => { } ) {
20
+ /******** SETUP */
19
21
if ( ! domain || ! validateUrl ( domain ) ) {
20
22
// just a safety check in case a JS user tries to skip this
21
23
throw new Error (
@@ -34,6 +36,13 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
34
36
return _user // so that we can continue chaining
35
37
}
36
38
39
+ React . useEffect ( ( ) => {
40
+ runRoutes ( goTrueInstance , _setUser )
41
+ } , [ ] )
42
+
43
+ /******* OPERATIONS */
44
+ // make sure the Registration preferences under Identity settings in your Netlify dashboard are set to Open.
45
+ // https://react-netlify-identity.netlify.com/login#access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NTY0ODY3MjEsInN1YiI6ImNiZjY5MTZlLTNlZGYtNGFkNS1iOTYzLTQ4ZTY2NDcyMDkxNyIsImVtYWlsIjoic2hhd250aGUxQGdtYWlsLmNvbSIsImFwcF9tZXRhZGF0YSI6eyJwcm92aWRlciI6ImdpdGh1YiJ9LCJ1c2VyX21ldGFkYXRhIjp7ImF2YXRhcl91cmwiOiJodHRwczovL2F2YXRhcnMxLmdpdGh1YnVzZXJjb250ZW50LmNvbS91LzY3NjQ5NTc_dj00IiwiZnVsbF9uYW1lIjoic3d5eCJ9fQ.E8RrnuCcqq-mLi1_Q5WHJ-9THIdQ3ha1mePBKGhudM0&expires_in=3600&refresh_token=OyA_EdRc7WOIVhY7RiRw5w&token_type=bearer
37
46
/******* external oauth */
38
47
type Provider = "bitbucket" | "facebook" | "github" | "gitlab" | "google"
39
48
@@ -45,8 +54,7 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
45
54
goTrueInstance . acceptInviteExternalUrl ( provider , token )
46
55
const settings : ( ) => Promise < Settings > = goTrueInstance . settings . bind ( goTrueInstance )
47
56
48
- /******* OPERATIONS */
49
- // make sure the Registration preferences under Identity settings in your Netlify dashboard are set to Open.
57
+ /******* email auth */
50
58
const signupUser = ( email : string , password : string , data : Object ) =>
51
59
goTrueInstance . signup ( email , password , data ) . then ( _setUser ) // TODO: make setUser optional?
52
60
const loginUser = ( email : string , password : string , remember : boolean = true ) =>
@@ -92,25 +100,6 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
92
100
delete : genericAuthedFetch ( "DELETE" )
93
101
}
94
102
95
- // // confirmation
96
- // http://lea.verou.me/2011/05/get-your-hash-the-bulletproof-way/
97
- React . useEffect ( ( ) => {
98
- const hash = window . location . hash . substring ( 1 )
99
- if ( hash . slice ( 0 , 19 ) === "confirmation_token=" ) {
100
- // we are in a confirmation!
101
- const token = hash . slice ( 19 )
102
- goTrueInstance
103
- . confirm ( token )
104
- . then ( _setUser )
105
- . catch ( console . error )
106
- // .then(
107
- // () =>
108
- // (window.location =
109
- // window.location.origin + window.location.pathname) // strip hash
110
- // )
111
- }
112
- } , [ ] )
113
-
114
103
/******* hook API */
115
104
return {
116
105
user,
0 commit comments