0

I'm trying to authenticate a Flutter app built on Flutter Flow. I've created a post request to authenticate the user from the app. I can confirm that the token gets generated whenever the user logs in through the app and stored on the personal_access_tokens table. However, when I test the api from Flutter Flow, I get null with a 200 code.

    public function login(Request $request)
    {
        try {
            $request->validate([
                'email' => 'required|email',
                'password' => 'required',
                'device_name' => 'required',
            ]);

            $user = User::where('email', $request->email)->first();

            if (! $user || ! Hash::check($request->password, $user->password)) {
                throw ValidationException::withMessages([
                    'email' => ['The provided credentials are incorrect.'],
                ]);
            }

            return $user->createToken($request->device_name)->plainTextToken;
        } catch (ValidationException $exception) {
            return response()->json([
                'success' => false,
                'message' => $exception->getMessage(),
                'errors' => $exception->errors(),
            ], 422);
        } catch (QueryException $exception) {
            return response()->json([
                'success' => false,
                'message' => $exception->getMessage(),
            ], 500);
        } catch (Exception $exception) {
            return response()->json([
                'success' => false,
                'message' => $exception->getMessage(),
            ], 500);
        }
    }

I receive the token on Postman but only get null when authenticating through the mobile app.

Route::post('/auth/login', [\App\Http\Controllers\Api\AuthController::class, 'login']);

1 Answers1

0

Solution 1: Try to change ->plainTextToken To ->accessToken

Solution 2: Check request on device_name ex. Using dd() or other

Solution 3: Try to dd() line by line your code. Is there is something missing.

Sidik
  • 128
  • 11