Authguard刷新后将我重定向到登录名,这是authguard
package de.tu_chemnitz.dayliehelperprototype;
import.*
/**
* A BroadcastReceiver that notifies of important wifi p2p events.
*/
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager manager;
private Channel channel;
private MainActivity activity;
private static final String TAG = "WiFiDirectBroadcastReceiver";
/**
* @param manager WifiP2pManager system service
* @param channel Wifi p2p channel
* @param activity activity associated with the receiver
*/
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,MainActivity activity){
super();
this.manager = manager;
this.channel = channel;
this.activity = activity;
}
/*
* (non-Javadoc)
* @see android.content.BroadcastReceiver#onReceive(android.content.Context,
* android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// UI update to indicate wifi p2p status.
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED && Utility.getBool(activity, "switch_states", "notify_switchkey")) {
activity.startFindPeers();
}
else {
}
Log.d(TAG, "P2P state changed - " + state);
}
else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// request available peers from the wifi p2p manager. This is an
// asynchronous call and the calling activity is notified with a
// callback on PeerListListener.onPeersAvailable()
if (manager != null) {
manager.requestPeers(channel, activity.peerListListener);
}
Log.d(TAG, "P2P peers changed");
}
else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
}
else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
}
}
}
这是身份验证服务
constructor(private auth: AuthService,
private router: Router) {
}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (this.auth.isLoggedIn === true) {
return true;
} else {
this.router.navigate(['login']);
return false;
}
}
login.ts
private loggedInStatus = false;
setLoggedIn(value: boolean) {
this.loggedInStatus = value;
}
get isLoggedIn() {
return this.loggedInStatus;
}
app-component.ts
onLogin() {
this.auth.login(this.username, this.password).subscribe(data => {
this.auth.setLoggedIn(true);
localStorage.setItem('login', JSON.stringify(this.auth.isLoggedIn));
localStorage.setItem('data', JSON.stringify(data));
this.router.navigate(['']);
}, err => {
console.log(err);
});
}
}
我猜 constructor(private router: Router) {
const login = JSON.parse(localStorage.getItem('login'));
if (login !== undefined && login === true) {
if (location.pathname === '/login') {
this.router.navigate(['']);
this.showMenu = false;
} else {
this.showMenu = true;
}
} else {
if (location.pathname === '/login') {
this.showMenu = false;
} else {
this.showMenu = true;
}
}
}
每次刷新都为假。我试图从localStorage读取其值,但我猜这不是使用authguard的正确方法
答案 0 :(得分:1)
刷新浏览器时,AuthService
将销毁并重建,从而重新初始化其上的每个变量。
应用程序启动时(刷新时),您需要检查localStorage
以查看是否存在一些登录信息,并根据此信息在loggedInStatus
中设置AuthService
。我认为您可以在AuthService
构造函数上执行此操作。