系好安全带,这个有点复杂。我知道快递会给浏览器发送一个connect.sid饼干..。Passport使用它来反序列化web请求中的用户。不仅如此,当我从我的NativeScript应用程序登录到我的应用程序时(我运行的是Windows上的Pixel 2仿真器,而且我知道它也在iOS上工作),这个cookie似乎被正确设置并与未来的web请求一起发送。我还理解application-settings API是如何工作的,您可以使用它来存储应用程序未来启动的用户标识令牌(这样我就不必每次登录)。
这就是断开的地方。可以想象,如果存储了请求头中的cookie,我可以覆盖它,但是在nativescript中,我找不到关于如何从成功的登录请求中检索cookie的文档。
下面是代码:
TokenSvc
import { Injectable } from "@angular/core";
import { getString, setString } from "application-settings";
export class TokenSvc {
static isLoggedIn(): boolean {
return !!getString("token");
}
static get token(): string {
return getString("token");
}
static set token(token: string) {
setString("token", token);
}
}登录组件
(请注意,我正在尝试从一个新的HttpHeaders实例中获取cookie.我不知道为什么我认为这会奏效。)
@Component({
selector: "app-login",
moduleId: module.id,
templateUrl: "./login.component.html",
styleUrls: ["./login.component.scss"]
})
export class LoginComponent {
credentials: ILoginCredentials;
@ViewChild("password") password: ElementRef;
@ViewChild("handle") handle: ElementRef;
@ViewChild("confirmPassword") confirmPassword: ElementRef;
constructor(private page: Page, private router: Router, private AuthSvc: AuthSvc, private _store: Store<AppStore>) {
this.page.actionBarHidden = true;
this.credentials = {
email: "",
password: "",
cPassword: "",
handle: "",
publicName: ""
};
}
login() {
const loginCredentials: ICredentials = {
username: this.credentials.email,
password: this.credentials.password,
rememberMe: false
};
this.AuthSvc.login(loginCredentials).subscribe(
(payload) => {
console.log(payload);
if (payload.failure) {
alert(payload.failure);
} else {
// user!
let cookies = new HttpHeaders().get("Cookie");
console.log(cookies);
TokenSvc.token = cookies;
this._store.dispatch({ type: "SET_USER", payload: payload });
this.router.navigate(["/tabs"]);
}
}, () => alert("Unfortunately we were unable to create your account.")
);
}
}这里的基本问题是..。如何在具有Node/Express后端的NativeScript应用程序设置中持久化基于cookie的会话?
发布于 2018-06-11 09:09:29
最基本的答案是:你没有。
当涉及到移动开发时,更喜欢JWT、OAuth2或其他基于令牌的身份验证方法。您也可以对web使用相同的身份验证方法。
使用安全存储存储用户令牌,并将令牌与用户提出的任何请求一起发送。
https://stackoverflow.com/questions/50776728
复制相似问题