我正在使用角度6,消耗休息api,后端是laravel php.没有令牌,如果您已登录,则会获得x会话,您可以在邮递员标题中看到它
X-Session ?1948c514b7e5669c284e85d6f612f9bd491
X-Session-Expiry ?2038-08-02T09:19:03+00:00
Run Code Online (Sandbox Code Playgroud)
如何检查x-session和X-Session-Expiry角度的值?在api endpoind中没有任何与session相关的内容.我需要知道会话是否仍然打开以及会话何时到期,我需要将用户注销.
登录时可以从角度访问这些x会话值吗?它们存储在标题中吗?
服务
login(username, password) {
const data = {
username: username,
password: password
};
const headers = new HttpHeaders();
headers.set('Content-Type', 'application/json');
localStorage.setItem('headers', JSON.stringify(headers));
return this.http.post(this.login_url, data, { headers: headers });
}
Run Code Online (Sandbox Code Playgroud)
我试图向用户服务提供相同的会话
getUsers() {
const headers = JSON.parse(localStorage.getItem('headers'));
return this.http.get(this.users_url, { headers: headers });
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我没有得到任何用户,是否有解决方案?
一周内两次,我对一个问题提出赏金,然后回答它,我能做什么?我无法删除它。
所以事实证明我需要更改我的登录功能
login(username, password) {
const data = {
username: username,
password: password
};
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post(this.login_url, data, { headers: headers, observe: 'response' });
}
Run Code Online (Sandbox Code Playgroud)
这就是你获得 x-session 的方式
onLogin() {
this.auth.login(this.username, this.password).subscribe(data => {
this.auth.setLoggedIn(true);
localStorage.setItem('login', JSON.stringify(this.auth.isLoggedIn));
if (localStorage.getItem('data') === null) {
localStorage.setItem('data', JSON.stringify(data));
}
const session = data.headers.get('x-session');
const expiry = data.headers.get('x-session-expiry');
localStorage.setItem('session', JSON.stringify(session));
localStorage.setItem('session-expiry', JSON.stringify(expiry));
this.router.navigate(['']);
}, err => {
console.log(err);
});
}
Run Code Online (Sandbox Code Playgroud)
这就是你如何使用它,例如getUsers从上面
getUsers() {
const session = JSON.parse(localStorage.getItem('session'));
if (session != null) {
let headers = new HttpHeaders().set('Content-Type', 'application/json');
headers = headers.set('x-session', session);
return this.http.get(this.users_url, { headers: headers });
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
394 次 |
| 最近记录: |