Search code examples
laravelauthenticationlaravel-blade

How do I route to my login when register account is successful?


**I've been trying to make a login and registration to the website we made. I'm using vs code and laravel blade template.

this is what i have for the login code:**

<?php
session_start();

$validationRules = [
    'email' => ['required', 'email'],
    'password' => ['required', 'regex:/^[A-Za-z0-9\s-_]+$/']
];

$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    foreach ($validationRules as $field => $rules) {
        foreach ($rules as $rule) {
            switch ($rule) {
                case 'required':
                    if (empty($_POST[$field])) {
                        $errors[$field] = ucfirst($field) . ' is required.';
                    }
                    break;
                case 'email':
                    if (!filter_var($_POST[$field], FILTER_VALIDATE_EMAIL)) {
                        $errors[$field] = 'Invalid email format.';
                    }
                    break;
                case 'regex':
                    if (!preg_match('/^[A-Za-z0-9\s-_]+$/', $_POST[$field])) {
                        $errors[$field] = ucfirst($field) . ' contains invalid characters.';
                    }
                    break;
            }
        }
    }
}    


?>

<!DOCTYPE html>
<html lang="en">
<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">
    <link rel="stylesheet" href="StyleLogin.css">
    
    <title>Style Sync Login Page</title>
</head>
<body>
    
    <div class="card">
        <div class="card-header">
            <img src="{{ asset('logo.png') }}" alt="Style Sync Logo">
            <div class="title-container">
            <h1>Account Login</h1>
        </div>
        <div class="card-body">
            <?php if (!empty($errors)): ?>
                <div class="alert alert-danger">
                    <ul>
                        <?php foreach ($errors as $error): ?>
                            <li><?= $error ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            <?php endif; ?>
            <form action="login" method="POST">
                @csrf
                <div class="form-group">
                    <label for="email">Email</label>
                    <input type="email" id="email" name="email" required>
                </div>
                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <button type="submit">Login</button>
            </form>
            <p>Don't have an account?<a href="{{ route('registration') }}">Register an Account</a></p>
        </div>
    </div>
</body>
</html>

Then this is what i have on the login controller:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    public function showLoginForm()
    {
        return view('auth.login');
    }

    public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required',
        ]);

        if ($validator->fails()) {
            return redirect()->route('login')->withErrors($validator)->withInput();
        }

        if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
            return redirect()->route('dashboard');
            
        } else {
            return redirect()->route('login')->with('error', 'Invalid email or password');
        }
    }
}

this is the registration code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Style Sync Registration Page</title>
    <link rel="stylesheet" href="StyleRegistration.css">
</head>
<body>
    <div class="card">
        <div class="card-header">
            <img src="{{ asset('logo.png') }}" alt="Style Sync Logo">
            <h1>Account Registration</h1>
        </div>
        <div class="card-body">
            <form method="POST" action="{{ route('registration') }}">
                @csrf
                <div>
                    <label for="name">Name</label>
                    <input id="name" type="text" name="name" value="{{ old('name') }}" required>
                    @error('name')
                        <div class="error">{{ $message }}</div>
                    @enderror
                </div>
                <div>
                    <label for="email">Email</label>
                    <input id="email" type="email" name="email" value="{{ old('email') }}" required>
                    @error('email')
                        <div class="error">{{ $message }}</div>
                    @enderror
                </div>
                <div>
                    <label for="password">Password</label>
                    <input id="password" type="password" name="password" required>
                    @error('password')
                        <div class="error">{{ $message }}</div>
                    @enderror
                </div>
                <div>
                    <label for="password_confirmation">Confirm Password</label>
                    <input id="password_confirmation" type="password" name="password_confirmation" required>
                </div>
                <button type="submit">Register</button>
            </form>
            <p>Already have an account? <a href="{{ route('login') }}">Login here!</a></p>
        </div>
    </div>
</body>
</html>

Then this is the registration controller

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
class RegistrationController extends Controller
{
    public function showRegistrationForm()
    {
        return view('auth.registration');
    }

    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:8|confirmed',
        ]);

        if ($validator->fails()) {
            return redirect()->route('registration')->withErrors($validator)->withInput();
        }
        User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password),
        ]);

        return redirect()->route('login')->with('success', 'Registration successful! Please login.');
    }
}

This is the web.php route

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegistrationController;
use App\Http\Controllers\DashboardController;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);

Route::get('/registration', [RegistrationController::class, 'showRegistrationForm'])->name('registration');
Route::post('/registration', [RegistrationController::class, 'registration']);

Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

I tried different routing but it still doesnt work this is the error that pops up: Call to undefined method App\Http\Controllers\Auth\RegistrationController::registration()


Solution

  • The error "Call to undefined method App\Http\Controllers\Auth\RegistrationController::registration()" indicates that Laravel is unable to find a method named registration() in your RegistrationController.

    Looking at your RegistrationController, the method responsible for handling the registration process is named register(), not registration(). Therefore, when you try to call registration() in your routes file, Laravel throws an error because the method does not exist.

    To fix this issue, you need to update your routes file (web.php) to use the correct method name (register()) in your registration route:

    Route::post('/registration', [RegistrationController::class, 'register']);