Skip to content

Commit 2b28b83

Browse files
committed
16-React Firebase Social Login: Google, Facebook, Twitter
1 parent b47b884 commit 2b28b83

File tree

2 files changed

+173
-1
lines changed

2 files changed

+173
-1
lines changed

src/components/Firebase/firebase.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class Firebase {
1717

1818
this.auth = app.auth();
1919
this.db = app.database();
20+
21+
this.googleProvider = new app.auth.GoogleAuthProvider();
22+
this.facebookProvider = new app.auth.FacebookAuthProvider();
23+
this.twitterProvider = new app.auth.TwitterAuthProvider();
2024
}
2125

2226
// *** Auth API ***
@@ -27,6 +31,15 @@ class Firebase {
2731
doSignInWithEmailAndPassword = (email, password) =>
2832
this.auth.signInWithEmailAndPassword(email, password);
2933

34+
doSignInWithGoogle = () =>
35+
this.auth.signInWithPopup(this.googleProvider);
36+
37+
doSignInWithFacebook = () =>
38+
this.auth.signInWithPopup(this.facebookProvider);
39+
40+
doSignInWithTwitter = () =>
41+
this.auth.signInWithPopup(this.twitterProvider);
42+
3043
doSignOut = () => this.auth.signOut();
3144

3245
doPasswordReset = email => this.auth.sendPasswordResetEmail(email);

src/components/SignIn/index.js

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const SignInPage = () => (
1111
<div>
1212
<h1>SignIn</h1>
1313
<SignInForm />
14+
<SignInGoogle />
15+
<SignInFacebook />
16+
<SignInTwitter />
1417
<PasswordForgetLink />
1518
<SignUpLink />
1619
</div>
@@ -80,11 +83,167 @@ class SignInFormBase extends Component {
8083
}
8184
}
8285

86+
class SignInGoogleBase extends Component {
87+
constructor(props) {
88+
super(props);
89+
90+
this.state = { error: null };
91+
}
92+
93+
onSubmit = event => {
94+
this.props.firebase
95+
.doSignInWithGoogle()
96+
.then(socialAuthUser => {
97+
// Create a user in your Firebase Realtime Database too
98+
this.props.firebase
99+
.user(socialAuthUser.user.uid)
100+
.set({
101+
username: socialAuthUser.user.displayName,
102+
email: socialAuthUser.user.email,
103+
roles: [],
104+
})
105+
.then(() => {
106+
this.setState({ error: null });
107+
this.props.history.push(ROUTES.HOME);
108+
})
109+
.catch(error => {
110+
this.setState({ error });
111+
});
112+
})
113+
.catch(error => {
114+
this.setState({ error });
115+
});
116+
117+
event.preventDefault();
118+
};
119+
120+
render() {
121+
const { error } = this.state;
122+
123+
return (
124+
<form onSubmit={this.onSubmit}>
125+
<button type="submit">Sign In with Google</button>
126+
127+
{error && <p>{error.message}</p>}
128+
</form>
129+
);
130+
}
131+
}
132+
133+
class SignInFacebookBase extends Component {
134+
constructor(props) {
135+
super(props);
136+
137+
this.state = { error: null };
138+
}
139+
140+
onSubmit = event => {
141+
this.props.firebase
142+
.doSignInWithFacebook()
143+
.then(socialAuthUser => {
144+
// Create a user in your Firebase Realtime Database too
145+
this.props.firebase
146+
.user(socialAuthUser.user.uid)
147+
.set({
148+
username: socialAuthUser.additionalUserInfo.profile.name,
149+
email: socialAuthUser.additionalUserInfo.profile.email,
150+
roles: [],
151+
})
152+
.then(() => {
153+
this.setState({ error: null });
154+
this.props.history.push(ROUTES.HOME);
155+
})
156+
.catch(error => {
157+
this.setState({ error });
158+
});
159+
})
160+
.catch(error => {
161+
this.setState({ error });
162+
});
163+
164+
event.preventDefault();
165+
};
166+
167+
render() {
168+
const { error } = this.state;
169+
170+
return (
171+
<form onSubmit={this.onSubmit}>
172+
<button type="submit">Sign In with Facebook</button>
173+
174+
{error && <p>{error.message}</p>}
175+
</form>
176+
);
177+
}
178+
}
179+
180+
class SignInTwitterBase extends Component {
181+
constructor(props) {
182+
super(props);
183+
184+
this.state = { error: null };
185+
}
186+
187+
onSubmit = event => {
188+
this.props.firebase
189+
.doSignInWithTwitter()
190+
.then(socialAuthUser => {
191+
// Create a user in your Firebase Realtime Database too
192+
this.props.firebase
193+
.user(socialAuthUser.user.uid)
194+
.set({
195+
username: socialAuthUser.additionalUserInfo.profile.name,
196+
email: socialAuthUser.additionalUserInfo.profile.email,
197+
roles: [],
198+
})
199+
.then(() => {
200+
this.setState({ error: null });
201+
this.props.history.push(ROUTES.HOME);
202+
})
203+
.catch(error => {
204+
this.setState({ error });
205+
});
206+
})
207+
.catch(error => {
208+
this.setState({ error });
209+
});
210+
211+
event.preventDefault();
212+
};
213+
214+
render() {
215+
const { error } = this.state;
216+
217+
return (
218+
<form onSubmit={this.onSubmit}>
219+
<button type="submit">Sign In with Twitter</button>
220+
221+
{error && <p>{error.message}</p>}
222+
</form>
223+
);
224+
}
225+
}
226+
83227
const SignInForm = compose(
84228
withRouter,
85229
withFirebase,
86230
)(SignInFormBase);
87231

232+
const SignInGoogle = compose(
233+
withRouter,
234+
withFirebase,
235+
)(SignInGoogleBase);
236+
237+
const SignInFacebook = compose(
238+
withRouter,
239+
withFirebase,
240+
)(SignInFacebookBase);
241+
242+
const SignInTwitter = compose(
243+
withRouter,
244+
withFirebase,
245+
)(SignInTwitterBase);
246+
88247
export default SignInPage;
89248

90-
export { SignInForm };
249+
export { SignInForm, SignInGoogle, SignInFacebook, SignInTwitter };

0 commit comments

Comments
 (0)