Skip to content

Commit 0aba3cd

Browse files
authored
Merge pull request #98 from lorstenoplo/fix-auth-bug
Fix auth bug
2 parents 6be3e99 + b23480d commit 0aba3cd

File tree

10 files changed

+222
-14
lines changed

10 files changed

+222
-14
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ npm-debug.log
1313
# Testing output
1414
.nyc_output
1515
coverage
16+
17+
#test files
18+
/example

auth/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { useAuthState } from 'react-firebase-hooks/auth';
1111
List of Auth hooks:
1212

1313
- [useAuthState](#useauthstate)
14+
- [useRegister](#useregister)
15+
- [useLogin](#uselogin)
1416

1517
### useAuthState
1618

@@ -30,6 +32,8 @@ Returns:
3032
- `loading`: A `boolean` to indicate whether the the authentication state is still being loaded
3133
- `error`: Any `firebase.auth.Error` returned by Firebase when trying to load the user, or `undefined` if there is no error
3234

35+
#### If you are registering or logingIn the user for the first time consider using [useRegister](#useregister), [useLogin](#uselogin)
36+
3337
#### Full Example
3438

3539
```js
@@ -70,3 +74,121 @@ const CurrentUser = () => {
7074
return <button onClick={login}>Log in</button>;
7175
};
7276
```
77+
78+
### useRegister
79+
80+
```js
81+
const [registeredUser, error, register, loading] = useRegister( auth, email, password );
82+
```
83+
84+
Import statement :
85+
86+
```js
87+
import { useRegister } from 'react-firebase-hooks/auth';
88+
```
89+
90+
For full full example [check here](#register-and-login-hook-usage)
91+
92+
Register a user and receive the user credentials
93+
94+
The `useRegister` hook takes the following parameters:
95+
96+
- `auth`: `firebase.auth.Auth` instance for the app you would like to monitor
97+
- `email`: `string` email of the user
98+
- `password`: `string` password of the user
99+
100+
Returns:
101+
102+
- `registeredUser`: The `registeredUser` if data is received or `undefined` if not
103+
- `loading`: A `boolean` to indicate whether the the registration is completed or it's yet processing
104+
- `error`: `any` returned by Firebase when trying to register the user, or `undefined` if there is no error
105+
- `register`: `void` a function you can call to start the registration
106+
107+
### useLogin
108+
109+
```js
110+
const [loggedInUser, error, login, loading] = useLogin(auth, email, password);
111+
```
112+
113+
Import statement :
114+
115+
```js
116+
import { useLogin } from 'react-firebase-hooks/auth';
117+
```
118+
119+
For full full example [check here](#register-and-login-hook-usage)
120+
121+
Register a user and receive the user credentials
122+
123+
The `useLogin` hook takes the following parameters:
124+
125+
- `auth`: `firebase.auth.Auth` instance for the app you would like to monitor
126+
- `email`: `string` email of the user
127+
- `password`: `string` password of the user
128+
129+
Returns:
130+
131+
- `loggedInUser`: The `loggedInUser` if data is received or `undefined` if not
132+
- `loading`: A `boolean` to indicate whether the the login process is completed or it's yet processing
133+
- `error`: `any` returned by Firebase when trying to register the user, or `undefined` if there is no error
134+
- `login`: `void` a function you can call to start the login process
135+
136+
## Register and Login hook usage
137+
138+
```jsx
139+
import React, { useState } from 'react';
140+
import { auth } from './firebase';
141+
import { useLogin, useRegister } from 'react-firebase-hooks/auth';
142+
143+
function App() {
144+
const [email, setEmail] = useState('');
145+
const [password, setPassword] = useState('');
146+
const [loggedInUser, error, login, loading] = useLogin(auth, email, password);
147+
const [registeredUser, error, register, loading] = useRegister(auth, email, password);
148+
149+
// Use only one of the above two hooks in one file
150+
151+
if (error) {
152+
return (
153+
<div>
154+
<p>Error: {error.message}</p>
155+
</div>
156+
);
157+
}
158+
if (loading) {
159+
return <p>Loading...</p>;
160+
}
161+
if (loggedInUser) {
162+
return (
163+
<div>
164+
<p>Currently LoggedIn User: {loggedInUser.email}</p>
165+
</div>
166+
);
167+
}
168+
if (registeredUser) {
169+
return (
170+
<div>
171+
<p>Currently Registered User: {loggedInUser.email}</p>
172+
</div>
173+
);
174+
}
175+
return (
176+
<div className="App">
177+
<input
178+
type="email"
179+
value={email}
180+
onChange={(e) => setEmail(e.target.value)}
181+
/>
182+
<input
183+
type="password"
184+
value={password}
185+
onChange={(e) => setPassword(e.target.value)}
186+
/>
187+
<button onClick={login}>SIGN IN</button>
188+
<button onClick={register}>SIGN UP</button>
189+
</div>
190+
);
191+
}
192+
193+
export default App;
194+
```

auth/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export { default as useAuthState, AuthStateHook } from './useAuthState';
2+
export { default as useLogin, loginHook } from './useLogin';
3+
export { default as useRegister, registerHook } from './useRegister';

auth/useAuthState.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ export default (auth: firebase.auth.Auth): AuthStateHook => {
2121
};
2222
}, [auth]);
2323

24-
const resArray:AuthStateHook = [value, loading, error]
25-
return useMemo<AuthStateHook>(
26-
() => resArray,
27-
resArray,
28-
);
24+
const resArray: AuthStateHook = [value, loading, error];
25+
return useMemo<AuthStateHook>(() => resArray, resArray);
2926
};

auth/useLogin.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { useState, useMemo } from 'react';
2+
import firebase from 'firebase/app';
3+
import 'firebase/auth';
4+
import { AuthHookType } from '../util';
5+
6+
export type loginHook = AuthHookType<
7+
firebase.auth.UserCredential,
8+
firebase.FirebaseError
9+
>;
10+
11+
export default (
12+
auth: firebase.auth.Auth,
13+
email: string,
14+
password: string
15+
): loginHook => {
16+
const [error, setError] = useState<firebase.FirebaseError>();
17+
const [
18+
loggedInUser,
19+
setLoggedInUser,
20+
] = useState<firebase.auth.UserCredential>();
21+
const [loading, setLoading] = useState<boolean>(false);
22+
23+
const login = () => {
24+
setLoading(true);
25+
auth
26+
.signInWithEmailAndPassword(email, password)
27+
.then((resUser) => {
28+
setLoggedInUser(resUser);
29+
setLoading(false);
30+
})
31+
.catch((err: firebase.FirebaseError) => {
32+
setError(err);
33+
setLoading(false);
34+
});
35+
};
36+
37+
const resArray: loginHook = [loggedInUser, error, login, loading];
38+
return useMemo<loginHook>(() => resArray, resArray);
39+
};

auth/useRegister.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { useState, useMemo } from 'react';
2+
import firebase from 'firebase/app';
3+
import { AuthHookType } from '../util';
4+
5+
export type registerHook = AuthHookType<
6+
firebase.auth.UserCredential,
7+
firebase.FirebaseError
8+
>;
9+
10+
export default (
11+
auth: firebase.auth.Auth,
12+
email: string,
13+
password: string
14+
): registerHook => {
15+
const [error, setError] = useState<firebase.FirebaseError>();
16+
const [
17+
registeredUser,
18+
setRegisteredUser,
19+
] = useState<firebase.auth.UserCredential>();
20+
const [loading, setLoading] = useState<boolean>(false);
21+
22+
const register = () => {
23+
setLoading(true);
24+
auth
25+
.createUserWithEmailAndPassword(email, password)
26+
.then((resUser) => {
27+
setRegisteredUser(resUser);
28+
setLoading(false);
29+
})
30+
.catch((err: firebase.FirebaseError) => {
31+
setError(err);
32+
setLoading(false);
33+
});
34+
};
35+
36+
const resArray: registerHook = [registeredUser, error, register, loading];
37+
return useMemo<registerHook>(() => resArray, resArray);
38+
};

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"build": "npm run clean && rollup -c",
4444
"clean": "rimraf ./dist ./auth/dist ./auth/*.d.ts ./database/dist ./database/*.d.ts ./firestore/dist ./firestore/*.d.ts ./storage/dist ./storage/*.d.ts ./util/*.d.ts",
4545
"dev": "npm run clean && rollup -c -w",
46-
"prepublish": "npm run build"
46+
"prepublish": "npm run build",
47+
"start": "rollup -c -w"
4748
},
4849
"main": "dist/index.cjs.js",
4950
"module": "dist/index.esm.js",

util/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ export { default as useLoadingValue } from './useLoadingValue';
22
export * from './refHooks';
33

44
export type LoadingHook<T, E> = [T | undefined, boolean, E | undefined];
5+
export type AuthHookType<T, E> = [
6+
T | undefined,
7+
E | undefined,
8+
() => void,
9+
boolean
10+
];

util/useLoadingValue.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ export default <T, E>(getDefaultValue?: () => T): LoadingValue<T, E> => {
8181
setError,
8282
setValue,
8383
value: state.value,
84-
}), [
85-
state.error,
86-
state.loading,
87-
reset,
88-
setError,
89-
setValue,
90-
state.value,
91-
]
84+
}),
85+
[state.error, state.loading, reset, setError, setValue, state.value]
9286
);
9387
};

0 commit comments

Comments
 (0)