generated from pascalmartineau/wp-skeleton
refactor: use onServerResponse for auth instead of server api
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
import { defineEventHandler, readBody } from "h3";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { email, password } = await readBody(event);
|
||||
try {
|
||||
const response = await useGraphqlMutation("login", { email, password });
|
||||
if (response.errors.length) {
|
||||
throw new Error(response.errors[0]?.message);
|
||||
}
|
||||
if (!response.data.login) {
|
||||
throw new Error("Login failed: Invalid credentials");
|
||||
}
|
||||
const { authToken, refreshToken, user } = response.data.login;
|
||||
await setUserSession(event, {
|
||||
user,
|
||||
secure: { authToken, refreshToken },
|
||||
loggedInAt: new Date().toISOString(),
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
catch (error) {
|
||||
const messages: Record<string, string> = {
|
||||
invalid_email: "Courriel et/ou mot de passe invalide(s).",
|
||||
incorrect_password: "Courriel et/ou mot de passe invalide(s).",
|
||||
};
|
||||
const message = error instanceof Error && messages[error.message] ? messages[error.message] : "Une erreur est survenue.";
|
||||
return { success: false, message };
|
||||
}
|
||||
});
|
||||
@@ -2,15 +2,12 @@ import { defineEventHandler } from "h3";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const response = await useGraphqlMutation("userSwitchBack");
|
||||
if (response.errors?.length) {
|
||||
throw new Error(response.errors[0]?.message);
|
||||
}
|
||||
// TODO: Switch back to the previous user.
|
||||
await clearUserSession(event);
|
||||
return { success: true };
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Échec du retour à l'utilisateur précédent";
|
||||
const message = error instanceof Error ? error.message : "Une erreur est survenue.";
|
||||
return { success: false, message };
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
import { defineEventHandler, readBody } from "h3";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { userId } = await readBody(event);
|
||||
try {
|
||||
const currentSession = await getUserSession(event);
|
||||
if (!currentSession?.user) {
|
||||
throw new Error("Authentication requise");
|
||||
}
|
||||
const response = await useGraphqlMutation("userSwitchTo", { userId });
|
||||
if (response.errors?.length) {
|
||||
throw new Error(response.errors[0]?.message);
|
||||
}
|
||||
if (!response.data.userSwitchTo) {
|
||||
throw new Error("Le changement d'utilisateur a échoué");
|
||||
}
|
||||
const { authToken, refreshToken, user } = response.data.userSwitchTo;
|
||||
await setUserSession(event, {
|
||||
user,
|
||||
secure: { authToken, refreshToken },
|
||||
loggedInAt: new Date().toISOString(),
|
||||
switchedBy: currentSession.user.id,
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Le changement d'utilisateur a échoué";
|
||||
return { success: false, message };
|
||||
}
|
||||
});
|
||||
@@ -1,10 +0,0 @@
|
||||
mutation login($email: String!, $password: String!) {
|
||||
login(input: { username: $email, password: $password }) {
|
||||
authToken
|
||||
refreshToken
|
||||
user {
|
||||
id
|
||||
email
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
mutation userSwitchBack {
|
||||
userSwitchBack(input: {}) {
|
||||
success
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
mutation userSwitchTo($userId: ID!) {
|
||||
userSwitchTo(input: { userId: $userId }) {
|
||||
authToken
|
||||
refreshToken
|
||||
user {
|
||||
id
|
||||
email
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { defineGraphqlServerOptions } from "nuxt-graphql-middleware/server-options";
|
||||
import { jwtDecode } from "jwt-decode";
|
||||
import type { LoginRootMutation, UserSwitchToRootMutation } from "#graphql-operations";
|
||||
|
||||
interface DecodedToken {
|
||||
exp: number;
|
||||
@@ -24,9 +25,51 @@ export default defineGraphqlServerOptions({
|
||||
const newToken = await refreshAuthToken(session.secure.refreshToken);
|
||||
if (newToken) {
|
||||
session.secure.authToken = newToken;
|
||||
await setUserSession(event, session);
|
||||
}
|
||||
}
|
||||
|
||||
return { headers: { ...headers, Authorization: `Bearer ${session.secure.authToken}` } };
|
||||
},
|
||||
|
||||
onServerResponse(event, response, _operation, operationName) {
|
||||
// Handle login mutation
|
||||
if (operationName === "login") {
|
||||
const loginData = response._data as LoginRootMutation;
|
||||
if (loginData?.login) {
|
||||
const { authToken, refreshToken, user } = loginData.login;
|
||||
setUserSession(event, {
|
||||
user: {
|
||||
id: user?.id,
|
||||
email: user?.email,
|
||||
},
|
||||
secure: {
|
||||
authToken,
|
||||
refreshToken,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Handle user switch mutations
|
||||
if (operationName === "userSwitchTo") {
|
||||
const switchData = response._data as UserSwitchToRootMutation;
|
||||
if (switchData?.userSwitchTo?.authToken) {
|
||||
const { authToken, refreshToken, user } = switchData.userSwitchTo;
|
||||
setUserSession(event, {
|
||||
user: {
|
||||
id: user?.id,
|
||||
email: user?.email,
|
||||
},
|
||||
secure: {
|
||||
authToken,
|
||||
refreshToken,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Return the original response data
|
||||
return response._data!;
|
||||
},
|
||||
});
|
||||
|
||||
@@ -17583,12 +17583,6 @@ type RootMutation {
|
||||
input: UpdateUserInput!
|
||||
): UpdateUserPayload
|
||||
|
||||
"""The userSwitchBack mutation"""
|
||||
userSwitchBack(
|
||||
"""Input for the userSwitchBack mutation"""
|
||||
input: UserSwitchBackInput!
|
||||
): UserSwitchBackPayload
|
||||
|
||||
"""The userSwitchTo mutation"""
|
||||
userSwitchTo(
|
||||
"""Input for the userSwitchTo mutation"""
|
||||
@@ -24953,25 +24947,6 @@ enum UserRoleEnum {
|
||||
TRANSLATOR
|
||||
}
|
||||
|
||||
"""Input for the userSwitchBack mutation."""
|
||||
input UserSwitchBackInput {
|
||||
"""
|
||||
This is an ID that can be passed to a mutation by the client to track the progress of mutations and catch possible duplicate mutation submissions.
|
||||
"""
|
||||
clientMutationId: String
|
||||
}
|
||||
|
||||
"""The payload for the userSwitchBack mutation."""
|
||||
type UserSwitchBackPayload {
|
||||
"""
|
||||
If a 'clientMutationId' input is provided to the mutation, it will be returned as output on the mutation. This ID can be used by the client to track the progress of mutations and catch possible duplicate mutation submissions.
|
||||
"""
|
||||
clientMutationId: String
|
||||
|
||||
"""Whether switching back was successful"""
|
||||
success: Boolean
|
||||
}
|
||||
|
||||
"""Input for the userSwitchTo mutation."""
|
||||
input UserSwitchToInput {
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user