Angular Guard循环和Node.js发送后无法设置标头

时间:2018-12-20 14:23:55

标签: node.js angular

我正在面对Nodejs和Angular Guard的怪异问题,所以让我们解释一下这个问题, 首先,这是一个带有后端代码的登录表单

(req,res)=>{

  //defien properties and validation
  let email    = req.body.email,
      password = req.body.password;

  //call function form auth class
  auth.signinByEmailAndPassword(email,password)
      .then(credential=>{
        return res.status(200);
      })
      .catch(error=>{
        return res.status(400).json(error);
      })

}

它工作正常并返回200,但是我在角度保护检查此问题是否在记录时出现问题,因此这是用于检查用户的后端代码在记录

   router.get('/issigning', (req,res)=>{
      auth.getCurrentUser((user)=>{
        return res.status(200).json(user);
      })
    })

此函数返回true或null

这是防护“ IsUserGuard”代码

return this.auth.isAuthorized().pipe(
  map( isAuthorized => {

    console.log(isAuthorized)

        if(!isAuthorized){
          this.router.navigate(['/auth']);
          return false;
        }

        return true;
  })
)

这是我的路线配置

const routes: Routes = [
    {
        path: '',
        component: PagesComponent,
        canActivate:[IsUserGuard,IsEmailVerifiedGuard],
        children: [
            {
                path: '',
                component: InnerComponent
            },
            {
                path: 'profile',
                component: ProfileComponent,
                children:[
                    {path :'' ,         redirectTo:'details'},
                    {path :'details',   component:DetailsComponent},
                    {path :'avatar' ,   component:AvatarComponent},
                    {path :'username',  component:UsernameComponent},
                    {path :'email',     component:EmailComponent},
                    {path :'mobile',    component:MobileComponent},
                    {path :'social',    component:SocialComponent},
                    {path :'settings',  component:SettingsComponent}
                ]
            },
            {
                path: 'inner',
                component: InnerComponent
            },
        ]
    },
    {
        path: 'auth',
        canActivate: [IsGuestGuard],
        component : AuthComponent,
        children:[
            {path :'', redirectTo: 'login'},
            {path :'login', component: LoginComponent},
            {path :'register', component: RegisterComponent},
            {path :'foregot-password', component: ForgotPasswordComponent},
        ]

    },

    {
        path: '404',
        component: ErrorPageComponent
    },
    {
        path: 'error/:type',
        component: ErrorPageComponent
    },
]; 

因此,当我单击“登录按钮”时发生了什么,这是登录,他检查用户是否已登录!所以这个问题出现在nodejs

  

错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后,无法设置标头       在ServerResponse.setHeader(_http_outgoing.js:470:11)       在ServerResponse.header(d:\ Nodejs Workarea \ MyProjects \ default-skeleton \ server \ node_modules \ express \ lib \ response.js:767:10)       在ServerResponse.send(d:\ Nodejs Workarea \ MyProjects \ default-skeleton \ server \ node_modules \ express \ lib \ response.js:170:12)       在ServerResponse.json(d:\ Nodejs Workarea \ MyProjects \ default-skeleton \ server \ node_modules \ express \ lib \ response.js:267:15)       在Object.auth.getCurrentUser [下一个](d:\ Nodejs Workarea \ MyProjects \ default-skeleton \ server \ routes \ auth.js:16:28)       在d:\ Nodejs Workarea \ MyProjects \ default-skeleton \ node_modules @ firebase \ util \ dist \ index.node.cjs.js:1349:22       在d:\ Nodejs Workarea \ MyProjects \ default-skeleton \ node_modules @ firebase \ util \ dist \ index.node.cjs.js:1453:21       在process._tickCallback(internal / process / next_tick.js:68:7)

并且在角度上很冻结,我认为警卫队做出了循环请求

更新 这是函数“ getCurrentUser”的代码

/**
 * For get currently Sign-In User
 */
exports.getCurrentUser = (callback)=>{
    return firebase.auth().onAuthStateChanged(callback);
}

这是函数“ isAuthorized”的代码

    public isAuthorized(){
    return this.http.get(environment.api+'auth/issigning').pipe(map(data=>data))
}

0 个答案:

没有答案