简体   繁体   English

laravel ajax 删除帐户并重新注册无法正常工作

[英]laravel ajax delete account and reregistering not working properly

new to laravel and Jason. laravel 和杰森的新手。 i have this project at hand and i have to give the user the option to delete their account.我手头有这个项目,我必须让用户选择删除他们的帐户。

now i succeed in deleting the user from the database but there is another issue now.现在我成功地从数据库中删除了用户,但现在还有另一个问题。

the issue is.问题是。 i register.我注册。 and then i delete my account.然后我删除我的帐户。 but when i try to log in. it says the account exists.但是当我尝试登录时,它说该帐户存在。 but the account no longer exists in the database.但该帐户不再存在于数据库中。 but when i tried logging in, it just logged me in and created a new id and username for the user in the database.但是当我尝试登录时,它只是让我登录并为数据库中的用户创建了一个新的 ID 和用户名。 i tested this on multiple devices, pages, and even after days of deleting the account and they are all like that.我在多个设备、页面上测试过这个,甚至在删除帐户几天后,它们都是这样。 (it worked perfectly fine before adding the user delete option) (在添加用户删除选项之前它工作得很好)

i thought it might be cookies but even when deleting the cookies it didn't work.我认为它可能是 cookies 但即使删除 cookies 它也不起作用。 someone suggested that there might be a secondary database but i didn't find anything.有人建议可能有一个辅助数据库,但我没有找到任何东西。 (note: i noticed the update user info section and the url is using firebase-auth. i tried searching how to do that but i didn't find anything so i don't know how to delete user using auth in laravel using ajax) (注意:我注意到更新用户信息部分和 url 正在使用 firebase-auth。我尝试搜索如何做到这一点,但我没有找到任何东西,所以我不知道如何使用 ajax 在 laravel 中使用 auth 删除用户)

for testing, i removed the section of the code where it logs you out after deleting the account and in the console, i still get the user id which is weird.为了进行测试,我删除了删除帐户后将您注销的代码部分,在控制台中,我仍然得到奇怪的用户 ID。 i have also updated the js code too.我也更新了 js 代码。 in the console i get the: the user's id "response it worked"在控制台中,我得到:用户的 ID“响应有效”

and in the.network, I'm not getting anything worth mentioning.在.network 中,我没有得到任何值得一提的东西。

I'm also not getting anything in the console from logging in我也没有通过登录在控制台中获得任何信息

this is what the codes look like这就是代码的样子

public\assets\js\common.js公共\资产\js\common.js

//this is the log in and register code
const firebaseConfig = {
    apiKey: fireapiKey,
    authDomain: authDomain,
    projectId: projectId,
    storageBucket: storageBucket,
    messagingSenderId: messagingSenderId,
    appId: appId,
    measurementId: measurementId
};

firebase.initializeApp(firebaseConfig);
firebase.analytics();

$("#loginWithEmail").on("submit", function (event) {
    event.preventDefault();
    var loginbtn45 = document.getElementById("loginWithEmailBtn");
    loginbtn45.textContent = `${localLOGGINGINPLEASEWAIT}`


    firebase.auth().signInWithEmailAndPassword($('#inputEmail').val(), $('#inputPassword').val()).then(function (response) {
            console.log(response);
            $(".loader").show();
            loginbtn45.textContent = `${localLOGIN}`
            if (response.user.emailVerified == true) {
                $.ajax({
                    type: "POST",
                    dataType: 'json',
                    data: {
                        "_token": "{{ csrf_token() }}",
                        'identity': $('#inputEmail').val(),
                        'firstname': "D",
                        'email': $('#inputEmail').val(),
                        'lastname': null,
                        'login_type': "fireBaseLogin",
                        'device_type': 3,
                        'device_token': "nothing"
                    },
                    headers: {
                        'apikey': 123
                    },
                    url: `${baseUrl}register`,
                    success: function (data) {
                        $('.signOutModal').modal('hide');
                        iziToast.success({
                            timeout: 2000,
                            title: `${locallOGINsuccessfully}`,
                            position: "topRight"
                        })

                        localStorage.setItem('userObject', JSON.stringify(data.data));
                        $(".loader").hide();
                    },
                    error: function (data) {
                        $(".loader").hide();
                        console.log(data);
                    }
                });
            } else {
                $(".loader").hide();
                iziToast.error({
                    icon: 'fas fa-times-circle',
                    message: `${localVerifyYourEmail}`,
                    position: "topRight"
                });
            }
        })

        .catch(function (error) {
            console.log(error);
            $(".loader").hide();
            loginbtn45.textContent = `${localLOGIN}`
            iziToast.error({
                icon: 'fas fa-times-circle',
                message: error.code,
                position: "topRight"
            });
        })
});
//------------
$("#registerForm").on("submit", function (event) {
    event.preventDefault();


    var fullname = $("#fullname").val();
    var email = $("#email").val();
    var password = $("#password").val();
    var confirmPassword = $("#confirmPassword").val();

    if (password.length < 6) {

        iziToast.error({
            icon: 'fas fa-times-circle',
            message: `${localPasswordLength}`,
            position: "topRight"
        });

    } else {

        if (password != confirmPassword) {

            iziToast.error({
                icon: 'fas fa-times-circle',
                message: `${localPasswordNotMatch}`,
                position: "topRight"
            });


        } else {
            $(".loader").show();
            $('#registerUser').text(`${localREGISTERINGPLEASEWAIT}`)

            firebase.auth().createUserWithEmailAndPassword(email, password).then(function (response) {


                    sendingVerifyEmail();
                    console.log(response);
                    $('#registerUser').text(`${localREGISTERED}`)



                    $.ajax({
                        type: "POST",
                        dataType: 'json',
                        data: {
                            'identity': email,
                            'firstname': fullname,
                            'email': email,
                            'lastname': null,
                            'login_type': "firebasLogin",
                            'device_type': 3,
                            'device_token': "nothing"

                        },
                        headers: {
                            'apikey': 123
                        },
                        url: `${baseUrl}firebaseRegister`,
                        success: function (data) {
                            console.log(data);
                            $(".loader").hide();
                            iziToast.success({
                                timeout: 2000,
                                title: `${localRegistersuccessfully}`,
                                position: "topRight"
                            })
                            $('#registerForm')[0].reset();


                        },
                        error: function (data) {
                            $(".loader").hide();
                            console.log(data);

                        }
                    });


                })
                .catch(function (error) {
                    $(".loader").hide();
                    console.log(error);
                    $('#registerUser').text(`${localREGISTERED}`)

                    if (error.code == "auth/email-already-in-use") {

                        iziToast.error({
                            icon: 'fas fa-times-circle',
                            message: `${localUserAllreadyExist}`,
                            position: "topRight"
                        });

                    } else {
                        iziToast.error({
                            icon: 'fas fa-times-circle',
                            message: `${localEnterValideEmail}`,
                            position: "topRight"
                        });
                    }
                })

            function sendingVerifyEmail() {
                firebase.auth().currentUser.sendEmailVerification().then(function (response) {
                        $('#registerUser').text(`${localREGISTERED}`)
                        console.log(response);
                    })
                    .catch(function (error) {
                        console.log(error);
                        $('#registerUser').text(`${localREGISTERED}`)
                    })
            }

        }
    }
});


