我在Angular 2中申请了用户登录和注册,但登录后我无法对用户进行身份验证(即检查用户是否已登录)。我已经将烧瓶的后端显影了。
任何人都可以帮助我如何维护用户的真实性。
login.component.ts是:
import { Component,OnInit } from '@angular/core';
import {AuthService} from '../services/auth.service';
import {User} from '../models/user';
@Component({
selector: 'login',
templateUrl: `./login.component.html`
})
export class LoginComponent {
user: User = new User();
constructor(private auth: AuthService) {}
onLogin(): void{
this.auth.login(this.user)
.then((user) => {
console.log(user);
})
.catch((err) => {
console.log(err);
});
}
auth.service.ts
import { Injectable } from '@angular/core';
import {HttpClient, HttpErrorResponse,HttpHeaders} from '@angular/common/http';
//import 'rxjs/add/operator/toPromise';
import {API_URL} from '../env';
import {map,catchError} from 'rxjs/operators';
import { throwError } from 'rxjs';
@Injectable()
export class AuthService {
test(): string {
return 'working';
}
constructor(private http: HttpClient) {}
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'my-auth-token'
})
};
private static _handleError(err: Response | any) {
return throwError(err || 'Error: Unable to complete request.');
// return "error"
}
login(user) : Promise<any>{
return this.http.post(`${API_URL}/login`,user, this.httpOptions).toPromise()
catchError(AuthService._handleError)
}
在烧瓶的后端,我实现了如下登录方法:
@app.route('/login', methods = ['POST'])
def login():
data1 = request.get_json(force=True)
mydoc = mycol.find(data1)
y = None
for x in mydoc:
print(x)
y = x
if (y == None):
return jsonify({'error': 'error'})
else :
return jsonify({'done':'done'})