Home > OS >  Authorization isn’t executed in laravel
Authorization isn’t executed in laravel

Time:06-01

This is form ‘login’, when I’m enter information in input and after press button ‘login’ I move to page with audit where 0 is authorization isn't executed and 1 is authorization is executed. I'm trying to create authorization in laravel. I did it in other project in order to understand how it works and then all was good. Now I'm trying to transfer it in my main project but when I am logging in nothing work. I don't have any mistake, authorization is simply not executed. I will grateful for any help.

Registration function

public function sub(ContactSignup $request){

    if(Auth::check()){
        return redirect(route('user.mainpage'));
    }
    
    $contact = new SignUps();
    $contact->name = $request->name;
    $contact->surname = $request->surname;
    $contact->age = $request->age;
    $contact->password = bcrypt($request->password);
    $contact->email = $request->email;

    $contact->save();

    Auth::login($contact);

    if($contact){
        Auth::login($contact);
        return redirect(route('user.sign-up'))->with('success', 'Реєстрація пройшла успішно');
    }
}

Function login

public function subin(ContactSignin $request){

    if(Auth::check()){
        return redirect()->intended(route('user.mainpage'));    
    }

    $contact = $request->only(['email', 'password']);
    
    if(Auth::attempt($contact)) {
        dd(1);    
    }
    else {
        dd(0);
    }
    return redirect()->intended(route('user.mainpage'));
}

Web.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return redirect()->route('mainpage');
});

Route::name('user.')->group(function(){
    Route::view('mainpage', 'mainpage')->middleware('auth')->name('mainpage');

Route::get('/signin', function(){
    if(Auth::check()){
        return redirect(route('user.mainpage'));
    }
    return view('signin');
})->name('sign-in');

Route::post('/signin', [\App\Http\Controllers\ContactController::class, 'subin'])->name('sign-in');

Route::get('logout', function(){
    Auth::logout();
    return redirect('/');
})->name('logout');

Route::get('/signup', function(){
    if(Auth::check()){
        return redirect(route('user.mainpage'));
    }
    return view('signup');
})->name('sign-up'); 

Route::post('/signup', [\App\Http\Controllers\ContactController::class, 'sub'])->name('sign-up');
});

Route::get('/mainpage', function () {
    return view('mainpage');
})->name('mainpage');





    

CodePudding user response:

in your methods inside your controller you have form request class called ContactSignin in this class you have code

public function authorize()
    {
        return false;
    }

make it true

The form request class is responsible of validating your request , also contains an authorize method. Within this method, you may determine if the authenticated user actually has the authority to update a given resource since you handle authorization logic for the request in the routes by provide middleware('auth') it is no need to check for use authentication in your form request class

CodePudding user response:

first you don't have route named ('user.mainpage') you have to define it in web.php second , since you named your rout in

Route::get('/mainpage', function () {
    return view('mainpage');
})->name('mainpage');

, you must redirect to the name of the route not to the path for example if your route is ('/api/main')->name('main') , you have to put the rout name in the redirect method , for example redirect(route('main'));

  • Related