Overview
Sociales uses Firebase for authentication services. This guide walks through setting up Firebase for your project.Prerequisites
- A Google account
- Node.js and npm installed
- Access to the Firebase Console
Creating a Firebase Project
Go to Firebase Console
Navigate to Firebase Console and sign in with your Google account.
Create a new project
Click Add project and follow the wizard:
- Enter a project name (e.g., “Sociales”)
- (Optional) Enable Google Analytics
- Click Create project
Register your web app
In the project overview:
- Click the Web icon (
</>) to add a web app - Enter an app nickname (e.g., “Sociales Web”)
- Check Also set up Firebase Hosting if you want to use Firebase Hosting
- Click Register app
Configuring the Application
Update Firebase Configuration
Opensrc/firebase/config.js and replace the placeholder values with your actual Firebase configuration:
Using Environment Variables (Recommended)
For better security, use Vite’s environment variables:- Create a
.envfile in the project root:
- Update
src/firebase/config.jsto use environment variables:
- Add
.envto.gitignore:
Enabling Authentication
Enable Google Sign-In
- Click the Sign-in method tab
- Find Google in the providers list
- Click the edit icon (pencil)
- Toggle Enable
- Select a support email for the project
- Click Save
Authentication Implementation
The application uses the AuthContext provider located insrc/context/AuthContext.jsx. The context provides:
currentUser- The currently authenticated user object (ornull)loginWithGoogle()- Function to trigger Google Sign-In popuplogout()- Function to sign out the current user
Wrapping Your App
To enable authentication throughout your application, wrap the root component withAuthProvider:
Testing Authentication
Test Google Sign-In
- Navigate to a page that uses authentication
- Click the “Sign in with Google” button
- Complete the Google Sign-In flow
- Verify that the user information displays correctly
Troubleshooting
”Firebase: Error (auth/unauthorized-domain)”
This error occurs when your domain is not authorized in Firebase:- Go to Firebase Console > Authentication > Settings
- Scroll to Authorized domains
- Add your domain (e.g.,
username.github.io)
“Firebase: Error (auth/api-key-not-valid)”
Your API key is incorrect:- Verify the configuration in
src/firebase/config.js - Compare with the config in Firebase Console > Project Settings
- Regenerate the API key if necessary
Authentication works locally but not in production
- Check that your production domain is in the Authorized domains list
- Verify that environment variables are correctly set in your deployment environment
- For GitHub Pages, ensure the domain is
username.github.io
Security Best Practices
Next Steps
- Configure deployment to GitHub Pages
- Review the technical architecture
- Explore Firebase Security Rules for additional services