I'm new to Laravel, and I'm trying to create a Login and Registration page using this reference Link
I have done the same code, but after login, it does not jump to the Dashboard page, even after Registration also pages do not jump to the Dashboard, now I'm too confused why it's not working, what I have to do the wrong thing here.
please help me with this.
My code
Web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CustomAuthController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
/*Route::get('', function () {
return view('auth/login');
});*/
Route::get('dashboard', [CustomAuthController::class, 'dashboard']);
Route::get('login', [CustomAuthController::class, 'index'])->name('login');
Route::post('custom-login', [CustomAuthController::class, 'customLogin'])->name('login.custom');
Route::get('registration', [CustomAuthController::class, 'registration'])->name('register-user');
Route::post('custom-registration', [CustomAuthController::class, 'customRegistration'])->name('register.custom');
Route::get('signout', [CustomAuthController::class, 'signOut'])->name('signout');
Path : E:\xampp\htdocs\xyz\app\Http\Controllers CustomAuthController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Hash;
use Session;
use Redirect;
use App\Models\User;
use App\Models\Admin;
class CustomAuthController extends Controller
{
public function index()
{
return view('auth.login');
}
public function registration()
{
return view('auth.registration');
}
public function customLogin(Request $request)
{
//dd('login');
$request->validate([
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
//return redirect()->intended('dashboard')->withSuccess('Signed in');
return redirect("dashboard")->withSuccess('Signed in');
}
return redirect("login")->withSuccess('Login details are not valid');
}
public function customRegistration(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
$data = $request->all();
$check = $this->create($data);
Auth::login($user);
return redirect("dashboard")->withSuccess('You have signed-in');
}
public function dashboard()
{
if(Auth::check()){
return view('dashboard');
}
return redirect("login")->withSuccess('You are not allowed to access');
}
public function create(array $data)
{
return Admin::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
}
public function signOut() {
Session::flush();
Auth::logout();
return Redirect('login');
}
}
in this CustomAuthController there is a function customLogin and customRegistration if condition is not work I think so.
Path : E:\xampp\htdocs\xyz\resources\views\auth registration.blade.php
**
@include('header')
<div >
<div >
<div >
<div >
<div >
<div >
<div >
<!-- <img src="../../images/logo.svg" alt="logo"> -->
<img src = "{{ asset('/images/logo2.png') }}" alt="logo"/>
</div>
<h4>New here?</h4>
<h6 >Signing up is easy. It only takes a few steps</h6>
<form action="{{ route('register.custom') }}" method="POST">
@csrf
<div >
<input type="text" id="name" name="name" placeholder="Username">
@if ($errors->has('name'))
<span >{{ $errors->first('name') }}</span>
@endif
</div>
<div >
<input type="email" id="email" name="email" placeholder="Email">
@if ($errors->has('email'))
<span >{{ $errors->first('email') }}</span>
@endif
</div>
<div >
<input type="password" id="password" name="password" placeholder="Password">
@if ($errors->has('password'))
<span >{{ $errors->first('password') }}</span>
@endif
</div>
<div >
<div >
<label >
<input type="checkbox" >
I agree to all Terms & Conditions
</label>
</div>
</div>
<div >
<!-- <a >SIGN UP</a> -->
<button type="submit" >SIGN UP</button>
</div>
<div >
Already have an account? <a href="{{ url('login') }}" >Login</a>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- content-wrapper ends -->
</div>
<!-- page-body-wrapper ends -->
</div>
@include('footer')**
Path : E:\xampp\htdocs\xyz\resources\views\auth login.blade.php
@include('header')
@extends('layouts.layout')
@section('content')
<div >
<div >
<div >
<div >
<div >
<div >
<div >
<!-- <img src="../../images/logo.svg" alt="logo"> -->
<img src = "{{ asset('/images/logo2.png') }}" />
</div>
<h4>Hello! let's get started</h4>
<h6 >Sign in to continue.</h6>
@if (Session::get('success'))
<div role="alert">
{{ Session::get('success') }}
</div>
@endif
<form action="{{ route('login.custom') }}" method="POST">
@csrf
<div >
<input type="email" name="email" id="email" placeholder="Username">
@if ($errors->has('email'))
<span >{{ $errors->first('email') }}</span>
@endif
</div>
<div >
<input type="password" name="password" id="password" placeholder="Password">
@if ($errors->has('password'))
<span >{{ $errors->first('password') }}</span>
@endif
</div>
<div >
<!-- <a
href="{{ url('login.custom') }}">SIGN IN</a> -->
<button type="submit" >SIGN IN</button>
</div>
<div >
<!-- <div >
<label >
<input type="checkbox" >
Keep me signed in
</label>
</div> -->
<a href="#" >Forgot password?</a>
</div>
<!-- <div >
<button type="button" >
<i ></i>Connect using facebook
</button>
</div> -->
<div >
Don't have an account? <a href="{{ url('registration') }}" >Create</a>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- content-wrapper ends -->
</div>
<!-- page-body-wrapper ends -->
</div>
@include('footer')
@endsection
Path : E:\xampp\htdocs\xyz\resources\views\dashboard.php dashboard.php
CodePudding user response:
In the registration, you're just creating the user, you're not logging the user in. Use Auth::login($user);
to login the user.
In the login, you're using redirect()->intended
, i think its intending to go to the login page instead of dashboard read THIS or THIS to read more about redirect()->intended
.
public function customRegistration(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
$data = $request->all();
$user = $this->create($data);
Auth::login($user);
return redirect("dashboard")->withSuccess('You have signed-in');
}
public function customLogin(Request $request)
{
//dd('login');
$request->validate([
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return redirect("dashboard")->withSuccess('Signed in');
}
return redirect("login")->withSuccess('Login details are not valid');
}
CodePudding user response:
i think its just a syntax error. you have to implement a middle-ware for protecting the routes from users to access without login
your custom login
method should be like this
public function authenticate(LoginRequest $request)
{
if(Auth::attempt([
'phonenumber'=>$request->phonenumber,
'password'=>$request->password],
$remember=$request->has('remember')?true:false))
{
return redirect()->route('dashboard');
}
}
i hope it helps you if you having a doubt.. Look after this github open repo