-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.html
182 lines (157 loc) · 5.4 KB
/
index.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Firebase Authentication</title>
<script src="https://www.gstatic.com/firebasejs/4.13.0/firebase.js"></script>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
<h1>Vue School - Firebase Authentication</h1>
<div v-if="authUser">
<h2>Signed in as {{authUser.email}}
<img v-if="linkedGoogle" src="https://www.gstatic.com/mobilesdk/160512_mobilesdk/auth_service_google.svg" alt="">
<img v-if="linkedPassword" src="https://www.gstatic.com/mobilesdk/160409_mobilesdk/images/auth_service_email.svg" alt="">
</h2>
<img :src="authUser.photoURL" height="150">
<p>👩🍳 Hi, {{authUser.displayName || 'friend'}} we know you like {{authUser.favoriteFood || 'food'}}.</p>
<button @click="signOut">Sign out</button>
<button v-if="!linkedGoogle" @click="linkGoogle">Link Google Account</button>
<button v-else @click="unlinkGoogle">Unlink Google Account</button>
<form @submit.prevent="updateProfile">
<h2>Update Profile</h2>
<input v-model="displayName" placeholder="Your name">
<input v-model="photoURL" placeholder="Your photo url">
<button>Update</button>
</form>
<form @submit.prevent="updateCustomDetails">
<h2>Update Additional Details</h2>
<input v-model="favoriteFood" placeholder="Your favorite food">
<button>Update</button>
</form>
<form @submit.prevent="updateEmail">
<h2>Update Email</h2>
<input v-model="email" placeholder="Your email">
<button>Update</button>
</form>
<form @submit.prevent="updatePassword">
<h2>Update Password</h2>
<input type="password" v-model="newPassword" placeholder="Your password">
<button>Update</button>
</form>
</div>
<div v-else>
<form @submit.prevent="register">
<h2>Register</h2>
<input type="email" v-model="email" placeholder="Type your email">
<input type="password" v-model="password" placeholder="Pick your password">
<button>Register</button>
</form>
<form @submit.prevent="signIn">
<h2>Sign in</h2>
<input type="email" v-model="email" placeholder="Type your email">
<input type="password" v-model="password" placeholder="Type your password">
<button>Sign In</button>
</form>
<div>
<h2>Sign in with Google</h2>
<button @click="signInWithGoogle">Sign In</button>
</div>
</div>
</div>
<script>
// Initialize Firebase
const config = {
apiKey: "AIzaSyCcT0BUbiZiKUuCw3AtB4U8UExHQkjI2e8",
authDomain: "vueschool-firebase-au.firebaseapp.com",
databaseURL: "https://vueschool-firebase-au.firebaseio.com",
projectId: "vueschool-firebase-au",
storageBucket: "vueschool-firebase-au.appspot.com",
messagingSenderId: "795856805888"
}
firebase.initializeApp(config)
new Vue({
el: '#app',
data: {
email: '',
password: '',
displayName: null,
photoURL: null,
newPassword: null,
favoriteFood: null,
authUser: null
},
computed: {
linkedGoogle () {
return !!this.authUser.providerData.find(provider => provider.providerId === 'google.com')
},
linkedPassword () {
return !!this.authUser.providerData.find(provider => provider.providerId === 'password')
}
},
methods: {
register () {
firebase.auth().createUserWithEmailAndPassword(this.email, this.password)
.catch(error => alert('🤕' + error.message))
},
signOut () {
firebase.auth().signOut()
},
signIn () {
firebase.auth().signInWithEmailAndPassword(this.email, this.password)
.catch(error => alert('🤕' + error.message))
},
signInWithGoogle () {
const provider = new firebase.auth.GoogleAuthProvider()
firebase.auth().signInWithPopup(provider)
.catch(error => alert('🤕' + error.message))
.then(data => console.log(data.user, data.credential.accessToken))
},
linkGoogle () {
const provider = new firebase.auth.GoogleAuthProvider()
this.authUser.linkWithPopup(provider)
.catch(error => alert('🤕' + error.message))
},
unlinkGoogle () {
this.authUser.unlink('google.com')
},
updateProfile () {
this.authUser.updateProfile({
displayName: this.displayName,
photoURL: this.photoURL
})
},
updateCustomDetails () {
firebase.database().ref('users').child(this.authUser.uid)
.update({favoriteFood: this.favoriteFood})
},
updateEmail () {
this.authUser.updateEmail(this.email)
},
updatePassword () {
this.authUser.updatePassword(this.newPassword)
.then(() => { this.newPassword = null })
.catch(error => alert('🤕' + error.message))
}
},
created () {
firebase.auth().onAuthStateChanged(user => {
this.authUser = user
if (user) {
this.displayName = user.displayName
this.photoURL = user.photoURL
this.email = user.email
firebase.database().ref('users').child(user.uid).once('value', snapshot => {
if (snapshot.val()) {
this.favoriteFood = snapshot.val().favoriteFood
Vue.set(this.authUser, 'favoriteFood', this.favoriteFood)
}
})
}
})
}
})
</script>
</body>
</html>