本文介绍了Angular auth.guard没有持久身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
刷新页面后,身份验证似乎无法持久.每当我登录时,它都会成功重定向我.但是,当我刷新时,再也无法访问了.
Authentication doesn't seem to persist after page has been refreshed. Whenever I login, it successfully redirects me. but, when i refreshed, accessing is not possible anymore.
这是身份验证卫士
路由器
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NavComponent } from './components/nav/nav.component';
import { HomeComponent } from './components/home/home.component';
import { GalleryComponent } from './components/gallery/gallery.component';
import { FooterComponent } from './components/footer/footer.component';
import { AngularFireModule } from 'angularfire2';
import { AngularFireAuthModule } from 'angularfire2/auth';
import { AngularFireDatabaseModule, AngularFireDatabase } from 'angularfire2/database';
import { FIREBASECONFIG as firebaseConfig } from './config/firebase-config';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ScrollToModule } from 'ng2-scroll-to';
import { NgxCarouselModule } from 'ngx-carousel';
import { AnimateOnScrollModule } from 'ng2-animate-on-scroll';
import { AuthService } from './services/auth.service';
import { AuthGuard } from './services/auth.guard.service';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'gallery', component: GalleryComponent, canActivate: [AuthGuard] }
];
@NgModule({
declarations: [
AppComponent,
NavComponent,
HomeComponent,
GalleryComponent,
FooterComponent,
],
imports: [
FormsModule,
BrowserModule,
AppRoutingModule,
NgxCarouselModule,
NgbModule.forRoot(),
ScrollToModule.forRoot(),
RouterModule.forRoot(routes),
AnimateOnScrollModule.forRoot(),
HttpClientModule,
AngularFireModule.initializeApp(firebaseConfig),
AngularFireAuthModule,
AngularFireDatabaseModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
exports: [
TranslateModule
],
providers: [AngularFireDatabase, AuthService, AuthGuard],
bootstrap: [AppComponent]
})
auth.guard.ts
import { Observable } from 'rxjs/Observable';
import { AuthService } from './auth.service';
import { AngularFireAuth } from 'angularfire2/auth';
import { Injectable, OnInit } from '@angular/core';
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivate {
public user = null;
constructor(private fireauth: AngularFireAuth, private router: Router) {
this.fireauth.auth.onAuthStateChanged((user) => {
if (user) {
this.user = this.fireauth.auth.currentUser;
}
});
}
ongOnInit() {
}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): any {
if (this.user) {
return true;
} else {
this.router.navigate(['']);
return false;
}
}
}
根据我的说法,路线还可以,我想问题应该出在auth Guard上.
the routes are ok according to me, i thing that the problem most be on the auth guard, i guess.
推荐答案
尝试此代码
constructor(
private router:Router,
public fireauth:AngularFireAuth
){}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean> {
return this.fireauth.authState.map((auth) => {
if(auth == null) {
this.router.navigate(['/']);
return false;
} else {
return true;
}
});
}
我认为this.fireauth.auth.onAuthStateChanged
用于较旧的版本
这篇关于Angular auth.guard没有持久身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!