-1
I am developing an Angular project from NGX-Rocket. And I am using the authentication service already implemented in NGX.
In the archive app/core/http/http.service.ts I am importing a Service located in app/core/Authentication/credentials.service.ts and trying to use the function refreshToken()
located in credentials.service.ts'.
However, when executing nothing happens. No error is displayed and also the function does not seem to be executed.
Below are the two files I’m working on.
Ps: This happens in all project service files. We already have components
i can call the services functions normally.
CREDENTIAL.SERVICE
import { Injectable } from '@angular/core';
export interface Credentials {
// Customize received credentials here
email: string;
token: string;
expiration?: number;
}
const credentialsKey = 'credentials';
/**
* Provides storage for authentication credentials.
* The Credentials interface should be replaced with proper implementation.
*/
@Injectable({
providedIn: 'root'
})
export class CredentialsService {
private _credentials: Credentials | null = null;
constructor() {
const savedCredentials = sessionStorage.getItem(credentialsKey) || localStorage.getItem(credentialsKey);
if (savedCredentials) {
this._credentials = JSON.parse(savedCredentials);
}
}
/**
* Checks is the user is authenticated.
* @return True if the user is authenticated.
*/
isAuthenticated(): boolean {
return !!this.credentials;
}
/**
* Gets the user credentials.
* @return The user credentials or null if the user is not authenticated.
*/
get credentials(): Credentials | null {
return this._credentials;
}
/**
* Sets the user credentials.
* The credentials may be persisted across sessions by setting the `remember` parameter to true.
* Otherwise, the credentials are only persisted for the current session.
* @param credentials The user credentials.
* @param remember True to remember credentials across sessions.
*/
setCredentials(credentials?: Credentials, remember?: boolean) {
this._credentials = credentials || null;
if (credentials) {
const minutes = 1 * 60 * 1000;
const expiration = Date.now() + minutes;
credentials.expiration = expiration; //set expiration
const storage = remember ? localStorage : sessionStorage;
storage.setItem(credentialsKey, JSON.stringify(credentials));
} else {
sessionStorage.removeItem(credentialsKey);
localStorage.removeItem(credentialsKey);
}
}
refreshToken() {
return true;
}
}
HTTP.SERVICE.TS
import { Inject, Injectable, InjectionToken, Injector, Optional, Type } from '@angular/core';
import { HttpClient, HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { ErrorHandlerInterceptor } from './error-handler.interceptor';
import { CacheInterceptor } from './cache.interceptor';
import { ApiPrefixInterceptor } from './api-prefix.interceptor';
import { CredentialsService } from './../authentication/credentials.service';
// HttpClient is declared in a re-exported module, so we have to extend the original module to make it work properly
// (see https://github.com/Microsoft/TypeScript/issues/13897)
declare module '@angular/common/http/http' {
// Augment HttpClient with the added configuration methods from HttpService, to allow in-place replacement of
// HttpClient with HttpService using dependency injection
export interface HttpClient {
/**
* Enables caching for this request.
* @param forceUpdate Forces request to be made and updates cache entry.
* @return The new instance.
*/
cache(forceUpdate?: boolean): HttpClient;
/**
* Skips default error handler for this request.
* @return The new instance.
*/
skipErrorHandler(): HttpClient;
/**
* Do not use API prefix for this request.
* @return The new instance.
*/
disableApiPrefix(): HttpClient;
}
}
// From @angular/common/http/src/interceptor: allows to chain interceptors
class HttpInterceptorHandler implements HttpHandler {
constructor(private next: HttpHandler, private interceptor: HttpInterceptor) {}
handle(request: HttpRequest<any>): Observable<HttpEvent<any>> {
return this.interceptor.intercept(request, this.next);
}
}
/**
* Allows to override default dynamic interceptors that can be disabled with the HttpService extension.
* Except for very specific needs, you should better configure these interceptors directly in the constructor below
* for better readability.
*
* For static interceptors that should always be enabled (like ApiPrefixInterceptor), use the standard
* HTTP_INTERCEPTORS token.
*/
export const HTTP_DYNAMIC_INTERCEPTORS = new InjectionToken<HttpInterceptor>('HTTP_DYNAMIC_INTERCEPTORS');
/**
* Extends HttpClient with per request configuration using dynamic interceptors.
*/
@Injectable({
providedIn: 'root'
})
export class HttpService extends HttpClient {
constructor(
private httpHandler: HttpHandler,
private injector: Injector,
@Optional() @Inject(HTTP_DYNAMIC_INTERCEPTORS) private interceptors: HttpInterceptor[] = [],
private credentialsService?: CredentialsService
) {
super(httpHandler);
if (!this.interceptors) {
// Configure default interceptors that can be disabled here
this.interceptors = [this.injector.get(ApiPrefixInterceptor), this.injector.get(ErrorHandlerInterceptor)];
}
}
cache(forceUpdate?: boolean): HttpClient {
const cacheInterceptor = this.injector
.get(CacheInterceptor as Type<CacheInterceptor>)
.configure({ update: forceUpdate });
return this.addInterceptor(cacheInterceptor);
}
skipErrorHandler(): HttpClient {
return this.removeInterceptor(ErrorHandlerInterceptor);
}
disableApiPrefix(): HttpClient {
return this.removeInterceptor(ApiPrefixInterceptor);
}
// Override the original method to wire interceptors when triggering the request.
request(method?: any, url?: any, options?: any): any {
const credentials = this.credentialsService.credentials;
if (credentials && Date.now() > credentials.expiration) {
console.log('é maior');
this.credentialsService.refreshToken;
}
const handler = this.interceptors.reduceRight(
(next, interceptor) => new HttpInterceptorHandler(next, interceptor),
this.httpHandler
);
return new HttpClient(handler).request(method, url, options);
}
private removeInterceptor(interceptorType: Type<HttpInterceptor>): HttpService {
return new HttpService(
this.httpHandler,
this.injector,
this.interceptors.filter(i => !(i instanceof interceptorType)),
this.credentialsService
);
}
private addInterceptor(interceptor: HttpInterceptor): HttpService {
return new HttpService(
this.httpHandler,
this.injector,
this.interceptors.concat([interceptor]),
this.credentialsService
);
}
}
Does it have anything to do with this?
@Injectable({ providedIn: 'root' })
I noticed that the files are not called in app.module.ts
yes in that index.ts
Print of the file structure:
This function looks very simple and only returns true, when you call it returns true?
– Eduardo Vargas