$(".delteuserbtn").on('click', function (e1){
  e1.preventDefault();
    $('.deltetemodal').modal('show');
});

$(".delteuserbtnyes").on('click', function (e2){
    e2.preventDefault();
    var theuser = localStorage.getItem('userObject');
    if(theuser != null){
      theuser = JSON.parse(theuser);
      userid=theuser.id;
    }
    console.log(userid);

    $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });

    $.ajax({
      type: "DELETE",
      url: "deleteAccount/"+userid,
      dataType: "JSON",
      success: function (response){
        console.log('response it worked');
      
        $(".deltetemodal").modal('hide');
        $(".updateProfileModal").modal('hide');
        localStorage.removeItem("userObject");

        $('.userName').text("")
        $('.useremail').text("")
        $(".usersideImage").attr('src',"")
        $('.signInRemove').removeClass('d-none');
        $(".userLoginDiv").css("display", "none");

        iziToast.success({timeout: 2000, title: localAccDeletedSuccess,position: "topRight"})
        window.location.href = `${appUrl}`;
      },
      error: function(xhr) {
        console.log(xhr.responseText); // this line will save you tons of hours while debugging
      }
    });
});


$(".delteuserno").on('click', function (e){
    $(".deltetemodal").modal('hide');
    $(".updateProfileModal").modal('hide');
});




$("#updateform").on('submit',function(event) {
event.preventDefault();
        $(".loader").show();
        var updatedata = localStorage.getItem('userObject');
        updatedata = JSON.parse(updatedata);

        var formdata = new FormData($("#updateform")[0]);
    
        formdata.append('id',updatedata.id);
        console.log(formdata);
        $.ajax({
            url: `${baseUrl}updateProfile`,
            type: 'POST',
            beforeSend: function(xhr) {
                xhr.setRequestHeader('apikey',apikey);
                xhr.setRequestHeader('userId',updatedata.id);
            },
            data: formdata,
            dataType: "json",
            contentType: false,
            cache: false,
            processData: false,
            success: function(response) {
                console.log(response)
                $(".loader").hide();
                iziToast.success({timeout: 2000, title: `${localUpdateSuccessfull}`,position: "topRight"})

                localStorage.setItem('userObject',JSON.stringify(response.data));
                $image = `${fimageUrl}${response.data.image}` ;
                $(".usersideImage").attr('src',$image)
                    
                $('.userName').text(response.data.firstname)
                $('.useremail').text(response.data.email)
                $('.updateProfileModal').modal('hide');
            },
            error: function(err) {   console.log(JSON.stringify(err));    }

        });
});

routes/web.php路线/web.php

<?php

use App\Http\Controllers\ViewController;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;

Route::delete('deleteAccount/{id}' , [App\Http\Controllers\UserController::class, 'destroy']) ->name('destroy');

http/Controller/userController.php http/控制器/userController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class UserController extends Controller {
    public function destroy($id){
/*              $user = users::find($id);
                $user ->delete(); */
            DB::table('users')->where('id', $id)->delete();
            return response()->json([
                'status' =>200,
                'message'=>'Account Deleted Successfully',
            ]);

/*             $directory=storage_path('framework/views');  
            $files=\File::allFiles($directory);
            \File::delete($files); */
        } 
    }

As far as I can see, you do two things when you create a user:据我所知,创建用户时会做两件事:

  1. You create the user in Firebase Authentication, by calling createUserWithEmailAndPassword .您通过调用createUserWithEmailAndPassword在 Firebase 身份验证中创建用户。
  2. You create a record for the user in your database.您在数据库中为用户创建一条记录。

But when you delete the user, you only delete them from the database.但是当你删除用户时,你只是从数据库中删除他们。 So that means the user can indeed still log in with Firebase Authentication, since you didn't delete it from there所以这意味着用户确实仍然可以使用 Firebase 身份验证登录,因为你没有从那里删除它

To also delete the user from Firebase Authentication, call delete on their profile as shown in the documentation on deleting the current user .要同时从 Firebase 身份验证中删除用户,请在其个人资料上调用delete ,如有关删除当前用户的文档中所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM