skip to Main Content

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', [AppHttpControllersApiAuthController::class, 'login']);

2

Answers


  1. 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.

    Login or Signup to reply.
  2. The plainTextToken you’re returning is a string, you should return a JSON response instead like this:

    return response()->json([
        'success' => true,
        'data' => [
            'token' => $user->createToken($request->device_name)->plainTextToken
        ],
        'message' => ...,
    ], 200);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search