lalarukh's avatar

Can't auth my customer login

Hi, I've 2 dashboards, 1 is customer and the other is admin. My admin can easily login as its working as 'users' but my customer can't login, however I have putted my best in code but can't get rid of login bugs. Here's my login controller:

` public function postLogin(CustomerLoginRequest $request){

    $request->authenticate();

    $request->session()->regenerate();

    return redirect()->intended('')->withSuccess('You have Successfully logged in');
}`

This is my CustomerLoginRequest.php file authenticate function: `

public function authenticate() { $this->ensureIsNotRateLimited();

    if (! Auth::guard('shopify_customers')->attempt($this->only('email', 'password'), $this->boolean('remember'))) {
        RateLimiter::hit($this->throttleKey());

        throw ValidationException::withMessages([
            'email' => __('auth.failed'),
        ]);
    }

    RateLimiter::clear($this->throttleKey());
}

`

Here's my modal: ` protected $table = 'shopify_customers';

use HasApiTokens, HasFactory, Notifiable;
protected $guard = 'shopify_customers';

protected $fillable = [
    'email',
    'password',
];

protected $hidden = [
    'password',
    'remember_token',
];

I have called my table modal in auth.php also. But I don't know may be in the modal something is not connecting my code with the table authentication. Seeking guidance.

0 likes
5 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Based on the provided information, it seems like the issue might be related to the authentication guard being used. The admin login is working because it is using the default guard ('web'), but the customer login is using a custom guard ('shopify_customers').

To ensure that the custom guard is being used for customer authentication, make sure to specify the guard in the login form and in the authentication middleware.

In the login form, add a hidden input field with the name 'guard' and value 'shopify_customers':

<form method="POST" action="{{ route('customer.login') }}">
    @csrf
    <input type="hidden" name="guard" value="shopify_customers">
    <!-- rest of the form fields -->
</form>

In the authentication middleware, specify the guard as 'shopify_customers':

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/home');
    }

    if ($guard == 'shopify_customers') {
        $this->redirectTo = '/customer/dashboard';
    }

    return $next($request);
}

Also, make sure that the 'shopify_customers' guard is defined in the auth.php config file:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],

    'shopify_customers' => [
        'driver' => 'session',
        'provider' => 'shopify_customers',
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],

    'shopify_customers' => [
        'driver' => 'eloquent',
        'model' => App\Models\ShopifyCustomer::class,
    ],
],

Make sure that the 'shopify_customers' provider is also defined in the auth.php config file.

If the issue persists, try debugging the authentication process by adding some logging statements or using the Laravel Debugbar package to inspect the authentication requests and responses.

lalarukh's avatar

@LaryAI I don't know why but still by following it up. Still all failed. Not working

lalarukh's avatar

@apex1 I'm doing wrong somewhere, I've tried thousands of ways to make another auth. But still can't !

Please or to participate in this conversation.