🎉 سال نو، مهارت نو، مشاوره رایگان نقشه راه برنامه نویسی (آفر ویژه ثبت نام قبل از افزایش قیمت 🔥)
۰ ثانیه
۰ دقیقه
۰ ساعت
۳ Ali Yz
logout کردن ادمین
جامعه لاراول ایجاد شده در ۰۹ آبان ۱۴۰۱

سلام من متد destroy رو از registercontroller آوردم و گارد admin هم دادم برای لاگ اوت اما لاگوت ادمین انجام نمیشه لطفا راهنمایی کنید و میدلویر auth برای ادمین کاربرد داره یا نه.

سلام علی عزیز، وقت به خیر

میدلور auth برای همه‌ی کاربران لاگین شده قابل استفاده هست و مختص admin و ... نیست و میتونید استفاده کنید.


برای حل مشکلتون هم لطفا پروژتون در گیت هاب قرار بدید و لینک قرار بدید در پاسخ همین تیکت تا بتونیم راهنماییتون کنیم.


موفق باشید ??

محمد حسن عاطفت ۰۹ آبان ۱۴۰۱، ۱۷:۳۲

پروژه شرکت رو نمیتونم پابلیک کنم. سوالم اینه من لاگین میکنم ولی لاگ اوت نمیتونم انجام بدم

این AdminController


namespace App\\Http\\Controllers;
use App\\Models\\Admin;
use App\\Providers\\RouteServiceProvider;
use Illuminate\\Foundation\\Auth\\AuthenticatesUsers;
use Illuminate\\Http\\Request;
use Illuminate\\Support\\Facades\\Auth;
use Illuminate\\Support\\Facades\\Hash;
use Illuminate\\Support\\Facades\\Validator;
class AdminController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = RouteServiceProvider::ADMIN_HOME;
    public function __construct()
    {
        $this->middleware('guest:web');
        $this->middleware('guest:admin');
    }
    public function index(){
        dd('index');
    }
    public function showLoginForm()
    {
        return view('admin.auth.login');
    }
    public function showRegisterForm()
    {
        return view('admin.auth.register');
    }
    public function register(Request $request){
        $this->validateRegister($request);
        $admin=$this->createAdmin($request->all());
        $this->guard()->login($admin);
        return redirect($this->redirectTo);
    }
    private function validateRegister($request){
        return $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }
    private function createAdmin(array $data){
        return Admin::create([
            'name'=>$data['name'],
            'phone'=>$data['phone'],
            'email'=>$data['email'],
            'address'=>$data['address'],
            'bio'=>$data['bio'],
            'department'=>$data['department'],
            'password'=>Hash::make($data['password']),
        ]);
    }
    private function guard(){
        return Auth::guard('admin');
    }
    public function logout(Request $request)
    {
        $this->guard()->logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        return redirect('/admin/login');
    }
}

اینم مدل Admin


namespace App\\Models;
use Illuminate\\Database\\Eloquent\\Factories\\HasFactory;
use Illuminate\\Foundation\\Auth\\User as Authenticatable;
use Illuminate\\Notifications\\Notifiable;
use Laravel\\Sanctum\\HasApiTokens;
class Admin extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;
    protected $guard = 'admin';
    protected $fillable = [
        'name',
        'phone',
        'email',
        'address',
        'bio',
        'department',
        'password',
    ];
}

اینم auth.php

 [
        'guard' => 'web',
        'passwords' => 'users',
    ],
    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session"
    |
    */
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
    ],
    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\\Models\\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\\Models\\Admin::class,
        ],
    ],
    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that each reset token will be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
        'admins' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],
    /*
    |--------------------------------------------------------------------------
    | Password Confirmation Timeout
    |--------------------------------------------------------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    */
    'password_timeout' => 10800,
];
Ali Yz ۱۰ آبان ۱۴۰۱، ۱۱:۳۹

علی عزیز، در فایل کنترلرتون و در بخش constructor این تغییر اعمال کنید:

public function __construct()
{  
$this>middleware('guest:web')->except('logout');
   $this>middleware('guest:admin')>except('logout');
}

امیدوارم این تغییر مشکلتون حل کنه.

موفق باشید ??

محمد حسن عاطفت ۱۱ آبان ۱۴۰۱، ۱۷:۴۹