I started a new Angular 6 project and decided to try out the new ng generate library
command.
The "main" feature of this library is a service:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { AuthResponseModel } from "./auth-response.model";
import { catchError, map } from 'rxjs/operators';
import { HttpErrorResponse } from '@angular/common/http';
import decode from 'jwt-decode'
@Injectable()
export class AuthService {
private authURL: string = "http://localhost:8090/oauth/token";
private loginPath : string = "/login";
isLoggedIn: boolean = false;
redirectURL: string;
public isRefreshing : boolean = false;
constructor(private http: HttpClient) {
this.redirectURL = "";
this.isLoggedIn = this.getToken() != null;
}
login(user: string, password: string): Observable<boolean> {
var data = new FormData();
data.append("grant_type", "password");
data.append("username", user);
data.append("password ", password);
const httpOptions = {
headers: new HttpHeaders({
'Authorization': 'Basic ' + window.btoa("web:secret")
})
};
return this.http.post<AuthResponseModel>(this.authURL, data, httpOptions)
.pipe(
map((r: AuthResponseModel) => {
if (r.access_token) {
localStorage.setItem("access_token", r.access_token);
localStorage.setItem("refresh_token", r.refresh_token);
this.isLoggedIn = true;
return true;
}
}
));
};
refresh() : Observable<any> {
var data = new FormData();
this.isRefreshing = true;
data.append("grant_type", "refresh_token");
data.append("refresh_token", this.getRefreshToken());
const httpOptions = {
headers: new HttpHeaders({
'Authorization': 'Basic ' + window.btoa("web:secret")
})
};
return this.http.post<AuthResponseModel>(this.authURL, data, httpOptions)
.pipe(
map( r => {
console.log("Old token" + this.getToken());
localStorage.setItem("access_token", r.access_token);
localStorage.setItem("refresh_token", r.refresh_token);
this.isLoggedIn = true;
this.isRefreshing = false;
console.log("New token" + r.access_token);
return r.access_token;
})
)
}
logout(): void {
localStorage.removeItem("access_token");
localStorage.removeItem("refresh_token");
this.isLoggedIn = false;
}
checkTokenExpired(checkRefresh = false) : boolean {
if(checkRefresh) { return decode(this.getRefreshToken()).exp < (Date.now().valueOf() / 1000); }
return decode(this.getToken()).exp < (Date.now().valueOf() / 1000);
}
getToken(): string {
return localStorage.getItem("access_token");
}
getRefreshToken() : string {
return localStorage.getItem('refresh_token');
}
}
Based upon the template code generated by ng generate library
, my public_api.ts
code follows:
/*
* Public API Surface of auth-lib
*/
export * from './lib/auth-lib.service';
export * from './lib/auth-interceptor';
export * from './lib/auth-response.model';
However, anytime I attempt to inject it into a @Component
.
import { Component, OnInit } from '@angular/core';
import { AuthService } from 'auth-lib';
import { Router } from '@angular/router';
import {Injectable} from '@angular/core';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
user : string;
pass : string;
constructor(private authService : AuthService, private router : Router) {
this.user = "";
this.pass = "";
}
I receive the following:
[Angular] Can't resolve all parameters for LoginComponent
.
The issue is related to the injection of AuthService
(removing it removes the error). I have referenced a few questions of StackOverflow (including this, this, and this one); however, the resolutions seems to revolve around "barrels" and forgetting the @Injectable
annotation. As such, I am not sure how to apply such resolutions to my problem.
Any advice would be appreciated.
Thanks.
EDIT:
Interestingly enough, if I change the import statement from import { AuthService } from 'auth-lib'
to import { AuthService } from 'projects/auth-lib/src/public_api';
, it does work, but that is not really the desired behavior.