"syntax error, unexpected token \"(\", expecting \"{\"" when attempting to login to laravel from ionic using passport authentication

Viewed 29

I am attempting to create a login / registration process using an ionic 5 app and laravel 9. Registration works okay but when I attempt to login, laravel returns the error:

"syntax error, unexpected token \"(\", expecting \"{\""

login.page.ts

login(email: String, password: String) {
return this.http.post(this.env.API_URL + 'auth/login',
  {email: email, password: password}
).pipe(
  tap(token => {
    this.global.storage.setItem('token', token)
    .then(
      () => {
        //alert('Token Stored');
      },
      error => alert(error)
    );
    this.global.token = token;
    this.global.isLoggedIn = true;
    return token;
  }),
);
}

Controller:

    public function login(Request $request)
{
    $data = [
        'email' => $request['email'],
        'password' => $request['password'],
    ];

    try (auth()->attempt($data)) {
        $tokenResult = auth()->user()->createToken('Pesacount-User-Access-Token');
        $token = $tokenResult->accessToken;
        
        if ($request->remember_me)
            $token->expires_at = Carbon::now()->addWeeks(1);

        $token->save();

        return response()->json([
            'access_token' => $tokenResult->accessToken,
            'token_type' => 'Bearer',
            'loginStatus' => 'Successful'
            /*** 
            'expires_at' => Carbon::parse(
                $tokenResult->token->expires_at
            )->toDateTimeString()
            */
        ]);            
    
        return response()->json(['token' => $token], 200);

        
    } catch (Exception $e) {
        return response()->json(['error' => $e->getMessage()], 401);
    }
}

According to the logs, the error is caused when running

try (auth()->attempt($data)) {

I was originally running laravel 5.6 and everything worked okay. Upgraded to laravel 9 and thats when the error started

0 Answers
Related