Node.js 无法访问localStorage 4
我正在使用angular 4和Node.js 无法访问localStorage 4,node.js,angular,Node.js,Angular,我正在使用angular 4和nodejs构建一个网站。当我尝试登录时,节点将数据发送到angular,这很好。只是当我尝试登录时,数据不会存储在本地存储中 这是auth.services.ts import { Injectable } from '@angular/core'; import { Http, Headers } from '@angular/http'; import 'rxjs/add/operator/map'; import { tokenNotExpired } f
nodejs
构建一个网站。当我尝试登录时,节点
将数据发送到angular,这很好。只是当我尝试登录时,数据不会存储在本地存储中
这是auth.services.ts
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import { tokenNotExpired } from 'angular2-jwt';
import { Router } from '@angular/router';
//import { User } from '../../User';
@Injectable()
export class AuthService {
authToken: any;
user: any;
constructor(private http:Http, private router: Router) {
console.log("authorization service initialized...");
}
signIn(user) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:3000/users/login', JSON.stringify(user), {headers: headers})
.map(res => res.json());
}
storeUserData(token, user) {
console.log("storeuserdata");
localStorage.setItem('id_token', token);
localStorage.setItem('user', JSON.stringify(user));
this.authToken = token;
this.user = user;
}
logout() {
this.authToken = null;
this.user = null;
localStorage.clear();
this.router.navigateByUrl('/');
}
getProfile() {
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() {
this.authToken = localStorage.getItem('id_token');
}
loggedIn() {
return tokenNotExpired();
}
}
这里是navbar.component.ts,因为它包含登录部分
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators} from '@angular/forms';
import { Router } from '@angular/router';
import { AuthService } from '../../services/auth.service';
//import { User } from '../../../User';
@Component({
moduleId: module.id,
selector: 'navbar',
templateUrl: `./navbar.component.html`,
})
export class NavBarComponent implements OnInit{
signInForm: FormGroup;
constructor(private authService: AuthService, private router: Router) {}
OnSignIn() {
const user = {
email: this.signInForm.value.email,
password: this.signInForm.value.password
};
this.authService.signIn(user)
.subscribe(
data => {
console.log(data);
if (data.success) {
this.authService.storeUserData(data.token, data.user);
this.router.navigateByUrl('profile');
} else {
}
},
error => console.log(error)
);
this.signInForm.reset();
}
ngOnInit() {
this.signInForm = new FormGroup({
email: new FormControl(null, [
Validators.required,
Validators.pattern("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
]),
password: new FormControl(null, Validators.required)
});
}
onLogout() {
this.authService.logout();
}
}
这是我尝试登录时收到的信息
Object {success: true, token: "JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyIkX18iO…Tc0fQ.rte25znZW9oASt-6scViAbA37W1DzXxCcg0Z_CMKWTY", user: Object}
我也从NodeJS部分获得了成功
当我建立这个网站时,我使用的是angular 2。我忙于上课,没有时间完成,angular 4发布了。我将所有代码复制到一个新目录中,并确保所有节点模块都是从一开始构建的
这是我以前的package.json:
{
"name": "angular-src",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/common": "^2.4.0",
"@angular/compiler": "^2.4.0",
"@angular/core": "^2.4.0",
"@angular/forms": "^2.4.0",
"@angular/http": "^2.4.0",
"@angular/platform-browser": "^2.4.0",
"@angular/platform-browser-dynamic": "^2.4.0",
"@angular/router": "^3.4.0",
"@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.20",
"angular2-jwt": "^0.1.28",
"core-js": "^2.4.1",
"ng2-uploader": "^2.0.0",
"ngx-uploader": "^2.2.5",
"rxjs": "^5.1.0",
"zone.js": "^0.7.6"
},
"devDependencies": {
"@angular/cli": "^1.0.0",
"@angular/compiler-cli": "^2.4.0",
"@types/jasmine": "2.5.38",
"@types/node": "~6.0.60",
"codelyzer": "~2.0.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.0.0",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^0.2.0",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"tslint": "~4.4.2",
"typescript": "~2.0.0"
}
}
这是新的package.json
{
"name": "angular-src",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/forms": "^4.0.0",
"@angular/http": "^4.0.0",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
"angular2-jwt": "^0.2.3",
"core-js": "^2.4.1",
"ngx-uploader": "^3.0.5",
"rxjs": "^5.1.0",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@angular/cli": "1.0.6",
"@angular/compiler-cli": "^4.0.0",
"@types/jasmine": "2.5.38",
"@types/node": "~6.0.60",
"codelyzer": "~2.0.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-coverage-istanbul-reporter": "^0.2.0",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"tslint": "~4.5.0",
"typescript": "~2.2.0"
}
}
当我使用旧版本的node_模块运行应用程序时,它可以正常工作,但当我使用新版本时,用户从未登录。您可以尝试使用此软件包。如果您不使用此软件包,这可能会有所帮助。您如何知道它未存储在localStorage中?我将尝试使用该软件包。当我尝试登录时,我会在chrome developer tools中检查本地存储,它不在那里,但当我停止服务器并运行使用旧angular版本的项目时,用户会自动登录并显示在本地存储中。这就是为什么我认为它只是出于某种原因没有访问本地存储当在inspector中单击“local storage”时,您是否看到任何东西,或者只是箭头改变了方向?你能不能至少把你的URL命名为“本地存储”?我唯一看到的就是箭头改变了方向。本地存储中没有url