Angular订阅登录服务并从登录用户获取特殊字段

时间:2018-03-27 11:33:21

标签: angular angular-observable

我在用户模型中有布尔字段,我需要在用户按下提交后立即在登录页面上打开一个模态窗口,并在我检查user.user_on_test后从服务器获取信息字段和开放模态只有在它是真的时候。你能帮我从回复中提取这些信息吗?

这是我的user.service

@Injectable()
export class UserService extends BaseService {
  protected API_PATH: string;

  constructor( protected _http: HttpClient ) {
   super( _http );
   this.API_PATH += '/rest-auth';
   this.getUserData().subscribe();
  }

  private _user: BehaviorSubject<User> = new BehaviorSubject( null );

  get user(): Observable<User> {
    return this._user.asObservable();
  }

  set user( user ) {
   if ( <User>user ) {
    this._user.next( new User( user ) );
    localStorage.setItem( 'active', ( <User>user)
      .is_active.toString() );
    localStorage.setItem( 'superuser', ( <User>user)
      .is_superuser.toString() );
    localStorage.setItem( 'user_on_test', ( <User>user)
      .user_on_test.toString() );
    } else {
    this._user.next( null );
      localStorage.removeItem( 'active' );
      localStorage.removeItem( 'superuser' );
      localStorage.removeItem( 'user_on_test');
     }
   }

 public login( user: User ): Observable<any> {
  return Observable.concat(
   this.request( {
    'method': 'POST',
    'url': '/login/',
    'body': {
      username: user.username,
      password: user.password
    }
   } ),
   this.getUserData()
  );
 }

和login.component:

login() {
  this.errorMsg = '';
  this._authService.login( this.user )
    .takeUntil( this.destroy$).subscribe(
    (response) => {
    if (this._authService.user) {
    // here I need to check user.user_on_test
      alert('this user is on test server!');
    }
    if ( this.redirect ) {
      const url = this._route.snapshot.queryParams['returnUrl'] ||'/';
      this._router.navigate( [ url ] );
    }
  },
  ( rejection ) => {
    if ( rejection && rejection.hasOwnProperty( 'error' ) && 
   rejection.error.non_field_errors ) {
      for (const error in rejection.error.non_field_errors) {
        if (rejection.error.non_field_errors.hasOwnProperty(error)) {
          this.errorMsg = rejection.error.non_field_errors[error];
        }
      }
    }
  });
  }

2 个答案:

答案 0 :(得分:1)

由于#!/bin/bash # FIRST_ARG - full class name (with package) # SECOND_ARG - class path CLASS_NAME=`javap -cp $2 $1 | \ grep -v "Compiled from" | \ grep "public class" | \ cut -f3 -d" " | \ awk -F"." '{ print $NF }'` PACKAGE_NAME=`javap -cp $2 $1 |i \ grep -v "Compiled from" | \ grep "public class" | \ cut -f3 -d" " | \ sed s/\.${CLASS_NAME}$//` DIR_NAME=`echo $PACKAGE_NAME | sed 's|\.|/|g'` mkdir -p java_jni/${DIR_NAME} JAVA_FILE_NAME="java_jni/${DIR_NAME}/${CLASS_NAME}.java" echo "package ${PACKAGE_NAME};" > ${JAVA_FILE_NAME} echo "public class ${CLASS_NAME} {" >> ${JAVA_FILE_NAME} javap -cp $2 $1 | grep "native" >> ${JAVA_FILE_NAME} echo "}" >> ${JAVA_FILE_NAME} mkdir -p c_header javac -h c_header ${JAVA_FILE_NAME} 的返回类型为public login(user: User),因此很难说。

假设API调用返回Observable<any>,这将使方法返回类型User,代码应如下所示:

Observable<User>

答案 1 :(得分:0)

很难说没有“getUserData”代码,也不是登录请求的响应示例。

我现在的猜测是你可以这样做:

const user = this._authService.user;

if (user && user.user_on_test) {
  alert('this user is on test server!');
}

希望有所帮助