Search code examples
laravel-passportlaravel-9ionic5

Header failure in Ionic when attempting to pull user infor from laravel


I set up passport authentication api between an ionic 5 application and laravel 9. After login, the access token is sent to the ionic app and stored. When i try to do pull user info, there seems to be a problem with how I structured the header as no response or error is received.

laravel

            public function login(Request $request)
            {
                $validator = Validator::make($request->all(), [
                    'email' => 'required|string|email|max:255',
                    'password' => 'required|string|min:6',
                ]);
                if ($validator->fails()) {
                    return response(['errors' => $validator->errors()->all()], 422);
                }
                $user = User::where('email', $request->email)->first();
                if ($user) {
                    if (Hash::check($request->password, $user->password)) {
                        $token = $user->createToken('Pesacount-User-Access-Token')->accessToken;
                        $response = ['token' => $token];
                        return response($response, 200);
                    } else {
                        $response = ["message" => "Password mismatch"];
                        return response($response, 422);
                    }
                } else {
                    $response = ["message" => 'User does not exist'];
                    return response($response, 422);
                }


             public function userInfo() 
            {
         
                $user = auth()->user();
              
                return response()->json(['user' => $user], 200);
         
            }

Ionic

              user() {
                const headers = new HttpHeaders({
                  //'Authorization': this.global.token["token_type"] + " " + this.global.token["access_token"]
                  'Authorization': 'Bearer ' + this.global.token
                });
                alert('getting user');
                return this.http.get(this.env.API_URL + 'auth/get-user', { headers: headers })
                  .pipe(
                    tap(user => {
                    alert('got uuser');
                      return user;
                    }),
                  )
              }

What could I be doing wrong?

looking at the laravel logs, the error returned is

[2022-10-15 08:51:52] local.ERROR: The resource owner or authorization server denied the request. {"exception":"[object] (League\\OAuth2\\Server\\Exception\\OAuthServerException(code: 9): The resource owner or authorization server denied the request. at /var/www/html/Pesacount/vendor/league/oauth2-server/src/Exception/OAuthServerException.php:243)

Solution

  • There was a conflict with different authentication modules in Laravel thus a different token was generated and sent to ionic app from what was expected by laravel when during POST / GET requests