我有以下可以对服务进行调用的Typescript方法,而该服务又会返回一个带有JWT标记的User对象:
populate() {
// If JWT detected, attempt to get & store user's info
if (this.jwtService.getToken()) {
this.apiService.get('/user')
.subscribe(
data => this.setAuth(data.user),
err => this.purgeAuth()
);
} else {
// Remove any potential remnants of previous auth states
this.purgeAuth();
}
}
这是我的user.model.ts的方式:
export class User {
email: string;
token: string;
username: string;
bio: string;
image: string;
}
对于一个简单的演示应用程序,我想摆脱对后端服务的调用,而是拥有一个标准的用户名和密码,并使用此标准凭据登录!
我知道我可以生成这个JWT令牌,但不知道如何 . 实际上,我想做以下事情:
假设我将用户名和密码捕获为常量,例如:
const user = 'abcdef'
const pass = 'abcdef'
我想创建一个新的用户模型,如:
let userModel = new User(
// set all the fields
// How do I generate a static JWT token for the above user and password that is valid for 2 hours?
)
然后,我将绕过对API的调用,并使用静态JWT令牌进一步调用我的Angular 4应用程序 . 有任何想法吗?