Skip to content

Commit 3b593dd

Browse files
author
sw-yx
committed
pull currentuser on init
1 parent 72e75ba commit 3b593dd

File tree

1 file changed

+40
-50
lines changed

1 file changed

+40
-50
lines changed

src/index.tsx

+40-50
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,94 @@
1-
import React from 'react';
1+
import React from 'react'
22

3-
import GoTrue, { User } from 'gotrue-js';
3+
import GoTrue, { User } from 'gotrue-js'
44

5-
type authChangeParam = (user?: User) => string | void;
5+
type authChangeParam = (user?: User) => string | void
66

77
interface NIProps {
8-
children: any;
9-
domain: string;
10-
onAuthChange?: authChangeParam;
8+
children: any
9+
domain: string
10+
onAuthChange?: authChangeParam
1111
}
1212
const NetlifyIdentity = ({ children, domain, onAuthChange }: NIProps) =>
13-
children(useNetlifyIdentity(domain, onAuthChange));
13+
children(useNetlifyIdentity(domain, onAuthChange))
1414

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 = () => {}) {
2017
const goTrueInstance = new GoTrue({
2118
APIUrl: `${domain}/.netlify/identity`,
2219
setCookie: true
23-
});
20+
})
2421

25-
const [user, setUser] = React.useState<User | undefined>(undefined);
22+
const [user, setUser] = React.useState<User | undefined>(goTrueInstance.currentUser() || undefined)
2623
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+
}
3128
/******* OPERATIONS */
3229
// make sure the Registration preferences under Identity settings in your Netlify dashboard are set to Open.
3330
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)
4135
const updateUser = (fields: Object) => {
4236
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?')
4438
} else {
4539
return user!
4640
.update(fields) // e.g. { email: "example@example.com", password: "password" }
47-
.then(_setUser);
41+
.then(_setUser)
4842
}
49-
};
43+
}
5044
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+
}
5448
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+
}
5852

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')
6555
const defaultObj = {
6656
headers: {
6757
Accept: 'application/json',
6858
'Content-Type': 'application/json',
6959
Authorization: 'Bearer ' + user.token.access_token
7060
}
71-
};
72-
const finalObj = Object.assign(defaultObj, { method }, obj);
61+
}
62+
const finalObj = Object.assign(defaultObj, { method }, obj)
7363
return fetch(endpoint, finalObj).then(res =>
7464
finalObj.headers['Content-Type'] === 'application/json' ? res.json() : res
75-
);
76-
};
65+
)
66+
}
7767
const authedFetch = {
7868
get: genericAuthedFetch('GET'),
7969
post: genericAuthedFetch('POST'),
8070
put: genericAuthedFetch('PUT'),
8171
delete: genericAuthedFetch('DELETE')
82-
};
72+
}
8373

8474
// // confirmation
8575
// http://lea.verou.me/2011/05/get-your-hash-the-bulletproof-way/
8676
React.useEffect(() => {
87-
const hash = window.location.hash.substring(1);
77+
const hash = window.location.hash.substring(1)
8878
if (hash.slice(0, 19) === 'confirmation_token=') {
8979
// we are in a confirmation!
90-
const token = hash.slice(19);
80+
const token = hash.slice(19)
9181
goTrueInstance
9282
.confirm(token)
9383
.then(_setUser)
94-
.catch(console.error);
84+
.catch(console.error)
9585
// .then(
9686
// () =>
9787
// (window.location =
9888
// window.location.origin + window.location.pathname) // strip hash
9989
// )
10090
}
101-
}, []);
91+
}, [])
10292

10393
/******* hook API */
10494
return {
@@ -116,5 +106,5 @@ export function useNetlifyIdentity(
116106
authedFetch,
117107
_goTrueInstance: goTrueInstance,
118108
_domain: domain
119-
};
109+
}
120110
}

0 commit comments

Comments
 (0)