代码之家  ›  专栏  ›  技术社区  ›  StuartDTO

使用Angular 6 get请求登录

  •  0
  • StuartDTO  · 技术社区  · 7 年前

    我试图登录到我的webservice我的注册器工作良好,但当我尝试登录时,它会随机返回不同的号码,如417415。

    这是我的帖子

    login(userToLogin : UserToLogin) {
            return this.http.post<any>(`${environment.apiUrl}/api/auth/login`, userToLogin)
                .pipe(map(userToLogin => {
                    // login successful if there's a jwt token in the response
                    if (userToLogin && userToLogin.token) {
                        // store user details and jwt token in local storage to keep user logged in between page refreshes
                        localStorage.setItem('currentUser', JSON.stringify(userToLogin));
                    }
    
                    return userToLogin;
                }));
        }
    

    问题是,如果可以,这个响应应该返回:

    export class LoginToken{
        accesToken:string;
        refreshToken:string;
        tokenType:string;
        expiryDuration:number;
    }
    

    UserToLogin是我的登录所需的requestBody

    export class UserToLogin {
        email: string;
        password: string;
        deviceInfo : DeviceInfo;
        constructor(email: string, password: string, deviceInfo: DeviceInfo){
            this.email = email;
            this.password = password;
            this.deviceInfo = deviceInfo;
        }
    }
    
    export class DeviceInfo {
        deviceId: string;
        deviceType: string;
        notificationToken: string;
    }
    

    然后我应该把这个保存在本地存储中,但是我想知道我是否需要把用户的电子邮件和这个保存为一个地图或者其他什么东西,你能指导我吗?

    我的问题是登录不成功,我不知道我错过了什么。

    编辑

    岗位 http://localhost:9004/api/auth/login 401号

    同时得到消息:

    http://localhost:9004/api/auth/login

    我显示的消息是“用户被禁用”,但我不知道如何检测这个错误,好吧,它转到错误方法,但它正在刷新页面,消息消失,数据也消失。

    这是我的 Routes

    const routes: Routes = [
    { path: "", component: HomeComponent, canActivate: [AuthGuard] },
    { path: "login", component: LoginComponent },
    { path: "register", component: RegisterComponent },
    
    //otherwise redirect to home
    { path: "**", redirectTo: "" }
    ];
    

    我的登录组件是:

    export class LoginComponent implements OnInit {
      loginForm: FormGroup;
      loading = false;
      submitted = false;
      returnUrl: string;
      usertologin: UserToLogin;
    
      constructor(
        private formBuilder: FormBuilder,
        private route: ActivatedRoute,
        private router: Router,
        private authenticationService: AuthenticationService,
        private alertService: AlertService
      ) {}
    
      ngOnInit() {
        this.loginForm = this.formBuilder.group({
          username: ["", Validators.required],
          password: ["", Validators.required]
        });
    
        // reset login status
        //this.authenticationService.logout();
    
        // get return url from route parameters or default to '/'
        this.returnUrl = this.route.snapshot.queryParams["returnUrl"] || "/";
      }
      // convenience getter for easy access to form fields
      get f() {
        return this.loginForm.controls;
      }
    
      onSubmit() {
        this.submitted = true;
    
        // stop here if form is invalid
        if (this.loginForm.invalid) {
          return;
        }
        this.usertologin = new UserToLogin(
          this.f.username.value,
          this.f.password.value,
          this.generateRandomDeviceInfo()
        );
    
        this.loading = true;
        this.authenticationService
          .login(this.usertologin)
          .pipe(first())
          .subscribe(
            data => {
              console.log("RETURN IS " + JSON.stringify(data));
              this.router.navigate([this.returnUrl]);
            },
            error => {
              console.log(error);
              this.alertService.error(error);
              this.loading = false;
            }
          );
      }
    
      generateRandomDeviceInfo() {
        var deviceInfo = new DeviceInfo();
        deviceInfo.deviceId = "123";
        deviceInfo.deviceType = "DEVICE_TYPE_ANDROID";
        deviceInfo.notificationToken = "123";
        return deviceInfo;
      }
    
    I can not stay in login without redirect to any page, I mean if it's error it should stay in the login page but it's refreshing even if it's error.
    
    0 回复  |  直到 7 年前
    推荐文章