Skip to content

Commit

Permalink
16-React Firebase Social Login: Google, Facebook, Twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
rwieruch committed Nov 24, 2018
1 parent b47b884 commit 2b28b83
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/components/Firebase/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Firebase {

this.auth = app.auth();
this.db = app.database();

this.googleProvider = new app.auth.GoogleAuthProvider();
this.facebookProvider = new app.auth.FacebookAuthProvider();
this.twitterProvider = new app.auth.TwitterAuthProvider();
}

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

doSignInWithGoogle = () =>
this.auth.signInWithPopup(this.googleProvider);

doSignInWithFacebook = () =>
this.auth.signInWithPopup(this.facebookProvider);

doSignInWithTwitter = () =>
this.auth.signInWithPopup(this.twitterProvider);

doSignOut = () => this.auth.signOut();

doPasswordReset = email => this.auth.sendPasswordResetEmail(email);
Expand Down
161 changes: 160 additions & 1 deletion src/components/SignIn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const SignInPage = () => (
<div>
<h1>SignIn</h1>
<SignInForm />
<SignInGoogle />
<SignInFacebook />
<SignInTwitter />
<PasswordForgetLink />
<SignUpLink />
</div>
Expand Down Expand Up @@ -80,11 +83,167 @@ class SignInFormBase extends Component {
}
}

class SignInGoogleBase extends Component {
constructor(props) {
super(props);

this.state = { error: null };
}

onSubmit = event => {
this.props.firebase
.doSignInWithGoogle()
.then(socialAuthUser => {
// Create a user in your Firebase Realtime Database too
this.props.firebase
.user(socialAuthUser.user.uid)
.set({
username: socialAuthUser.user.displayName,
email: socialAuthUser.user.email,
roles: [],
})
.then(() => {
this.setState({ error: null });
this.props.history.push(ROUTES.HOME);
})
.catch(error => {
this.setState({ error });
});
})
.catch(error => {
this.setState({ error });
});

event.preventDefault();
};

render() {
const { error } = this.state;

return (
<form onSubmit={this.onSubmit}>
<button type="submit">Sign In with Google</button>

{error && <p>{error.message}</p>}
</form>
);
}
}

class SignInFacebookBase extends Component {
constructor(props) {
super(props);

this.state = { error: null };
}

onSubmit = event => {
this.props.firebase
.doSignInWithFacebook()
.then(socialAuthUser => {
// Create a user in your Firebase Realtime Database too
this.props.firebase
.user(socialAuthUser.user.uid)
.set({
username: socialAuthUser.additionalUserInfo.profile.name,
email: socialAuthUser.additionalUserInfo.profile.email,
roles: [],
})
.then(() => {
this.setState({ error: null });
this.props.history.push(ROUTES.HOME);
})
.catch(error => {
this.setState({ error });
});
})
.catch(error => {
this.setState({ error });
});

event.preventDefault();
};

render() {
const { error } = this.state;

return (
<form onSubmit={this.onSubmit}>
<button type="submit">Sign In with Facebook</button>

{error && <p>{error.message}</p>}
</form>
);
}
}

class SignInTwitterBase extends Component {
constructor(props) {
super(props);

this.state = { error: null };
}

onSubmit = event => {
this.props.firebase
.doSignInWithTwitter()
.then(socialAuthUser => {
// Create a user in your Firebase Realtime Database too
this.props.firebase
.user(socialAuthUser.user.uid)
.set({
username: socialAuthUser.additionalUserInfo.profile.name,
email: socialAuthUser.additionalUserInfo.profile.email,
roles: [],
})
.then(() => {
this.setState({ error: null });
this.props.history.push(ROUTES.HOME);
})
.catch(error => {
this.setState({ error });
});
})
.catch(error => {
this.setState({ error });
});

event.preventDefault();
};

render() {
const { error } = this.state;

return (
<form onSubmit={this.onSubmit}>
<button type="submit">Sign In with Twitter</button>

{error && <p>{error.message}</p>}
</form>
);
}
}

const SignInForm = compose(
withRouter,
withFirebase,
)(SignInFormBase);

const SignInGoogle = compose(
withRouter,
withFirebase,
)(SignInGoogleBase);

const SignInFacebook = compose(
withRouter,
withFirebase,
)(SignInFacebookBase);

const SignInTwitter = compose(
withRouter,
withFirebase,
)(SignInTwitterBase);

export default SignInPage;

export { SignInForm };
export { SignInForm, SignInGoogle, SignInFacebook, SignInTwitter };

0 comments on commit 2b28b83

Please sign in to comment.