Я использую Ionic 4.10.2 и пытаюсь получить службу аутентификации, написанную с использованием примера из учебника, который я нашел (https://www.techiediaries.com/ionic-jwt-authentication-httpclient/, за исключением того, что я использую java REST-сервер на сервере)
Я получаю ошибку:
[ng] ERROR in src/app/auth/auth.service.ts(40,17): error TS2345: Argument of type 'MonoTypeOperatorFunction<AuthResponse>' is not assignable to parameter of type 'OperatorFunction<HttpResponse<AuthResponse>, AuthResponse>'.
[ng] Types of parameters 'source' and 'source' are incompatible.
[ng] Type 'Observable<HttpResponse<AuthResponse>>' is not assignable to type 'Observable<AuthResponse>'.
[ng] Type 'HttpResponse<AuthResponse>' is not assignable to type 'AuthResponse'.
[ng] Property 'user' is missing in type 'HttpResponse<AuthResponse>'.
[ng] src/app/web-services/test-page/test-page.page.ts(4,40): error TS2307: Cannot find module './test-rest-service.servi
ce'.
[ng] src/app/web-services/test-rest-service.service.ts(42,30): error TS2304: Cannot find name 'apiUrl'.
[ng]
Все ответы, которые я нашел в Интернете, говорят, что это может быть связано с несколькими версиями rxjs в проекте.Но я вижу только одно:
PS C:\projectFolder> npm list rxjs
MobileAppTemplate@0.0.1 C:\projectFolder
+-- @angular-devkit/architect@0.12.4
| `-- rxjs@6.3.3 deduped
+-- @angular-devkit/build-angular@0.12.4
| +-- @angular-devkit/build-webpack@0.12.4
| | `-- rxjs@6.3.3 deduped
| +-- @ngtools/webpack@7.2.4
| | `-- rxjs@6.3.3 deduped
| `-- rxjs@6.3.3 deduped
+-- @angular-devkit/core@7.2.4
| `-- rxjs@6.3.3 deduped
+-- @angular-devkit/schematics@7.2.4
| `-- rxjs@6.3.3 deduped
+-- @angular/cli@7.2.4
| +-- @schematics/update@0.12.4
| | `-- rxjs@6.3.3 deduped
| `-- inquirer@6.2.1
| `-- rxjs@6.3.3 deduped
`-- rxjs@6.3.3
Вот код:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { tap } from 'rxjs/operators';
import { Observable, BehaviorSubject } from 'rxjs';
import { Storage } from '@ionic/storage';
import { User } from './user';
import { AuthResponse } from './auth-response';
@Injectable({
providedIn: 'root'
})
export class AuthService {
AUTH_SERVER_ADDRESS: string = "http://SERVERIP:8080/WebServicesTemplate";
REGISTRATION_URL: string = this.AUTH_SERVER_ADDRESS + "/user/register";
LOGIN_URL: string = this.AUTH_SERVER_ADDRESS + "/login";
ACCESS_TOKEN_NAME: string = "ACCESS_TOKEN";
ACCESS_TOKEN_EXPIRATION_TIME: string = "EXPIRES_IN";
authSubject = new BehaviorSubject(false);
constructor(private httpClient: HttpClient, private storage: Storage) { }
register(user: User): Observable<AuthResponse> {
return this.httpClient.post<AuthResponse>(`${this.REGISTRATION_URL}`, user)
.pipe(
tap(async (response: AuthResponse) => {
if(response.user) {
await this.storage.set(this.ACCESS_TOKEN_NAME, response.user.accessToken);
await this.storage.set(this.ACCESS_TOKEN_EXPIRATION_TIME, response.user.expiresIn);
this.authSubject.next(true);
}
})
);
}// end register(.)
login(user: User): Observable<AuthResponse> {
return this.httpClient.post<AuthResponse>(`${this.LOGIN_URL}`, user, {observe: "response"})
.pipe(
tap(async (response: AuthResponse ) => {
if(response.user) {
await this.storage.set(this.ACCESS_TOKEN_NAME, response.user.accessToken);
await this.storage.set(this.ACCESS_TOKEN_EXPIRATION_TIME, response.user.expiresIn);
this.authSubject.next(true);
}
})
);
}// end login(.)
async logout() {
await this.storage.remove(this.ACCESS_TOKEN_NAME);
await this.storage.remove(this.ACCESS_TOKEN_EXPIRATION_TIME);
this.authSubject.next(false);
}
isLoggedIn() {
return this.authSubject.asObservable();
}
}
Что я могу сделать, чтобы решить эту проблему?