NullInjectorError: No provider for AuthenticationService? - javascript

this is the authentication page:-
import {Injectable} from '#angular/core';
import {HttpClient, HttpHeaders} from '#angular/common/http';
import {SECURITY} from '../constant/SecurityAPI';
import {CONFIG} from '../config';
#Injectable()
export class AuthenticationService {
constructor(private http: HttpClient) {
}
public isUserLoggedIn() {
return localStorage.getItem('token') === undefined;
}
public login(credentials) {
return new Promise((resolve, reject) => {
console.log('username ' , credentials.userName, ' pass ' , credentials.password );
const data = {userName: credentials.userName, password: credentials.password, orgId: CONFIG.ORG_ID};
this.http.post(SECURITY.GET_LOGIN_URL, JSON.stringify(data), {
headers: new HttpHeaders().set('Content-Type', 'application/json')
})
.subscribe(res => {
console.log('res ' , res);
resolve(res);
}, (err) => {
reject(err);
});
});
}
}

You need to specify in #Injectable decorator where you want to provide this service
#Injectable({
providedIn: 'root'
})
export class AuthenticationService {
...
}
or to add it in app.module.ts providers:
providers: [
AuthenticationService,
any other services,
...
]

Related

ERROR TypeError: Cannot read property 'subscribe' of undefined on POST HTTP Call - Angular [8]

