Skip to content

Commit

Permalink
Feat: improved account deletion function, added check to prevent user…
Browse files Browse the repository at this point in the history
…s who have requested account deletion to login via password recovery
  • Loading branch information
wiebken-n committed Feb 26, 2024
1 parent 307cb6b commit b60813e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/components/UserAuth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ const resetPassword = async () => {
const checkForDeletionRequest = async (userId) => {
router.push('/welcome')
const { data, error } = await supabase.from('deletion_requests').select().eq('user_id', userId)
const { data, error } = await supabase
.from('profiles')
.select()
.eq('id', userId)
.eq('account_deleted', true)
if (error) {
console.log(error)
}
Expand Down
1 change: 1 addition & 0 deletions src/views/HerderView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ async function searchUser() {
.select()
.ilike('username', `%${userName.value}%`)
.neq('id', userStore.state.userId)
.neq('account_deleted', true)
if (error) {
console.log(error)
Expand Down
49 changes: 47 additions & 2 deletions src/views/ResetPasswordView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@
</template>
<script setup>
import { supabase } from '../supabase'
import { onBeforeMount, ref, toRefs } from 'vue'
import { onBeforeMount, onMounted, ref, toRefs } from 'vue'
import { useUserStore } from '../stores/useUserStore'
import { useDeletionStore } from '../stores/useDeletionStore'
import Toast from 'primevue/toast'
import { useToast } from 'primevue/usetoast'
import { useRouter } from 'vue-router'
const deletionStore = useDeletionStore()
const props = defineProps(['session'])
const { session } = toRefs(props)
Expand Down Expand Up @@ -82,9 +85,51 @@ const changePassword = async () => {
}
}
onBeforeMount(() => {
async function signOut() {
const { error, data } = await supabase.auth.signOut()
if (error) {
console.log(error)
}
if (data) {
console.log(data)
}
}
const checkIfUserActive = async () => {
const { data, error } = await supabase
.from('profiles')
.select()
.eq('id', userStore.state.userId)
.eq('account_deleted', true)
if (error) {
console.log(error)
}
if (data) {
// console.log(data)
if (data.length > 0) {
deletionStore.state.deletionActive = true
signOut()
router.push('/welcome')
setTimeout(() => {
toast.add({
severity: 'warn',
summary: 'Account geschlossen',
detail: 'Dieser Account ist inaktiv und wird in Kürze gelöscht werden',
life: 5000
})
}, 1000)
}
}
}
onBeforeMount(async () => {
userStore.getProfile(session)
})
onMounted(async () => {
await checkIfUserActive()
})
</script>

<style scoped>
Expand Down
61 changes: 61 additions & 0 deletions src/views/UserAccountView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,72 @@ const addDeletionRequest = async () => {
console.log(error)
}
if (data) {
// console.log(data)
await deleteCats()
await deleteHerderConnections()
await deleteUserConnections()
await changeUserData()
setTimeout(() => {
signOut()
}, 5000)
}
}
const changeUserData = async () => {
const { data, error } = await supabase
.from('profiles')
.update({ username: userStore.state.username + ' (gelöscht)', account_deleted: true })
.eq('id', userStore.state.userId)
if (error) {
console.log(error)
}
if (data) {
console.log(data)
}
}
const deleteCats = async () => {
const { data, error } = await supabase
.from('cats')
.delete()
.eq('user_id', userStore.state.userId)
.select()
if (error) {
console.log(error)
}
if (data) {
// console.log(data)
}
}
const deleteHerderConnections = async () => {
const { data, error } = await supabase
.from('herder_connections')
.delete()
.eq('herder_id', userStore.state.userId)
.select()
if (error) {
console.log(error)
}
if (data) {
// console.log(data)
}
}
const deleteUserConnections = async () => {
const { data, error } = await supabase
.from('user_connections')
.delete()
.or(`user_active.eq.${userStore.state.userId},user_passive.eq.${userStore.state.userId}`)
.select()
if (error) {
console.log(error)
}
if (data) {
// console.log(data)
}
}
onBeforeMount(() => {
userStore.getProfile(session)
})
Expand Down

0 comments on commit b60813e

Please sign in to comment.