This example demonstrates how to use the Auth Gateway SDK with vanilla JavaScript (without React).
- Google Sign-In integration
- User profile display
- Login/logout functionality
- Error handling
- Loading state management
- A Firebase project with Google Sign-In enabled
- The Auth Gateway backend service running
- The Auth Gateway frontend SDK built
- Update the Auth Gateway backend URL in
index.html:
const authBackendUrl = "http://localhost:8000"; // Replace with your Auth Gateway backend URLNote: Firebase configuration is no longer needed in the client application. All Firebase configuration is now handled by the Auth Gateway backend service.
- Build the Auth Gateway frontend SDK:
cd ../../javascript-sdk
npm install
npm run buildYou can run this example using any static file server. For example:
# Using Python's built-in HTTP server
python -m http.server
# Using Node.js http-server
npx http-serverThen open your browser and navigate to the example page (e.g., http://localhost:8000).
This example uses the VanillaAuthClient class from the Auth Gateway SDK to handle authentication:
-
Initialization:
const authClient = new VanillaAuthClient({ authBackendUrl });
-
Auth State Changes:
authClient.onAuthStateChanged((user) => { // Update UI based on user state });
-
Loading State:
authClient.onLoadingStateChanged((isLoading) => { // Show/hide loading indicator });
-
Error Handling:
authClient.onError((error) => { // Display error message });
-
Login:
await authClient.login();
-
Logout:
await authClient.logout();
-
Get Current User:
const user = authClient.getCurrentUser();
Unlike the React implementation which uses React Context and hooks, the vanilla JavaScript implementation:
- Uses an event-based system for state changes
- Provides methods for getting the current state
- Requires manual DOM manipulation for UI updates
- Does not depend on any React-specific features
This makes it suitable for use in any JavaScript application, not just React applications.