角度认证服务作为单身人士

时间:2017-10-08 17:09:19

标签: angular

我尝试按照这个答案让我的Auth服务成为单身人士。 Angular Service is reset on each call of it

当用户登录时,我在我的AuthService中设置了一个isLoggedIn变量,希望保持这种状态。但是,当我导航到限制路线,如/ main。当用户登录时,该变量在我的控制台中返回false。

我在这里做错了什么?

auth.service.ts

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { Router } from '@angular/router';
import { AuthService } from '../services/auth.service';

@Injectable()
export class AuthGuard implements CanActivate {


  constructor(private authService: AuthService, private router: Router) { }


  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {


    console.log(this.authService.isAuthenticated());
    if (this.authService.isAuthenticated()) {
      return true;
    }


    this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
    return false

  }
}

auth.guard.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {HttpClientModule} from '@angular/common/http';

import { AppComponent } from './app.component';

import {BrowserAnimationsModule} from '@angular/platform-browser/animations';

import {FormsModule, ReactiveFormsModule} from '@angular/forms';


import {AppRoutingModule} from './app-routing.module';

import { HeaderComponent } from './components/header/header.component';
import { FooterComponent } from './components/footer/footer.component'
import { RegisterComponent } from './components/register/register.component';
import { LoginComponent } from './components/login/login.component';
import { MainComponent } from './components/main/main.component';


import {ApiService} from './services/api.service';
import {JwtService} from './services/jwt.service';
import {UserService} from './services/user.service';
import {SharedModule} from './shared/shared.module';



@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    FooterComponent,
    RegisterComponent,
    LoginComponent,

  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    BrowserAnimationsModule,
    FormsModule,
    ReactiveFormsModule,
    AppRoutingModule,
    SharedModule.forRoot(),
  ],
  providers: [
    ApiService,
    JwtService,
    UserService,
  ],
  bootstrap: [AppComponent]
})

export class AppModule { }

app.module.ts

import {ModuleWithProviders, NgModule} from '@angular/core';
import {MaterialModule} from './material.module';
import { FlexLayoutModule } from "@angular/flex-layout";

import {AuthGuard} from '../guards/auth.guard';
import { AuthService } from '../services/auth.service';

@NgModule({
  imports: [
    MaterialModule,
    FlexLayoutModule,

  ],
  exports: [
    MaterialModule,
    FlexLayoutModule,    
  ]
})

export class SharedModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: SharedModule,
      providers: [
        AuthService,
        AuthGuard,
      ]
    };
  }
}

shared.module.ts

import { NgModule }       from '@angular/core';
import { CommonModule }   from '@angular/common';

import {MainComponent} from './main.component'
import {ChatComponent} from '../chat/chat.component';
import {MomentsComponent} from '../moments/moments.component';
import {SettingsComponent} from '../settings/settings.component';
import {QuestionsComponent} from '../questions/questions.component';
import { SearchComponent } from '../search/search.component';
import { FormsModule } from '@angular/forms';

import {SharedModule} from '../../shared/shared.module';


import { MainRoutingModule }       from './main-routing.module';

@NgModule({
  imports: [
    CommonModule,
    MainRoutingModule,
    SharedModule,
    FormsModule,

  ],
  declarations: [
    MainComponent,
    ChatComponent,
    MomentsComponent,
    SearchComponent,
    SettingsComponent, 
    QuestionsComponent,
  ]
})
export class MainModule {}

main.module.ts(受auth.guard限制,仅限登录用户)

{{1}}

1 个答案:

答案 0 :(得分:1)

由于刷新页面,即使角度服务是单例,页面刷新时的值也会丢失。

或者,您可以在不刷新页面的情况下更改路径。或者,您可以将 LoginStatus 存储在 localstorage 变量中,如果您确实希望页面刷新,则可以获取其他页面上的信息

this.isLoggedIn = true;
localStorage.setItem('isLoggedIn', 'true');