Skip to content

Commit a498c91

Browse files
authored
Merge pull request #5 from cobuildlab/hotfix/queries-erros
Hotfix/queries erros
2 parents a1a23ce + 10dac20 commit a498c91

6 files changed

Lines changed: 25 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Refresh Token
55
- Auth0 Params
66
- Clean Code
7+
- Test Code
78

89

910
Basic use:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cobuildlab/react-native-auth0",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Todo",
55
"main": "lib/index.js",
66
"types": "./lib/index.d.ts",

src/constant.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
export const USER_QUERY = `
2-
mutation UserSignUp($user: UserCreateInput!, $authProfileId: ID) {
3-
userSignUpWithToken(user: $user, authProfileId: $authProfileId) {
2+
query {
3+
user{
44
id
55
email
66
}
77
}
88
`;
99

1010
export const CREATE_USER_QUERY = (email: string, authProfileId: string): string => (`
11-
mutation UserSignUp{
12-
userSignUpWithToken(user: { email: ${email} }, authProfileId: ${authProfileId}) {
11+
mutation{
12+
userSignUpWithToken(user: { email: "${email}" }, authProfileId: "${authProfileId}") {
1313
id
1414
email
1515
}

src/provider.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AuthProviderProps } from './types';
88

99

1010
export const AuthProvider: React.FC<AuthProviderProps> = ({ children, config }) => {
11-
const { auth0, eichBaseEndpoint, eichBaseToken, eichBaseAuthProfileId, saveCredentials, getCredentials, removeCredentials } = config;
11+
const { auth0, eichBaseEndpoint, eichBaseAuthProfileId, saveCredentials, getCredentials, removeCredentials } = config;
1212
const [credentials, setCredentials] = React.useState<Credentials>();
1313
const [userInfo, setUserInfo] = React.useState<UserInfo>();
1414
const [isAuthenticated, setAuthenticated] = React.useState(false);
@@ -77,16 +77,17 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children, config })
7777
}
7878

7979
const authorizeUserInfo = jwt_decode(authorizeResponse.idToken) as UserInfo;
80+
const token = authorizeResponse.idToken;
8081

8182
const email = authorizeUserInfo?.email;
8283
try {
83-
await fetchUser(eichBaseEndpoint, eichBaseToken);
84+
await fetchUser(eichBaseEndpoint, token);
8485
} catch (error) {
8586
console.log('eichbase error', JSON.stringify(error));
8687
await createUser({
87-
endpoint: eichBaseEndpoint,
88-
token: eichBaseToken,
88+
token,
8989
email,
90+
endpoint: eichBaseEndpoint,
9091
authProfileId: eichBaseAuthProfileId
9192
});
9293
}
@@ -97,11 +98,12 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children, config })
9798
setUserInfo(authorizeUserInfo);
9899
setAuthenticated(true);
99100
setLoading(false);
100-
}, [eichBaseEndpoint, eichBaseToken, auth0, eichBaseAuthProfileId, onSaveToken]);
101+
}, [eichBaseEndpoint, auth0, eichBaseAuthProfileId, onSaveToken]);
101102

102103

103104

104105
const logout = React.useCallback(async () => {
106+
setLoading(true);
105107
try {
106108
await auth0.webAuth.clearSession();
107109
} catch (error) {
@@ -126,8 +128,9 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children, config })
126128
token: storedCredentials.accessToken,
127129
});
128130
} catch (error) {
129-
console.error('Authenticate error', JSON.stringify(error));
131+
console.warn('Authenticate error', JSON.stringify(error));
130132
removeToken();
133+
return;
131134
}
132135

133136
setCredentials(storedCredentials);

src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export type UseAuthType = {
1717
export type AuthProviderConfig = {
1818
auth0: Auth0;
1919
eichBaseEndpoint: string;
20-
eichBaseToken: string;
2120
eichBaseAuthProfileId: string;
2221
getCredentials: () => Promise< Credentials | null>;
2322
removeCredentials: () => Promise<void>;

src/utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { User } from './types';
22
import { USER_QUERY, CREATE_USER_QUERY } from './constant';
33

44

5-
export const fetchUser = async (endpoint: string, token: string): Promise<User> => {
5+
export const fetchUser = async (endpoint: string, token: string): Promise<User | null> => {
66
const request = await fetch(endpoint, {
77
method: "POST",
88
headers: {
@@ -14,6 +14,10 @@ export const fetchUser = async (endpoint: string, token: string): Promise<User>
1414

1515
const response = await request.json();
1616

17+
if(response?.errors){
18+
throw new Error(JSON.stringify(response?.errors));
19+
}
20+
1721
return response as User;
1822
};
1923

@@ -26,6 +30,7 @@ type createUserParams = {
2630

2731
export const createUser = async (params: createUserParams): Promise<User> => {
2832
const { endpoint, token, email, authProfileId } = params;
33+
2934
const request = await fetch(endpoint, {
3035
method: "POST",
3136
headers: {
@@ -39,5 +44,9 @@ export const createUser = async (params: createUserParams): Promise<User> => {
3944

4045
const response = await request.json();
4146

47+
if(response?.errors){
48+
throw new Error(JSON.stringify(response?.errors));
49+
}
50+
4251
return response as User;
4352
};

0 commit comments

Comments
 (0)