forked from swaraj-das/Collect-your-GamingTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request swaraj-das#681 from DeekshaVarshney123/main
Implement User Authentication and Registration Features
- Loading branch information
Showing
7 changed files
with
113 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,56 @@ | ||
|
||
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.14.1/firebase-app.js"; | ||
import { getAuth, signOut, onAuthStateChanged, GoogleAuthProvider, signInWithPopup } from "https://www.gstatic.com/firebasejs/10.14.1/firebase-auth.js"; | ||
|
||
// Firebase configuration | ||
const firebaseConfig = { | ||
apiKey: process.env.FIREBASE_API_KEY, | ||
authDomain: process.env.FIREBASE_AUTH_DOMAIN, | ||
projectId: process.env.FIREBASE_PROJECT_ID, | ||
storageBucket: process.env.FIREBASE_STORAGE_BUCKET, | ||
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID, | ||
appId: process.env.FIREBASE_APP_ID, | ||
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.14.1/firebase-app.js"; | ||
import { getAuth, signOut, onAuthStateChanged, GoogleAuthProvider, signInWithPopup } from "https://www.gstatic.com/firebasejs/10.14.1/firebase-auth.js"; | ||
|
||
// Firebase configuration | ||
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" | ||
}; | ||
|
||
// Initialize Firebase | ||
const app = initializeApp(firebaseConfig); | ||
const auth = getAuth(app); | ||
const provider = new GoogleAuthProvider(); | ||
|
||
|
||
// Initialize Firebase | ||
const app = initializeApp(firebaseConfig); | ||
const auth = getAuth(app); | ||
auth.languageCode = "en"; | ||
const provider = new GoogleAuthProvider(); | ||
|
||
// Sign in with Google | ||
const googleLoginBtn = document.getElementById('google-login-btn'); | ||
googleLoginBtn?.addEventListener('click', function () { | ||
// Sign in with Google | ||
document.getElementById('google-login-btn').addEventListener('click', function () { | ||
signInWithPopup(auth, provider) | ||
.then((result) => { | ||
const user = result.user; | ||
console.log('User logged in:', user); | ||
|
||
// Redirect to the home page | ||
window.location.href = "../index.html"; | ||
}) | ||
.catch((error) => { | ||
console.error('Login error:', error); | ||
}); | ||
}); | ||
|
||
// Handle auth state change and toggle login/logout button | ||
window.addEventListener('load', () => { | ||
.then((result) => { | ||
const user = result.user; | ||
alert(`Welcome ${user.displayName}!`); | ||
window.location.href = "../index.html"; | ||
}) | ||
.catch((error) => { | ||
alert('Login failed: ' + error.message); | ||
}); | ||
}); | ||
|
||
// Handle auth state change | ||
onAuthStateChanged(auth, (user) => { | ||
const authLink = document.getElementById('auth-link'); | ||
|
||
onAuthStateChanged(auth, (user) => { | ||
if (user) { | ||
// If the user is logged in, change the link to "Logout" | ||
if (user) { | ||
authLink.textContent = 'Logout'; | ||
authLink.removeAttribute('href'); | ||
authLink.addEventListener('click', handleLogout); | ||
} else { | ||
// If the user is not logged in, change the link to "Login" | ||
authLink.onclick = handleLogout; | ||
} else { | ||
authLink.textContent = 'Login'; | ||
authLink.setAttribute('href', 'login/login.html'); | ||
authLink.removeEventListener('click', handleLogout); | ||
} | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
// Handle logout | ||
function handleLogout() { | ||
// Handle logout | ||
function handleLogout() { | ||
signOut(auth) | ||
.then(() => { | ||
console.log('User logged out'); | ||
window.location.reload(); // Reload the page if needed | ||
}) | ||
.catch((error) => { | ||
console.error('Logout error:', error); | ||
}); | ||
} | ||
.then(() => { | ||
alert('Logged out successfully!'); | ||
window.location.reload(); | ||
}) | ||
.catch((error) => { | ||
alert('Logout failed: ' + error.message); | ||
}); | ||
} | ||
|
||
|
Oops, something went wrong.