cari

Rumah  >  Soal Jawab  >  teks badan

Log masuk/pendaftaran tersuai Laravel 10 tidak memasuki halaman papan pemuka

<p>Saya cuba membuat log masuk/pendaftaran Laravel 10 tersuai saya sendiri kerana saya tidak mahu menggunakan pakej breez kerana saya mahu belajar cara melakukan log masuk/pendaftaran sendiri. </p> <p>Tetapi saya nampaknya tidak dapat mengesahkan ke halaman papan pemuka. </p> <p>Saya menggunakan pernyataan if <code>if(Auth::check())</code> pada fungsi papan pemuka untuk mengesahkan pengguna dalam pangkalan data. </p> <p>Tetapi ini tidak berfungsi untuk saya kerana saya terus menerima mesej ralat daripada mengubah hala semula ke halaman log masuk (<strong>Ini hanya berlaku apabila saya mendaftarkan pengguna baharu ke dalam pangkalan data</ strong>), tetapi setiap kali saya cuba log masuk saya menerima mesej kejayaan daripada fungsi log masuk (<strong>Lihat kod selanjutnya</strong>) semasa masih dalam halaman log masuk. </p> <p><strong>AuthController (papan pemuka): </strong></p> <pre class="brush:php;toolbar:false;">papan pemuka fungsi awam(): Lihat { if(Auth::check()) { return view('auth.dashboard'); } return view('auth.login')->with('error', 'Anda tidak dibenarkan mengakses'); }</pre> <p><strong>AuthController (log masuk): </strong></p> <pre class="brush:php;toolbar:false;">public function loginPost(Request $request): RedirectResponse { $request->validate([ 'email' => 'diperlukan', 'kata laluan' => 'diperlukan' ]); $credentials = $request->only('email', 'password'); if(Auth::attempt($credentials)) { $request->session()->regenerate(); return redirect()->intended(route('dashboard'))->with('berjaya', 'Anda telah berjaya log masuk'); } return redirect(route('login'))->with('error', 'Oppes! Anda telah memasukkan kelayakan tidak sah'); }</pre> <p><strong>web.php</strong></p> <pre class="brush:php;toolbar:false;">Route::get('/register', [AuthController::class, 'register'])->name('register'); Route::post('/register', [AuthController::class, 'registerPost'])->name('register.post'); Route::get('/login', [AuthController::class, 'login'])->name('login'); Route::post('/login', [AuthController::class, 'loginPost'])->name('login.post'); Route::get('/dashboard', [AuthController::class, 'dashboard'])->name('dashboard'); Laluan::post('/logout', [AuthController::class, 'logout'])->middleware('auth')->name('logout');</pre> <p>Saya belum menemui sebarang penyelesaian lagi, jadi jika sesiapa boleh membantu saya, saya akan berterima kasih. </p>
P粉162773626P粉162773626450 hari yang lalu520

