我当时为登录/注册创建了api,并为laravel应用创建了自定义身份验证,但是遇到了问题。身份验证尝试始终返回false,我已经检查了我的代码,但是感觉是正确的。
请检查我的代码:
LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\LoginValidation;
class LoginController extends Controller
{
function index() {
return view('auth.login');
}
function login(LoginValidation $data) {
dd($data->all());
if (Auth::attempt(['EMAIL' => $data->email, 'PASSWORD' => $data->password])) {
if (Auth::user()->IS_VERIFIED == 1) {
return response()->json([
'message' => 'Authentikasi berhasil'
], 200);
} else {
Auth::logout();
return response()->json([
'message' => 'Authentikasi gagal, silahkan verifikasi email terlebih dahulu'
], 401);
}
} else {
return response()->json([
'message' => 'Authentikasi gagal, email atau password salah'
], 401);
}
}
}
RegisterController.php
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
use App\User;
use App\Mail\EmailConfirmation;
use App\Http\Requests\RegisterValidation;
class RegisterController extends Controller
{
protected $token;
function index() {
return view('auth.register');
}
function register(RegisterValidation $data) {
$user = new User;
$user->NAME = $data->name;
$user->EMAIL = $data->email;
$user->PASSWORD = Hash::make($data->password);
$user->REGISTRATION_DATE = Carbon::now()->toDateString();
$user->VERIFICATION_KEY = $this->generateToken();
$user->IS_VERIFIED = 0;
$user->save();
Mail::to($data->email)->send(new EmailConfirmation($this->token));
return response()->json([
'message' => 'Pendaftaran berhasil silahkan cek email anda untuk verifikasi akun'
]);
}
function generateToken() {
$this->token = str_random(50);
return $this->token;
}
}
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'USER_TBL';
public $timestamps = false;
}
请帮忙,每一个答案将不胜感激