samedi 27 juillet 2019

Laravel login return false for some users with correct credentials

My authController used to work perfectly, but recently something changed. I'm not sure what caused this change, but for some users it's no longer possible to sign into my application.

Returning unauthorised

I haven't changed anything to my code, but I am running my database with MySql and valet, and have yesterday installed phpmyadmin

What I expect to happend is that all my users can login in with correct credentials

What is happening that some (tested with creating a new user and trying to sign in) users can't sign in (definitely all the new users)

AuthController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
 use App\User;
 use App\Role;

class AuthController extends Controller
{
  public function register(Request $request)
    {
      $user = User::create([
        'name' => $request->name,
        'email' => $request->email,
        'adress' => 'Enter your adress',
        'sex' => $request->sex,
        'password' => bcrypt($request->password),
      ]);
      $user
      ->roles()
      ->attach(Role::where('name', 'user')->first());
      $token = auth()->login($user);
      $signedInUser = \Auth::user();
      $signedInUserStore = \Auth::user()->stores;
    return $signedInUser;
    }

    public function update(Request $request)
    {
      $id = \Auth::user()->id;
      $token = User::find($id);
      $token->name = Request::input('name');
      $token->email = Request::input('email');
      $token->email = Request::input('email');
      $token->save();

      return $this->respondWithToken($token);
    }

    public function login(Request $request)
    {
      $credentials = $request->only(['email', 'password']);
      if (!$token = auth()->attempt($credentials)) {
        return response()->json(['error' => 'Unauthorized'], 401);
      }
      $signedInUser = \Auth::user();
      $signedInUserStore = \Auth::user()->stores;
      return $this->respondWithToken($token, $signedInUser, $signedInUserStore);
    }

    protected function respondWithToken($token, $signedInUser, $signedInUserStore)
    {
      return response()->json([
        'user' => $signedInUser,
        'access_token' => $token,
        'token_type' => 'bearer',
        'expires_in' => auth()->factory()->getTTL() * 60
      ]);
    }
}




via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire