21 lines
715 B
TypeScript
21 lines
715 B
TypeScript
import type { AuthLoginMutation } from "#build/graphql/typed-documents";
|
|
import { defineRemoteExecMiddleware } from "@lewebsimple/nuxt-graphql/helpers";
|
|
|
|
export default defineRemoteExecMiddleware({
|
|
onRequest({ context, fetchOptions }) {
|
|
// Attach auth token from context to request headers
|
|
if (context.authToken) {
|
|
fetchOptions.headers.set("Authorization", `Bearer ${context.authToken}`);
|
|
}
|
|
},
|
|
async onResponse({ operationName, response, context }) {
|
|
// Save auth token in user session
|
|
if (operationName === "AuthLogin") {
|
|
const { data } = await response.json() as { data?: AuthLoginMutation };
|
|
if (data) {
|
|
await context.handleLogin(data);
|
|
}
|
|
}
|
|
},
|
|
});
|