-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignup.html
101 lines (95 loc) · 3.7 KB
/
signup.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<html>
<head>
<title>Sign Up</title>
<link rel="stylesheet" href="styles/style.css">
</head>
<body>
<div class="container">
<img class="logo" src="images/logo.png" alt="Logo">
<h1>Sign Up</h1>
<form id="signupForm">
<input type="text" id="username" placeholder="Username" required>
<input type="email" id="email" placeholder="Email" required>
<input type="password" id="password" placeholder="Password" required>
<input type="submit" value="SUBMIT" class="sub" id="submit">
<a href="#" onclick="signUpWithGoogle()" class="button">Sign Up with Google</a>
</form>
</div>
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.1/firebase-app.js";
import { getAuth, createUserWithEmailAndPassword, signInWithPopup, GoogleAuthProvider } from "https://www.gstatic.com/firebasejs/10.12.1/firebase-auth.js";
import { getDatabase, ref, set } from "https://www.gstatic.com/firebasejs/10.12.1/firebase-database.js";
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyCQ2pqMF9AGpqswdTeahsoDC1M57aErafM",
authDomain: "job-board-c0cba.firebaseapp.com",
projectId: "job-board-c0cba",
storageBucket: "job-board-c0cba.appspot.com",
messagingSenderId: "952776217509",
appId: "1:952776217509:web:ae94c4e0ec99e9b9f9ce4c",
measurementId: "G-TG7JQNR5J4"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const database = getDatabase(app);
// Sign up with email and password
document.getElementById("signupForm").addEventListener('submit', function(e) {
e.preventDefault();
const username = document.getElementById("username").value;
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// User created successfully
const user = userCredential.user;
set(ref(database, 'users/' + user.uid), {
username: username,
email: email,
uid: user.uid
})
.then(() => {
alert("Successful registration");
window.location.href = 'login.html'; // Redirect to login page
})
.catch((error) => {
console.error("Error writing to database: ", error);
});
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(`Error: ${errorMessage}`);
});
});
// Sign up with Google
function signUpWithGoogle() {
const provider = new GoogleAuthProvider();
signInWithPopup(auth, provider)
.then((result) => {
// Google sign-in successful
const user = result.user;
set(ref(database, 'users/' + user.uid), {
username: user.displayName,
email: user.email,
uid: user.uid
})
.then(() => {
alert("Sign up with Google successful");
window.location.href = 'login.html'; // Redirect to login page
})
.catch((error) => {
console.error("Error writing to database: ", error);
});
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(`Error: ${errorMessage}`);
});
}
</script>
</body>
</html>