graphql - Apollo resetStore is not working in Angular client -
i trying integrate authorization token @ client side. passing token in middleware. when user logout reset store , new token. when send new request still sending old token(cached)
here code app.module.ts
const networkinterface = createnetworkinterface({ uri: "http://localhost:3000/graphql" }); networkinterface.use([ { applymiddleware(req, next) { if (!req.options.headers) { req.options.headers = {}; // create header object if needed. } req.options.headers.authorization = localstorage.getitem(auth_token); next(); } } ]); export function provideclient(): apolloclient { return new apolloclient({ networkinterface, dataidfromobject: (o: any) => `${o.__typename}-${o.id},` }); }
when logout have code
localstorage.removeitem(auth_token); this._apollo.getclient().resetstore();
then when make request still taking old token in request headers.
how can update new token?
i might change middleware this:
networkinterface.use([{ applymiddleware(req, next) { if (!req.options.headers) req.options.headers = {} const token = localstorage.getitem('token') req.options.headers.authorization = token ? token : null next() } }])
notice ternary operator in there, token ? token : null
. ensure auth header cannot sent unless app knows of token. if client still sending it, token not being removed properly.
you try quick test also: after logging out, press f12 , type browser console: localstorage
, see if token still in there.
i can't tell code, looks have variable called auth_token returns string of 'token' or whatever key use. want explicitly mention removing token key, not value.
add token: localstorage.setitem('token', 'e47nes45nysde5nue5nu')
remove token: localstorage.removeitem('token')
if code this:
const auth_token = 'e47nes45nysde5nue5nu' localstorage.removeitem(auth_token)
then, why still making queries auth token. don't think generates errors in console if doesn't find matching key remove localstorage.
Comments
Post a Comment