Prompt Title: React 18: Social Auth

Created 1 year ago
Votes 0
Views 184 times
IT
0

Prompt Detail:

To implement authorization via Google, Apple, Twitter, and Facebook in your React 18 TypeScript project, you can use libraries like Firebase or Passport.js to handle the authentication process. In this example, I'll show you how to use Firebase to set up authentication with these four providers.

  1. Install Firebase and its TypeScript typings:
bash
npm install firebase npm install @types/firebase
  1. Initialize Firebase:

Create a firebaseConfig.ts file in your project's src folder and add your Firebase configuration:

typescript
// src/firebaseConfig.ts import firebase from "firebase/app"; import "firebase/auth"; const firebaseConfig = { apiKey: "YOUR_API_KEY", authDomain: "YOUR_AUTH_DOMAIN", projectId: "YOUR_PROJECT_ID", storageBucket: "YOUR_STORAGE_BUCKET", messagingSenderId: "YOUR_MESSAGING_SENDER_ID", appId: "YOUR_APP_ID", }; firebase.initializeApp(firebaseConfig); export default firebase;

Replace the placeholders with your actual Firebase configuration values.

  1. Create a custom React hook for authentication:

Create a useAuth.ts file in your project's src folder:

typescript
// src/useAuth.ts import { useState, useEffect } from "react"; import firebase from "./firebaseConfig"; const useAuth = () => { const [user, setUser] = useState<firebase.User | null>(null); useEffect(() => { const unsubscribe = firebase.auth().onAuthStateChanged((user) => { setUser(user); }); return () => unsubscribe(); }, []); return user; }; export default useAuth;
  1. Implement provider-based authentication:

Create an AuthProvider.ts file in your project's src folder:

typescript
// src/AuthProvider.ts import firebase from "./firebaseConfig"; const signInWithGoogle = () => { const provider = new firebase.auth.GoogleAuthProvider(); return firebase.auth().signInWithPopup(provider); }; const signInWithApple = () => { const provider = new firebase.auth.OAuthProvider("apple.com"); return firebase.auth().signInWithPopup(provider); }; const signInWithTwitter = () => { const provider = new firebase.auth.TwitterAuthProvider(); return firebase.auth().signInWithPopup(provider); }; const signInWithFacebook = () => { const provider = new firebase.auth.FacebookAuthProvider(); return firebase.auth().signInWithPopup(provider); }; export { signInWithGoogle, signInWithApple, signInWithTwitter, signInWithFacebook };
  1. Use the custom hook and authentication providers in your component:
typescript
// src/App.tsx import React from "react"; import useAuth from "./useAuth"; import { signInWithGoogle, signInWithApple, signInWithTwitter, signInWithFacebook } from "./AuthProvider"; const App: React.FC = () => { const user = useAuth(); const handleSignIn = async (signInMethod: () => Promise<firebase.auth.UserCredential>) => { try { await signInMethod(); } catch (error) { console.error("Error signing in:", error); } }; if (!user) { return ( <div> <button onClick={() => handleSignIn(signInWithGoogle)}>Sign in with Google</button> <button onClick={() => handleSignIn(signInWithApple)}>Sign in with Apple</button> <button onClick={() => handleSignIn(signInWithTwitter)}>Sign in with Twitter</button> <button onClick={() => handleSignIn(signInWithFacebook)}>Sign in with Facebook</button> </div> ); } return ( <div> <h1>Welcome, {user.displayName}</h1> </div> ); }; export
avatar
me
Shared 41 prompts
Created 1 year ago

Leave a Comment

Related Tag Prompts

0
0
props drilling
1 year ago 2023-03-09 03:28:27 anik
0
0
Html para Reac.JS
1 year ago 2023-03-09 23:26:32 Tomás Sarmento
0
0
0
0
React Form Submission.
1 year ago 2023-03-17 13:51:57 nikita
0
2
react error
1 year ago 2023-04-07 11:13:29 tayyeb chaudhary
0
0
Changement couleur menu survolé.
9 months ago 2023-07-05 14:20:14 stephane
0
0
React App for screenshot
8 months ago 2023-08-07 11:32:36 Apoorv Gupta
0
0
SSR with React: Pros/Cons
7 months ago 2023-09-04 08:47:46 AK
0
0
React Annotator
6 months ago 2023-10-06 13:12:37 Adarsh
0
0
React Functional
6 months ago 2023-10-06 13:14:08 Adarsh