I'm trying to Post data to rest API. I'm using Postman for my rest API.
Getting Cannot read property 'subscribe' of undefined on POST HTTP Call in console log:
My rest API: "dradiobeats.x10host.com/api/areas"
userService.ts:
import { Injectable, Input } from "#angular/core";
import { HttpClient, HttpHeaders } from "#angular/common/http";
import { userArray } from "./users.model";
import { Observable } from "rxjs";
const httpOptions = {
headers: new HttpHeaders({
"Content-Type": "application/json",
Authorization:
"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImYyOTc3OTBmODc3ODlhYzg3MGE2ZmU3YTY0YzY2YmIwOGU4M2Q0ZmQzY2IyNmNiNWU3NDEzMTFmZjExMDk4NTA5NWUzN2IxN2I5YmI2YmFjIn0.eyJhdWQiOiIyIiwianRpIjoiZjI5Nzc5MGY4Nzc4OWFjODcwYTZmZTdhNjRjNjZiYjA4ZTgzZDRmZDNjYjI2Y2I1ZTc0MTMxMWZmMTEwOTg1MDk1ZTM3YjE3YjliYjZiYWMiLCJpYXQiOjE1NzU4NzM4MzksIm5iZiI6MTU3NTg3MzgzOSwiZXhwIjoxNjA3NDk2MjM5LCJzdWIiOiIyIiwic2NvcGVzIjpbXX0.J3nMXcPpqlRVvIRkrVAMblSUwdlXFmrkn9SPD2WE1DwdiqAMdhay8zAeD550ta9qWiNxHOKMAWF8t3H9cIgItaB9ZX2CxoxzS5P1nJFzit8qxiB-gzJL3mpybrnLtrKGjxsM5i_lBvdJsnhWzi15jKWIu-RNxUYPnXCjuxnXYEiyoJg17hsYUh4910VfFWx4R3WvH7WOvczF53IDKyX5fSTt4WSJUqciuNepkO6Klc8sj_yPmDPQltUjUXSSplkOQ8sL5uHk7PmzSjIfoR8RC0A-YQqI9mbZMTyJ0IyKoAHvRHF8q1cW5qfUmLXTgxcCTmFPqXqIlcAoOoJMCxke5fl0PuK0rgU7dxouATk_3B6cio7-7Zgps0iopDpk2nm-o40mjSiOUGb2kyKckYN09orYuan5wEd1KJ873ejKEgBWOhJu4gQFps8M9VoDXncAqMxeBqbUY1UZENx_n6uduQ_SAY4rgIUFCixfNc5Y_-HLDa108u4-z3APGbdxrhEdZXyHz9xQTaLrWcU_iCJ5g_ObT5VGZHtawZbfOYm2ZZpjPiCZpXunhrsbAcHBX64akWcehmT2gUJqPsxvaObKN3nayML1NHtdZGgAHUE89clhIH610Fod0C_jMTqpU7IkY9aSU781HsQVlHNw3qGbTinWfYPDBG0Lkp9NnmRe9BU",
Accept: "application/json"
})
};
#Injectable({
providedIn: "root"
})
export class UsersService {
users$: userArray[];
apiUrl = "http://dradiobeats.x10host.com/api/areas";
delUrl = "http://dradiobeats.x10host.com/api/areas";
constructor(private _http: HttpClient) {}
getUsers() {
return this._http.get<userArray[]>(this.apiUrl);
}
deleteUser(id: userArray): Observable<userArray> {
const url = `${this.apiUrl}/${id}`;
console.log();
return this._http.delete<userArray>(url, httpOptions);
}
onSubmit(users$: userArray): Observable<userArray> {
console.log(users$);
this._http.post<userArray>(this.apiUrl, users$, httpOptions);
}
}
add-post.component.ts:
import { Component, OnInit } from "#angular/core";
import { UsersService } from "src/app/users.service";
import { userArray } from "src/app/users.model";
#Component({
selector: "app-add-posts",
templateUrl: "./add-posts.component.html",
styleUrls: ["./add-posts.component.css"]
})
export class AddPostsComponent implements OnInit {
name: string;
description: string;
domain: string;
picture: string;
id: number = 29;
constructor(private userService: UsersService) {}
users: userArray[];
ngOnInit() {}
onSubmit() {
const users$ = {
name: this.name,
description: this.description,
domain: this.domain,
picture: this.picture
};
this.userService.onSubmit(users$).subscribe();
}
}
Can someone please help?
You need to return the http call observable
Try:
onSubmit(users$: userArray): Observable<userArray> {
console.log(users$);
return this._http.post<userArray>(this.apiUrl, users$, httpOptions);
}
As onSubmit() has an Observable signature, it must return an observable. You must change your function to 'return this.http.post ...'
You forgot to return, As only observable can be subscribe
import { Injectable, Input } from "#angular/core";
import { HttpClient, HttpHeaders } from "#angular/common/http";
import { userArray } from "./users.model";
import { Observable } from "rxjs";
const httpOptions = {
headers: new HttpHeaders({
"Content-Type": "application/json",
Authorization:
"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImYyOTc3OTBmODc3ODlhYzg3MGE2ZmU3YTY0YzY2YmIwOGU4M2Q0ZmQzY2IyNmNiNWU3NDEzMTFmZjExMDk4NTA5NWUzN2IxN2I5YmI2YmFjIn0.eyJhdWQiOiIyIiwianRpIjoiZjI5Nzc5MGY4Nzc4OWFjODcwYTZmZTdhNjRjNjZiYjA4ZTgzZDRmZDNjYjI2Y2I1ZTc0MTMxMWZmMTEwOTg1MDk1ZTM3YjE3YjliYjZiYWMiLCJpYXQiOjE1NzU4NzM4MzksIm5iZiI6MTU3NTg3MzgzOSwiZXhwIjoxNjA3NDk2MjM5LCJzdWIiOiIyIiwic2NvcGVzIjpbXX0.J3nMXcPpqlRVvIRkrVAMblSUwdlXFmrkn9SPD2WE1DwdiqAMdhay8zAeD550ta9qWiNxHOKMAWF8t3H9cIgItaB9ZX2CxoxzS5P1nJFzit8qxiB-gzJL3mpybrnLtrKGjxsM5i_lBvdJsnhWzi15jKWIu-RNxUYPnXCjuxnXYEiyoJg17hsYUh4910VfFWx4R3WvH7WOvczF53IDKyX5fSTt4WSJUqciuNepkO6Klc8sj_yPmDPQltUjUXSSplkOQ8sL5uHk7PmzSjIfoR8RC0A-YQqI9mbZMTyJ0IyKoAHvRHF8q1cW5qfUmLXTgxcCTmFPqXqIlcAoOoJMCxke5fl0PuK0rgU7dxouATk_3B6cio7-7Zgps0iopDpk2nm-o40mjSiOUGb2kyKckYN09orYuan5wEd1KJ873ejKEgBWOhJu4gQFps8M9VoDXncAqMxeBqbUY1UZENx_n6uduQ_SAY4rgIUFCixfNc5Y_-HLDa108u4-z3APGbdxrhEdZXyHz9xQTaLrWcU_iCJ5g_ObT5VGZHtawZbfOYm2ZZpjPiCZpXunhrsbAcHBX64akWcehmT2gUJqPsxvaObKN3nayML1NHtdZGgAHUE89clhIH610Fod0C_jMTqpU7IkY9aSU781HsQVlHNw3qGbTinWfYPDBG0Lkp9NnmRe9BU",
Accept: "application/json"
})
};
#Injectable({
providedIn: "root"
})
export class UsersService {
users$: userArray[];
apiUrl = "http://dradiobeats.x10host.com/api/areas";
delUrl = "http://dradiobeats.x10host.com/api/areas";
constructor(private _http: HttpClient) {}
getUsers() {
return this._http.get<userArray[]>(this.apiUrl);
}
deleteUser(id: userArray): Observable<userArray> {
const url = `${this.apiUrl}/${id}`;
console.log();
return this._http.delete<userArray>(url, httpOptions);
}
onSubmit(users$: userArray): Observable<userArray> {
console.log(users$);
return this._http.post<userArray>(this.apiUrl, users$, httpOptions);
}
}

