feat: Initial authentication logic and UX
This commit is contained in:
20
wp-content/themes/moonshine/server/graphql/wp-middleware.ts
Normal file
20
wp-content/themes/moonshine/server/graphql/wp-middleware.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user