错误InvalidTokenError:指定了无效的令牌

时间:2019-06-12 07:57:19

标签: angular

我正在使用Angular 7,并且我有一个表单字段,不需要任何身份验证,这意味着即使非用户也可以填写表单并提交。但是现在我注销并填写表格时按提交按钮时出现错误

:version
VIM - Vi IMproved 8.1 (2018 May 18, compiled Feb 19 2019 12:07:03)
macOS version
Included patches: 1-950
Compiled by Homebrew
Huge version with MacVim GUI.  Features included (+) or not (-):
+acl               +cindent           +cursorshape       +file_in_path      +job               +menu              +mouse_urxvt       +persistent_undo   +signs             +terminal          +vertsplit         -xfontset
+arabic            +clientserver      +dialog_con_gui    +find_in_path      +jumplist          +mksession         +mouse_xterm       +postscript        +smartindent       +terminfo          +virtualedit       +xim
+autocmd           +clipboard         +diff              +float             +keymap            +modify_fname      +multi_byte        +printer           +startuptime       +termresponse      +visual            -xpm
+autochdir         +cmdline_compl     +digraphs          +folding           +lambda            +mouse             +multi_lang        +profile           +statusline        +textobjects       +visualextra       -xsmp
-autoservername    +cmdline_hist      +dnd               -footer            +langmap           +mouseshape        -mzscheme          -python            -sun_workshop      +textprop          +viminfo           -xterm_clipboard
+balloon_eval      +cmdline_info      -ebcdic            +fork()            +libcall           +mouse_dec         +netbeans_intg     +python3           +syntax            +timers            +vreplace          -xterm_save
+balloon_eval_term +comments          +emacs_tags        +fullscreen        +linebreak         -mouse_gpm         +num64             +quickfix          +tag_binary        +title             +wildignore
+browse            +conceal           +eval              -gettext           +lispindent        -mouse_jsbterm     +odbeditor         +reltime           +tag_old_static    +toolbar           +wildmenu
++builtin_terms    +cryptv            +ex_extra          -hangul_input      +listcmds          +mouse_netterm     +packages          +rightleft         -tag_any_white     +transparency      +windows
+byte_offset       +cscope            +extra_search      +iconv             +localmap          +mouse_sgr         +path_extra        +ruby              -tcl               +user_commands     +writebackup
+channel           +cursorbind        -farsi             +insert_expand     +lua               -mouse_sysmouse    +perl              +scrollbind        +termguicolors     +vartabs           -X11
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/Applications/MacVim.app/Contents/Resources/vim"
Compilation: clang -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_MACVIM -Wall -Wno-unknown-pragmas -pipe  -DMACOS_X -DMACOS_X_DARWIN  -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: clang   -L. -L.  -L/usr/local/lib -o Vim -framework Cocoa -framework Carbon       -lm  -lncurses -liconv -framework AppKit  -L/usr/local/opt/lua/lib -llua5.3 -fstack-protector  -L/System/Library/Perl/5.18/darwin-thread-multi-2lev
el/CORE -lperl  -L/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin -lpython3.7m -framework CoreFoundation  -framework Ruby

我已从该特定路由中删除了AuthGuard,但抛出此错误。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

拦截器是造成问题的一种。因为它会拦截所有http调用,并在其后注入令牌。

intercept( req: HttpRequest<any>, next: HttpHandler ): Observable<HttpEvent<any>> { 
   const authToken = this.authService.getToken(); 
   const authRequest = req.clone({ 
      headers: req.headers.set("Authorization", "Bearer " + authToken) 
   }); 

   return next.handle(authRequest); 
}

如您所见。它从服务获取令牌,然后将承载令牌注入标头中,然后再添加。

发生这种情况是因为您试图获取注销时不存在的令牌(在此行中):

const authToken = this.authService.getToken(); 

解决方案?将if添加到以下类型的语句中:

const authRequest = authToken ? req.clone({ ..what you got }) : req;

如果这不起作用,我们将开始调试您的服务(authService),了解getToken的作用。否则,这应该起作用。