我正在尝试实现简单的用户身份验证,目前我正在努力防止用户在页面刷新时注销。为此,我使用defer。首次打开应用程序时,请延迟返回AuthActions.Logout(),这是正确的,因为没有身份验证,问题在于刷新,因为尽管在chrome存储中设置了令牌,并且调度了所有操作,但效果不会再次调用。
@Effect()
init$ = defer((): Observable<AuthActions.SigninUser | AuthActions.Logout> => {
const token = this.authService.getToken();
console.log('in def')
return (token)
? of(new AuthActions.SigninUser())
: of(new AuthActions.Logout())
});
然后就到这里->
@Effect()
login$ = this.actions$
.pipe(
ofType(AuthActions.SIGNIN_USER),
switchMap(() => {
console.log('signin user')
return [new UserDetailsActions.GetUserDetails];
}),
)
如果设置了令牌(即令牌),则应将其触发,但仅当用户登录->
时才触发一次 @Effect()
getUserById = this.actions$
.pipe(
ofType(UserDetailsActions.GET_USER_DETAILS),
mergeMap(() => {
let token = this.authService.getTokenUsername();
return this.http.get(`${AppProperties.API_URL}user/${token}`)
}),
concatMap((userDetails: UserDetails) => {
sessionStorage.setItem('currentUserId', userDetails.id.toString());
return [new UserDetailsActions.SetUserDetails(userDetails)]
})
);
任何帮助将不胜感激