我在用angular 7开发的应用程序中有一个用于身份验证服务的代码。我需要恢复在API标头中发送的数据,但这些数据为空。
这是autheticationService:
import {Injectable} from '@angular/core';
import {HttpClient, HttpResponse, HttpHeaders} from '@angular/common/http';
import {BehaviorSubject, Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {User} from '../../models/index';
import {global} from '../global';
@Injectable({providedIn: 'root'})
export class AuthenticationService {
private currentUserSubject: BehaviorSubject<User>;
public currentUser: Observable<User>;
constructor(private http: HttpClient) {
this.currentUserSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem('currentUser')));
this.currentUser = this.currentUserSubject.asObservable();
}
public get currentUserValue(): User {
return this.currentUserSubject.value;
}
login(email: string, password: string): Observable<HttpResponse<any>> {
return this.http.post(`${global.URL}/user/login`, {email, password}, {observe: 'response'});
}
logout() {
localStorage.removeItem('currentUser');
this.currentUserSubject.next(null);
}
}
这是登录组件
import {Component, OnInit} from '@angular/core';
import {FormControl, FormGroup, Validators, FormBuilder} from '@angular/forms';
import {User} from '../../models/index';
import {Router, ActivatedRoute} from '@angular/router';
import {AuthenticationService} from '../../services/index';
import {first} from 'rxjs/operators';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
loginForm: FormGroup;
model: User;
constructor(private route: ActivatedRoute, private router: Router, formBuilder: FormBuilder, private authenticationService: AuthenticationService) {
this.model = new User();
this.loginForm = formBuilder.group({
email: new FormControl(this.model.email, [Validators.required, Validators.email]),
password: new FormControl(this.model.password, [Validators.required]),
});
}
ngOnInit() {
this.authenticationService.logout();
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
}
submitForm() {
if (this.loginForm.invalid) {
return;
}
this.authenticationService.login(this.form.email.value, this.form.password.value)
.subscribe(
data=>{
console.log(data);
},
error=>{
console.log(error)
}
}
}
}
响应如下
这是标题
我需要找回令牌,但这没有来。