Multi domain auth problem!

Posted 11 months ago by Stank0V01

So hello guys.. I have problem with auth.. So lets explain more details!

I have main page mydomain.com and it use default laravel auth.. I have shop page {slug_of_shop}.mydomain.com and auth is only with username.. i have another table for user that not have password column and other things.

So i want to login user on subdomain with only username.. How it will be done that?

My Routes:

<?php

$mainRouter = function () {
    Route::get('/', '[email protected]')->name('home');
    Route::post('/gdpr', '[email protected]');
    Auth::routes();

// Main site cart
    Route::group(['prefix' => 'cart'], function () {
        Route::post('/checkout', '[email protected]')->name('checkout');
        Route::get('/success', '[email protected]')->name('purchaseSuccess');
        Route::get('/failed', '[email protected]')->name('purchaseFailed');
        Route::put('/proccess', '[email protected]')->name('purchase');
        Route::get('/check', '[email protected]')->name('check');
    });

};
Route::group(['domain' => env('PLAIN_URL')], $mainRouter);
Route::group(['domain' => 'www.'.env('PLAIN_URL')], $mainRouter);

// Shop things content
Route::group(['domain' => '{slug}.' . env('PLAIN_URL'), 'middleware' => 'ShopExist'], function () {
    Route::get('/', 'Shop\[email protected]');
    Route::get('/language/{lang}', 'Shop\[email protected]');
    Route::get('/currency/{currency}', 'Shop\[email protected]');
    Route::get('/category/{category}', 'Shop\[email protected]');

    Route::group(['prefix' => 'auth'], function () {
        Route::get('/login','Shop\[email protected]')->middleware('guest');
        Route::get('/logout','Shop\[email protected]')->middleware('auth');
        Route::post('/login','Shop\[email protected]')->middleware('guest');
    });

});

My Shop\AuthController.php

<?php

namespace App\Http\Controllers\Shop;

use App\Http\Controllers\Controller;
use App\Http\Requests\UserCreate;
use App\MCUser;
use App\Setting;
use App\Site;
use Auth;
use Illuminate\Http\Request;

class AuthController extends Controller
{

    public $theme;
    public $site_id;

    /**
     * CategoryController constructor.
     */
    public function __construct(Request $request)
    {
        $site_id = Site::where('slug', $request->route()->parameter('slug'))->first()->id;

        $theme = Setting::where('key', 'theme')->where('site_id', $site_id)->first()->value;

        $this->theme = $theme;
        $this->site_id = $site_id;
    }

    /**
     * @param $user
     */
    private function login($user)
    {
        Auth::login($user);
    }

    /**
     * @param $slug
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function page($slug = null)
    {
        return view('shop.themes.' . $this->theme . '.pages.login');
    }

    /**
     * @param UserCreate $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function create($slug = null, UserCreate $request)
    {

        $createUser = MCUser::firstOrCreate(
            ['username' => $request->input('username'), 'site_id' => $this->site_id]
        );

        $this->login($createUser);

        return redirect()->to('/');

    }

    /**
     * @return \Illuminate\Http\RedirectResponse
     */
    public function logout()
    {
        Auth::logout();
        return redirect('/');
    }
}

Please sign in or create an account to participate in this conversation.