skip to Main Content

I have an issue with the login, I’m pretty sure to enter the good login information but I have the error message 401 i DONT understand why please someone help me
enter image description here
I expect to understand why i have the bad answers even I enter the good login information.

2

Answers


  1. Chosen as BEST ANSWER

    https://stackoverflow.com/users/12750612/hichemtech for the register this is what i did public function register(Request $request) { $validatedData = $request->validate([ 'lastname' => 'required|string|max:255', 'firstname' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:8', 'adress' => 'required', 'postcode' => 'required', 'city' => 'required', 'roles_id' => 'required', 'birthday' => 'required', ]);

        $user = User::create([
            'lastname' => $validatedData['lastname'],
            'firstname' => $validatedData['firstname'],
            'email' => $validatedData['email'],
            'adress' => $validatedData['adress'],
            'postcode' => $validatedData['postcode'],
            'city' => $validatedData['city'],
            'roles_id' => $validatedData['roles_id'],
            'birthday' => $validatedData['birthday'],
            'password' => Hash::make($validatedData['password']),
        ]);
    
        $token = $user->createToken('auth_token')->plainTextToken;
    
        return response()->json([
            'access_token' => $token,
            'token_type' => 'Bearer',
        ]);
    }
    

  2. Can you try this code, I wouldn’t check !Auth and it could be your front end passing the data incorrectly.

    public function login(Request $request) {
        $fields = $request->validate([
            'email' => 'required|string',
            'password' => 'required|string'
        ]);
    
        // Check email
        $user = User::where('email', $fields['email'])->first();
    
        // Check password
        if(!$user || !Hash::check($fields['password'], $user->password)) {
            return response([
                'message' => 'Incorrect Credentials'
            ], 401);
        }
    
        $token = $user->createToken('mytokenstring')->plainTextToken;
    
        $response = [
            'user' => $user,
            'token' => $token
        ];
    
        return response($response, 201);
    }
    

    If you want to check data coming from front end you can put the below code in the controller and it will appear in storage-laravel log:

    info($request->all());
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search