1

所以我是 Laravel 的新手。只是通过一个测试项目来学习。

我正在使用 Vue 制作带有 Laravel 8 和 Sanctum 后端的 SPA。

我成功创建了一个注册系统,但无法登录。

下面是我的代码。

注册功能

    {
        // Validate the user input
        $request->validate([
            "company" => ["required"],
            "firstName" => ["required"],
            "lastName" => ["required"],
            "phone" => ["required"],
            "email" => ["required", "email", "unique:users"],
            "password" => ["required", "min:8", "confirmed"],
        ]);
        // Unique Company Identifier
        $uniqueCode = unique_company_code();

        // Create A DB Name
        $dbName = "tn_";
        $dbName .= substr(str_replace(".", "", str_replace(" ", "", $request->company)), 0, 4);
        $dbName .= substr($uniqueCode, 0, 6);
        try
        {
            DB::beginTransaction();
            $company = Company::create([
                "Name" => $request->company,
                "Code" => $uniqueCode,
                "DatabaseName" => $dbName,
                "Status" => "Waiting Activation",
                "AllowedUsers" => 3,
                "RegisteredUsers" => 1
            ]);
            User::create([
                "CompanyID" => $company->id,
                "FirstName" => $request->firstName,
                "LastName" => $request->lastName,
                "Phone" => $request->phone,
                "Email" => $request->email,
                "Password" => Hash::make($request->input("password")),
                "AccountType" => "Admin"
            ]);
            // Create New DB
            $query = "CREATE DATABASE IF NOT EXISTS $dbName CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';";
            DB::statement($query);
            DB::commit();

            return response()->json(["action" => "Success", "msg" => "<p>Congratulations, Your Account has been created!!<br><b>You will get a confirmation email once your Account has been Approved and Activated</b></p>"], 200);
        }
        catch (\Exception $e)
        {
            DB::rollback();
            return response()->json(array("Error" => "<p>Oop...Seems like there was an error at our end.<br /> Please contact us at <b>support@simpleflow.in</b> for assistance</p>"), 500);
            logger($e->getMessage());
        }
    }

登录功能

    {
        $request->validate([
            "email" => "required",
            "password" => "required"
        ]);

        $user = User::where("Email", $request->email)->first();
        $check = Hash::check($request->password, $user->Password);
        //return $check;

        if (Auth::attempt(["Email" => $request->email, "password" => $request->password]))
        {
            return response()->json(Auth::user(), 200);
        }

        throw ValidationException::withMessages([
            "Login" => "The Provided Credentials are Incorrect.",
            "emailId" => $request->email,
            "password" => $request->password
        ]);
    }

注意:我在 Auth::attempt() 函数中尝试了“密码”和“密码”,它不起作用

注 2:注释 'return $check' 在浏览器控制台中返回 '1'。这意味着散列密码(在用户模型中加载)与请求中的密码匹配......对吗?

最后,我的用户表 数据库

自昨晚以来,我一直在尝试解决此问题,并通过多次谷歌搜索并尝试了一些方法,但没有任何效果。

我也找不到直接调试 Auth::attempt 方法的方法,就像我不知道发生了什么故障一样。

请帮忙……因为这个,我一直在拔头发。

更新:所以我只是在搞砸......我将表中的列名从(电子邮件,密码)更改为(电子邮件,密码)......并且在我的代码中,恢复为 $request->only('email', 'password')

出于某种原因......它有效......我仍然不知道为什么我会遇到这个错误以及如果我的表列名被大写,我怎么可能让它工作(我喜欢这样......:/ )

因此,尽管我的问题已解决 ATM ...对此的任何解释将不胜感激...

4

0 回答 0