Skip to content

Commit c03d136

Browse files
committed
Make a few structural changes to #98
1 parent 0aba3cd commit c03d136

10 files changed

+184
-147
lines changed

auth/README.md

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

1313
- [useAuthState](#useauthstate)
14-
- [useRegister](#useregister)
15-
- [useLogin](#uselogin)
14+
- [useCreateUserWithEmailAndPassword](#usecreateuserwithemailandpassword)
15+
- [useSignInWithEmailAndPassword](#usesigninwithemailandpassword)
1616

1717
### useAuthState
1818

@@ -32,7 +32,7 @@ Returns:
3232
- `loading`: A `boolean` to indicate whether the the authentication state is still being loaded
3333
- `error`: Any `firebase.auth.Error` returned by Firebase when trying to load the user, or `undefined` if there is no error
3434

35-
#### If you are registering or logingIn the user for the first time consider using [useRegister](#useregister), [useLogin](#uselogin)
35+
#### If you are registering or signing in the user for the first time consider using [useCreateUserWithEmailAndPassword](#usecreateuserwithemailandpassword), [useSignInWithEmailAndPassword](#usesigninwithemailandpassword)
3636

3737
#### Full Example
3838

@@ -75,79 +75,121 @@ const CurrentUser = () => {
7575
};
7676
```
7777

78-
### useRegister
78+
### useCreateUserWithEmailAndPassword
7979

8080
```js
81-
const [registeredUser, error, register, loading] = useRegister( auth, email, password );
81+
const [
82+
createUserWithEmailAndPassword,
83+
user,
84+
loading,
85+
error,
86+
] = useCreateUserWithEmailAndPassword(auth);
8287
```
8388

84-
Import statement :
89+
Create a user with email and password. Wraps the underlying `firebase.auth().createUserWithEmailAndPassword` method and provides additional `loading` and `error` information.
8590

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:
91+
The `useCreateUserWithEmailAndPassword` hook takes the following parameters:
9592

9693
- `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
9994

10095
Returns:
10196

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
97+
- `createUserWithEmailAndPassword(email: string, password: string)`: a function you can call to start the registration
98+
- `user`: The `firebase.User` if the user was created or `undefined` if not
99+
- `loading`: A `boolean` to indicate whether the the user creation is processing
100+
- `error`: Any `firebase.auth.Error` returned by Firebase when trying to create the user, or `undefined` if there is no error
106101

107-
### useLogin
102+
#### Full Example
108103

109-
```js
110-
const [loggedInUser, error, login, loading] = useLogin(auth, email, password);
104+
```jsx
105+
import { useSignInWithEmailAndPassword } from 'react-firebase-hooks/auth';
106+
107+
const SignIn = () => {
108+
const [email, setEmail] = useState('');
109+
const [password, setPassword] = useState('');
110+
const [
111+
createUserWithEmailAndPassword,
112+
user,
113+
loading,
114+
error,
115+
] = useCreateUserWithEmailAndPassword(auth);
116+
117+
if (error) {
118+
return (
119+
<div>
120+
<p>Error: {error.message}</p>
121+
</div>
122+
);
123+
}
124+
if (loading) {
125+
return <p>Loading...</p>;
126+
}
127+
if (user) {
128+
return (
129+
<div>
130+
<p>Registered User: {user.email}</p>
131+
</div>
132+
);
133+
}
134+
return (
135+
<div className="App">
136+
<input
137+
type="email"
138+
value={email}
139+
onChange={(e) => setEmail(e.target.value)}
140+
/>
141+
<input
142+
type="password"
143+
value={password}
144+
onChange={(e) => setPassword(e.target.value)}
145+
/>
146+
<button onClick={() => createUserWithEmailAndPassword(email, password)}>
147+
Register
148+
</button>
149+
</div>
150+
);
151+
};
111152
```
112153

113-
Import statement :
154+
### useSignInWithEmailAndPassword
114155

115156
```js
116-
import { useLogin } from 'react-firebase-hooks/auth';
157+
const [
158+
signInWithEmailAndPassword,
159+
user,
160+
loading,
161+
error,
162+
] = useSignInWithEmailAndPassword(auth, email, password);
117163
```
118164

119-
For full full example [check here](#register-and-login-hook-usage)
165+
Login a user with email and password. Wraps the underlying `firebase.auth().signInWithEmailAndPassword` method and provides additional `loading` and `error` information.
120166

121-
Register a user and receive the user credentials
122-
123-
The `useLogin` hook takes the following parameters:
167+
The `useSignInWithEmailAndPassword` hook takes the following parameters:
124168

125169
- `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
128170

129171
Returns:
130172

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
173+
- `signInWithEmailAndPassword(email: string, password: string)`: a function you can call to start the login
174+
- `user`: The `firebase.User` if the user was logged in or `undefined` if not
175+
- `loading`: A `boolean` to indicate whether the the user login is processing
176+
- `error`: Any `firebase.auth.Error` returned by Firebase when trying to login the user, or `undefined` if there is no error
135177

136-
## Register and Login hook usage
178+
#### Full Example
137179

138180
```jsx
139-
import React, { useState } from 'react';
140-
import { auth } from './firebase';
141-
import { useLogin, useRegister } from 'react-firebase-hooks/auth';
181+
import { useSignInWithEmailAndPassword } from 'react-firebase-hooks/auth';
142182

143-
function App() {
183+
const SignIn = () => {
144184
const [email, setEmail] = useState('');
145185
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-
186+
const [
187+
signInWithEmailAndPassword,
188+
user,
189+
loading,
190+
error,
191+
] = useSignInWithEmailAndPassword(auth);
192+
151193
if (error) {
152194
return (
153195
<div>
@@ -158,17 +200,10 @@ function App() {
158200
if (loading) {
159201
return <p>Loading...</p>;
160202
}
161-
if (loggedInUser) {
162-
return (
163-
<div>
164-
<p>Currently LoggedIn User: {loggedInUser.email}</p>
165-
</div>
166-
);
167-
}
168-
if (registeredUser) {
203+
if (user) {
169204
return (
170205
<div>
171-
<p>Currently Registered User: {loggedInUser.email}</p>
206+
<p>Signed In User: {user.email}</p>
172207
</div>
173208
);
174209
}
@@ -184,11 +219,10 @@ function App() {
184219
value={password}
185220
onChange={(e) => setPassword(e.target.value)}
186221
/>
187-
<button onClick={login}>SIGN IN</button>
188-
<button onClick={register}>SIGN UP</button>
222+
<button onClick={() => signInWithEmailAndPassword(email, password)}>
223+
Sign In
224+
</button>
189225
</div>
190226
);
191-
}
192-
193-
export default App;
227+
};
194228
```

auth/index.ts

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

auth/types.ts

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

auth/useLogin.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

auth/useRegister.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

auth/useSignInWithEmailAndPassword.ts

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

0 commit comments

Comments
 (0)