Refresh token (JWT) in interceptor Angular 6

Initially, I had a function that simply checked for the presence of a token and, if it was not present, sent the user to the login header. Now I need to implement the logic of refreshing a token when it expires with the help of a refreshing token. But I get an error 401. The refresh function does not have time to work and the work in the interceptor goes further to the error. How can I fix the code so that I can wait for the refresh to finish, get a new token and not redirect to the login page?
TokenInterceptor
import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "#angular/common/http";
import {Injectable, Injector} from "#angular/core";
import {AuthService} from "../services/auth.service";
import {Observable, throwError} from "rxjs";
import {catchError, tap} from "rxjs/operators";
import {Router} from "#angular/router";
import {JwtHelperService} from "#auth0/angular-jwt";
#Injectable({
providedIn: 'root'
})
export class TokenInterceptor implements HttpInterceptor{
private auth: AuthService;
constructor(private injector: Injector, private router: Router) {}
jwtHelper: JwtHelperService = new JwtHelperService();
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.auth = this.injector.get(AuthService);
const accToken = this.auth.getToken();
const refToken = this.auth.getRefreshToken();
if ( accToken && refToken ) {
if ( this.jwtHelper.isTokenExpired(accToken) ) {
this.auth.refreshTokens().pipe(
tap(
() => {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${accToken}`
}
});
}
)
)
} else {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${accToken}`
}
});
}
}
return next.handle(req).pipe(
catchError(
(error: HttpErrorResponse) => this.handleAuthError(error)
)
);
}
private handleAuthError(error: HttpErrorResponse): Observable<any>{
if (error.status === 401) {
this.router.navigate(['/login'], {
queryParams: {
sessionFailed: true
}
});
}
return throwError(error);
}
}
AuthService
import {Injectable} from "#angular/core";
import {HttpClient, HttpHeaders} from "#angular/common/http";
import {Observable, of} from "rxjs";
import {RefreshTokens, Tokens, User} from "../interfaces";
import {map, tap} from "rxjs/operators";
#Injectable({
providedIn: 'root'
})
export class AuthService{
private authToken = null;
private refreshToken = null;
constructor(private http: HttpClient) {}
setToken(authToken: string) {
this.authToken = authToken;
}
setRefreshToken(refreshToken: string) {
this.refreshToken = refreshToken;
}
getToken(): string {
this.authToken = localStorage.getItem('auth-token');
return this.authToken;
};
getRefreshToken(): string {
this.refreshToken = localStorage.getItem('refresh-token');
return this.refreshToken;
};
isAuthenticated(): boolean {
return !!this.authToken;
}
isRefreshToken(): boolean {
return !!this.refreshToken;
}
refreshTokens(): Observable<any> {
const httpOptions = {
headers: new HttpHeaders({
'Authorization': 'Bearer ' + this.getRefreshToken()
})
};
return this.http.post<RefreshTokens>('/api2/auth/refresh', {}, httpOptions)
.pipe(
tap((tokens: RefreshTokens) => {
localStorage.setItem('auth-token', tokens.access_token);
localStorage.setItem('refresh-token', tokens.refresh_token);
this.setToken(tokens.access_token);
this.setRefreshToken(tokens.refresh_token);
console.log('Refresh token ok');
})
);
}
}
In your example you never subscribe to your refreshTokens().pipe() code. Without a subscription, the observable won't execute.
req = this.auth.refreshTokens().pipe(
switchMap(() => req.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getToken()}`
}
}))
)
This will first call refreshToken and run the tap there, then emit request with the new this.auth.getToken(), note that accToken still have old value as the code is not rerun.
You have to do something like that:
const firstReq = cloneAndAddHeaders(req);
return next.handle(firstReq).pipe(
catchError(
err => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401 || err.status === 403) {
if (firstReq.url === '/api2/auth/refresh') {
auth.setToken('');
auth.setRefreshToken('');
this.router.navigate(['/login']);
} else {
return this.auth.refreshTokens()
.pipe(mergeMap(() => next.handle(cloneAndAddHeaders(req))));
}
}
return throwError(err.message || 'Server error');
}
}
)
);
The implementation of cloneAndAddHeaders should be something like this:
private cloneAndAddHeaders(request: HttpRequest<any>): HttpRequest<any> {
return request.clone({
setHeaders: {
Authorization: `YourToken`
}
});
}

Storing Registration Data in Firebase

I need help with storing registration data in Firebase.
I want to have email name of current logged user stored in Firebase database.
Pls help me. I am writhing code where user can write on profile of onother user.
//auth service
import { Injectable } from "#angular/core";
import { AngularFireAuth } from "#angular/fire/auth";
import { Observable } from "rxjs";
import "rxjs/add/operator/map";
#Injectable()
export class AuthService {
constructor(private afAuth: AngularFireAuth) {}
login(email: string, password: string) {
return new Promise((resolove, reject) => {
this.afAuth.auth
.signInWithEmailAndPassword(email, password)
.then(userData => resolove(userData), err => reject(err));
});
}
getAuth() {
return this.afAuth.authState.map(auth => auth);
}
logout() {
this.afAuth.auth.signOut();
}
register(email: string, password: string) {
return new Promise((resolove, reject) => {
this.afAuth.auth
.createUserWithEmailAndPassword(email, password)
.then(userData => resolove(userData), err => reject(err));
});
}
}
**register component**
import { Component, OnInit } from "#angular/core";
import { AuthService } from "../../service/auth.service";
import { Router } from "#angular/router";
#Component({
selector: "app-register",
templateUrl: "./register.component.html",
styleUrls: ["./register.component.css"]
})
export class RegisterComponent implements OnInit {
email: string;
password: string;
constructor(private authService: AuthService, private router: Router) {}
ngOnInit() {}
onSubmit() {
this.authService
.register(this.email, this.password)
.then(res => {
this.router.navigate(["/"]);
})
.catch(err => console.log(err.message));
}
}

Angular 5 component expecting an argument

Im trying a simple profile app, and all the sudden Im getting error TS2554
ERROR in /app/components/profile/profile.component.ts(25,3): error TS2554: Expected 1 arguments, but got 0.
import { Component, OnInit } from '#angular/core';
import { AuthService } from '../../services/auth.service';
import { FlashMessagesService } from 'angular2-flash-messages';
import { Router } from '#angular/router';
#Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
user: Object;
constructor(
private auth: AuthService,
private flashMsg: FlashMessagesService,
private router: Router
) {
}
ngOnInit() {
this.auth.getProfile().subscribe( profile => {
this.user = profile.user;
},
err => {
return false;
});
}
}
auth.service.ts
import { Injectable } from '#angular/core';
import { Http, Headers } from '#angular/http';
import 'rxjs/add/operator/map';
import { tokenNotExpired } from 'angular2-jwt';
#Injectable()
export class AuthService {
authToken: any;
user: any;
constructor(
private http: Http
) {
}
getProfile(user) {
let headers = new Headers();
this.loadToken();
headers.append('Authorization', this.authToken);
headers.append('Content-Type','application/json');
return this.http.get('http://localhost:3000/users/profile', {headers:headers})
.map(res => res.json());
}
loadToken() {
const token = localStorage.getItem('id_token');
this.authToken = token;
}
}
Your getProfile is expecting an argument named user but you are not passing it from the component
You need to pass an argument as follows,
this.auth.getProfile(user).subscribe( profile => {
this.user = profile.user;
},
err => {
return false;
});
or if you don't need an argument , remove it from your service method.

http.post not working in angular 2

Why is my http.post not working in angular 2. Just can't figure out what I have been missing.
Here is login.component.ts file
export class LoginComponent implements OnInit{
model: any = {};
loading = false;
constructor(
private router: Router,
private authenticationService: AuthenticationService,
private alertService: AlertService
){}
ngOnInit(){
// reset login status
this.authenticationService.logout();
}
login(){
let errMsg: string;
this.loading = true;
this.authenticationService.login(this.model.username, this.model.password)
.subscribe(
data => {
console.log('Success');
//this.router.navigate(['/']);
},
error => {
console.log('Error');
this.loading = false;
}
);
}
Here is the authentication.service.ts
#Injectable()
export class AuthenticationService{
private baseUrl = '..';
constructor(private http: Http){}
login(username: string, password: string): Observable<Response>{
let body = JSON.stringify({username: username, password: password});
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers});
const url = `${this.baseUrl}/api/auth/login`;
return this.http.post(url, body, options)
.map(this.extractData)
.catch(this.handleError);
}
logout(){
// remove user from local storage to log user out
localStorage.removeItem('currentUser');
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
private handleError (error: Response | any) {
// In a real world app, we might use a remote logging infrastructure
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
Here is the routes file
const appRoutes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'home', component: HomeComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
// otherwise redirect to home
{ path: '**', redirectTo: '' }
];
#NgModule({
imports: [ RouterModule.forRoot(appRoutes) ],
exports: [ RouterModule ]
})
export class AppRoutingMoudle{}
Any help would be appreciated. Thank You!
import { Injectable } from '#angular/core';
import {Headers, Http, Response,RequestOptions} from '#angular/http';
import 'rxjs/add/operator/toPromise';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
#Injectable()
import these above your services , then do it on constructor
constructor(private _http:Http) { }
then youcan use it with this._http.post

Categories

Resources