membalas semua(2)saya akan balas

  • P粉006540600

    P粉0065406002023-09-06 12:41:06

    hii, fungsi log keluar anda dilindungi oleh middleware, anda juga perlu menambah middleware penghalaan papan pemuka, anda boleh mengumpulkan laluan yang memerlukan middleware pengesahan.

    Route::middleware('auth')->group(function () {
        Route::get('/dashboard', [AuthController::class, 'dashboard'])->name('dashboard');
        Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
    });

    balas
    0
  • P粉676588738

    P粉6765887382023-09-06 00:56:28

    Laluan anda

    Route::get('login', [FrontendAuthController::class, 'loginGet'])->name('login');
    Route::post('login', [FrontendAuthController::class, 'loginPost']);
    Route::post('logout', [FrontendAuthController::class, 'logout'])->name('logout');
    Route::get('register', [FrontendAuthController::class, 'registerGet'])->name('register');
    Route::post('register', [FrontendAuthController::class, 'registerPost']);

    Pengawal anda:

    <?php
    
    namespace App\Http\Controllers\Frontend;
    
    use App\Http\Controllers\Controller;
    use App\Models\User;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;
    use Illuminate\Support\Facades\Hash;
    use Illuminate\Support\Facades\Session;
    
    class FrontendAuthController extends Controller
    {
        public function loginGet(Request $request)
        {
            return view('front.auth.login');
        }
    
        public function loginPost(Request $request)
        {
            $request->validate([
                'email' => 'required|email',
                'password' => 'required',
            ], [
                'email.required' => 'The email field is required.',
                'email.email' => 'Please enter a valid email address.',
                'password.required' => 'The password field is required.',
            ]);
    
            $credentials = $request->only('email', 'password');
            if (Auth::attempt($credentials)) {
                return redirect()->intended('/');
            } else {
                return redirect()->back()->withErrors(['email' => 'These credentials do not match our records.']);
            }
        }
    
        public function logout()
        {
            Auth::logout();
            Session::flush(); // Clear all session data
            Session::regenerate(); // Regenerate the session ID
            return redirect()->route('login');
        }
    
        public function registerGet()
        {
            return view('front.auth.register');
        }
    
        public function registerPost(Request $request)
        {
            $request->validate([
                'name' => 'required|string|max:255',
                'email' => 'required|email|unique:users,email',
                'password' => 'required|min:8|confirmed',
            ], [
                'name.required' => 'The name field is required.',
                'email.required' => 'The email field is required.',
                'email.email' => 'Please enter a valid email address.',
                'email.unique' => 'This email address is already registered.',
                'password.required' => 'The password field is required.',
                'password.min' => 'The password must be at least 8 characters.',
                'password.confirmed' => 'The password confirmation does not match.',
            ]);
    
            // Create a new user record
            $user = new User();
            $user->name = $request->input('name');
            $user->email = $request->input('email');
            $user->password = Hash::make($request->input('password'));
            $user->save();
    
            // Log in the newly registered user
            Auth::login($user);
    
            // Redirect the user to the home page or any other desired page
            return redirect()->intended('/');
        }
    }

    Bilah log masuk anda

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Sign In</title>
    
        <link rel="stylesheet" href="{{ url('frontend/css/style.css') }}">
    </head>
    
    <body>
        <div class="main">
            <section class="signup">
    
                <div class="container">
                    <div class="signup-content">
    
                        <form id="signup-form" class="signup-form" method="POST" action="{{ url('login') }}">
                            @csrf
                            <!--    <img src="images/logo.svg" alt="">-->
                            <h2 class="form-title">Login Here</h2>
                            <div class="form-group">
                                <input type="email" class="form-input" value="{{ old('email') }}" name="email"
                                    id="email" placeholder="Your Email" />
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-input" name="password" id="password"
                                    placeholder="Password" />
                                <span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
                            </div>
                            @error('email')
                                <div style="color: red">{{ $message }}</div>
                            @enderror
    
                            @error('password')
                                <div style="color: red">{{ $message }}</div>
                            @enderror
                            <div class="form-group">
                                <input type="submit" name="submit" id="submit" class="form-submit" value="Sign in" />
                            </div>
                        </form>
                        <p class="loginhere">
                            New User? <a href="reg-form.html" class="loginhere-link">Register here</a>
                        </p>
                    </div>
                </div>
            </section>
        </div>
    </body>
    
    </html>

    Halaman pendaftaran anda

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Register</title>
    
        <link rel="stylesheet" href="{{ url('frontend/css/style.css') }}">
    </head>
    
    <body>
        <div class="main">
            <section class="signup">
    
                <div class="container">
                    <div class="signup-content">
    
                        <form id="signup-form" class="signup-form" method="POST" action="{{ route('register') }}">
                            @csrf
                            <h2 class="form-title">Register Here</h2>
                            <div class="form-group">
                                <input type="text" class="form-input" value="{{ old('name') }}" name="name" id="name"
                                    placeholder="Your Name" />
                            </div>
                            <div class="form-group">
                                <input type="email" class="form-input" value="{{ old('email') }}" name="email"
                                    id="email" placeholder="Your Email" />
                            </div>
                            <div class="form-group">
                                <input type="password" class="form-input" name="password" id="password"
                                    placeholder="Password" />
                                <span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
                            </div>
                            <div class="form-group">
                                <input type="password" class="form-input" name="password_confirmation"
                                    id="password_confirmation" placeholder="Confirm Password" />
                            </div>
                            @error('name')
                                <div style="color: red">{{ $message }}</div>
                            @enderror
    
                            @error('email')
                                <div style="color: red">{{ $message }}</div>
                            @enderror
    
                            @error('password')
                                <div style="color: red">{{ $message }}</div>
                            @enderror
    
                            <div class="form-group">
                                <input type="submit" name="submit" id="submit" class="form-submit" value="Register" />
                            </div>
                        </form>
                        <p class="loginhere">
                            Already have an account? <a href="{{ route('login') }}" class="loginhere-link">Login here</a>
                        </p>
                    </div>
                </div>
            </section>
        </div>
    </body>
    
    </html>

    Saya rasa ini akan menyelesaikan semua keraguan anda

    balas
    0
  • Batalbalas