Merge pull request #49 from 0hAodha/userAccounts

Implement password reset
This commit is contained in:
2023-03-13 09:43:20 +00:00
committed by GitHub
3 changed files with 65 additions and 27 deletions

View File

@ -1,21 +1,22 @@
<template> <template>
<Navbar /> <Navbar />
<h1>Account Settings</h1> <h1>Account Settings</h1>
<p>Your Email: {{ displayEmail }}</p> <p v-if="this.user">Your Email: {{ this.user.email }}</p>
<div> <div>
<p>Enter your current password to edit account settings</p> <h3>Enter your current password to edit account settings</h3>
<input type="password" v-model="currentPassword" placeholder="Enter existing password"> <input type="password" v-model="currentPassword" placeholder="Enter existing password">
<h3>Send a password reset email</h3>
<input @click="resetPasswordEmail" type="submit" name="" value="Send Password reset email">
<h1>Change email</h1> <h3>Change email</h3>
<input type="email" v-model="newEmail" aria-describedby="emailHelp" placeholder="Enter new email"> <input type="email" v-model="newEmail" aria-describedby="emailHelp" placeholder="Enter new email">
<input @click="updateUserEmail" type="submit" name="" value="Update Email"> <input @click="updateUserEmail" type="submit" name="" value="Update Email">
<h1>Change password</h1> <h3>Change password</h3>
<input type="password" v-model="newPassword" placeholder="Enter new password"> <input type="password" v-model="newPassword" placeholder="Enter new password">
<input @click="updateUserPassword" type="submit" name="" value="Update Password"> <input @click="updateUserPassword" type="submit" name="" value="Update Password">
<h1>Delete account</h1> <h3>Delete account</h3>
<input @click="deleteUserAccount" type="submit" name="" value="Delete Account"> <input @click="deleteUserAccount" type="submit" name="" value="Delete Account">
</div> </div>
@ -28,9 +29,8 @@
import Navbar from '../components/Navbar.vue' import Navbar from '../components/Navbar.vue'
import app from '../api/firebase' import app from '../api/firebase'
import { getFunctions, httpsCallable, connectFunctionsEmulator } from "firebase/functions" import { getFunctions, httpsCallable, connectFunctionsEmulator } from "firebase/functions"
import { getAuth, updateEmail, updatePassword, deleteUser, reauthenticateWithCredential, EmailAuthProvider } from "firebase/auth"; import { getAuth, updateEmail, updatePassword, deleteUser, reauthenticateWithCredential, EmailAuthProvider, sendPasswordResetEmail } from "firebase/auth";
const auth = getAuth(); const auth = getAuth();
const user = auth.currentUser;
export default { export default {
name: "AccountPage", name: "AccountPage",
@ -41,7 +41,7 @@ export default {
data() { data() {
return { return {
displayEmail: "", user: null,
newEmail: "", newEmail: "",
newPassword: "", newPassword: "",
currentPassword: "", currentPassword: "",
@ -55,10 +55,7 @@ export default {
}, },
created() { created() {
const user = auth.currentUser this.user = auth.currentUser
this.displayEmail = user.email
const functions = getFunctions(app) const functions = getFunctions(app)
let host = window.location.hostname let host = window.location.hostname
if (host === '127.0.0.1' || host === 'localhost') { if (host === '127.0.0.1' || host === 'localhost') {
@ -104,8 +101,8 @@ export default {
} }
this.authenticateUser(this.currentPassword).then(() => { this.authenticateUser(this.currentPassword).then(() => {
if (!this.reAuthSuccessful) return if (!this.reAuthSuccessful) return
updateEmail(user, this.newEmail).then(() => { updateEmail(this.user, this.newEmail).then(() => {
this.displayEmail = this.newEmail // might need to reset user here
this.resetCredentials() this.resetCredentials()
this.FirebaseSuccessMsg = "Email updated" this.FirebaseSuccessMsg = "Email updated"
this.displayFirebaseSuccessMsg = true this.displayFirebaseSuccessMsg = true
@ -125,7 +122,7 @@ export default {
} }
this.authenticateUser(this.currentPassword).then(() => { this.authenticateUser(this.currentPassword).then(() => {
if (!this.reAuthSuccessful) return if (!this.reAuthSuccessful) return
updatePassword(user, this.newPassword).then(() => { updatePassword(this.user, this.newPassword).then(() => {
this.resetCredentials() this.resetCredentials()
this.FirebaseSuccessMsg = "Password updated" this.FirebaseSuccessMsg = "Password updated"
this.displayFirebaseSuccessMsg = true this.displayFirebaseSuccessMsg = true
@ -145,7 +142,7 @@ export default {
} }
this.authenticateUser(this.currentPassword).then(() => { this.authenticateUser(this.currentPassword).then(() => {
if (!this.reAuthSuccessful) return if (!this.reAuthSuccessful) return
deleteUser(user).then(() => { deleteUser(this.user).then(() => {
this.resetCredentials() this.resetCredentials()
this.FirebaseSuccessMsg = "Account deleted" this.FirebaseSuccessMsg = "Account deleted"
this.displayFirebaseSuccessMsg = true this.displayFirebaseSuccessMsg = true
@ -156,6 +153,17 @@ export default {
this.displayFirebaseError = true this.displayFirebaseError = true
}) })
}) })
},
resetPasswordEmail() {
sendPasswordResetEmail(auth, this.user.email).then(() => {
this.FirebaseSuccessMsg = "Reset password email sent"
this.displayFirebaseSuccessMsg = true
})
.catch((error) => {
this.FirebaseError = error.message
this.displayFirebaseError = true
})
} }
} }
} }

View File

@ -3,22 +3,36 @@
<div id="background"> <div id="background">
<div class="loginbox"> <div class="loginbox">
<img src="https://cdn.discordapp.com/attachments/1017419092447207436/1063092138029625394/pixil-frame-0.png" class="avatar"> <img src="https://cdn.discordapp.com/attachments/1017419092447207436/1063092138029625394/pixil-frame-0.png" class="avatar">
<h1>Login</h1> <div v-if="!forgotPassword">
<p>Email Address</p> <h1>Login</h1>
<input type="email" v-model="email" aria-describedby="emailHelp" placeholder="Enter email"> <p>Email Address</p>
<p>Password</p> <input type="email" v-model="email" aria-describedby="emailHelp" placeholder="Enter email">
<input type="password" v-model="password" placeholder="Enter password"> <p>Password</p>
<input @click="login" type="submit" name="" value="Login"> <input type="password" v-model="password" placeholder="Enter password">
<a><router-link to="/signup">Don't have an account?</router-link></a> <input @click="login" type="submit" name="" value="Login">
<a><router-link to="/signup">Don't have an account?</router-link></a>
<a @click="forgotPassword = !forgotPassword; this.email = ''">Forgot password?</a>
</div>
<div v-else>
<h1>Forgot Password</h1>
<p>Email Address</p>
<input type="email" v-model="email" aria-describedby="emailHelp" placeholder="Enter email">
<input @click="resetPasswordEmail" type="submit" name="" value="Send Reset Email">
<a @click="forgotPassword = !forgotPassword; this.email = ''">Go back</a>
</div>
</div> </div>
<p v-if="displayFirebaseError">{{ FirebaseError }}</p> <p v-if="displayFirebaseError">{{ FirebaseError }}</p>
<p v-if="displayFirebaseSuccessMsg">{{ FirebaseSuccessMsg }}</p>
</div> </div>
</template> </template>
<script> <script>
import app from '../api/firebase'; import app from '../api/firebase';
import {getAuth, signInWithEmailAndPassword} from "firebase/auth" import { getAuth, signInWithEmailAndPassword, sendPasswordResetEmail } from "firebase/auth"
import Navbar from '../components/Navbar.vue' import Navbar from '../components/Navbar.vue'
const auth = getAuth()
export default { export default {
name: "LoginPage", name: "LoginPage",
@ -28,7 +42,10 @@ export default {
email: "", email: "",
password: "", password: "",
displayFirebaseError: false, displayFirebaseError: false,
FirebaseError: "" FirebaseError: "",
displayFirebaseSuccessMsg: false,
FirebaseSuccessMsg: "",
forgotPassword: false
} }
}, },
@ -49,6 +66,18 @@ export default {
this.FirebaseError = error.message this.FirebaseError = error.message
this.displayFirebaseError = true this.displayFirebaseError = true
}) })
},
resetPasswordEmail() {
sendPasswordResetEmail(auth, this.email).then(() => {
this.FirebaseSuccessMsg = "Reset password email sent"
this.displayFirebaseSuccessMsg = true
this.email = ""
})
.catch((error) => {
this.FirebaseError = error.message
this.displayFirebaseError = true
})
} }
} }
} }
@ -105,7 +134,6 @@ h1 {
height: 40px; height: 40px;
color: #fff; color: #fff;
font-size: 16px; font-size: 16px;
} }
.loginbox input[type="submit"]:hover { .loginbox input[type="submit"]:hover {
@ -120,6 +148,7 @@ h1 {
font-size: 12px; font-size: 12px;
line-height: 20px; line-height: 20px;
color: darkgray; color: darkgray;
display: flex
} }
.loginbox a:hover { .loginbox a:hover {

View File

@ -8,6 +8,7 @@
<input type="email" v-model="email" aria-describedby="emailHelp" placeholder="Enter email"> <input type="email" v-model="email" aria-describedby="emailHelp" placeholder="Enter email">
<p>Password</p> <p>Password</p>
<input type="password" v-model="password" placeholder="Enter password"> <input type="password" v-model="password" placeholder="Enter password">
<a>Passwords must have 6 or more characters</a>
<input @click="signup" type="submit" name="" value="Sign Up"> <input @click="signup" type="submit" name="" value="Sign Up">
<a><router-link to="/login">Already have an account?</router-link></a> <a><router-link to="/login">Already have an account?</router-link></a>
</div> </div>