2022-12-02 10:55:56 +00:00
|
|
|
import NextAuth from "next-auth";
|
|
|
|
|
import Google from "next-auth/providers/google";
|
2023-08-25 07:11:33 +00:00
|
|
|
import Credentials from "next-auth/providers/credentials";
|
|
|
|
|
import Apple from "next-auth/providers/apple";
|
|
|
|
|
|
|
|
|
|
const headers = { Authorization: `Token ${process.env.ZAMMAD_API_TOKEN}` };
|
|
|
|
|
|
|
|
|
|
const fetchRoles = async () => {
|
|
|
|
|
const url = `${process.env.ZAMMAD_URL}/api/v1/roles`;
|
|
|
|
|
const res = await fetch(url, { headers });
|
|
|
|
|
const roles = await res.json();
|
|
|
|
|
const formattedRoles = roles.reduce((acc: any, role: any) => {
|
|
|
|
|
acc[role.id] = role.name;
|
|
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
return formattedRoles;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fetchUser = async (email: string) => {
|
|
|
|
|
const url = `${process.env.ZAMMAD_URL}/api/v1/users/search?query=email:${email}&limit=1`;
|
2023-09-06 16:42:52 +02:00
|
|
|
console.log({ url });
|
2023-08-25 07:11:33 +00:00
|
|
|
const res = await fetch(url, { headers });
|
|
|
|
|
const users = await res.json();
|
|
|
|
|
const user = users?.[0];
|
|
|
|
|
return user;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getUserRoles = async (email: string) => {
|
2023-08-28 14:36:34 +02:00
|
|
|
try {
|
|
|
|
|
const user = await fetchUser(email);
|
|
|
|
|
const allRoles = await fetchRoles();
|
|
|
|
|
const roles = user.role_ids.map((roleID: number) => {
|
|
|
|
|
const role = allRoles[roleID];
|
|
|
|
|
return role ? role.toLowerCase().replace(" ", "_") : null;
|
|
|
|
|
});
|
|
|
|
|
return roles.filter((role: string) => role !== null);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log({ e });
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2023-08-25 07:11:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const login = async (email: string, password: string) => {
|
|
|
|
|
const url = `${process.env.ZAMMAD_URL}/api/v1/users/me`;
|
2023-09-06 16:42:52 +02:00
|
|
|
console.log({ url });
|
2023-08-25 07:11:33 +00:00
|
|
|
const authorization = 'Basic ' + Buffer.from(email + ":" + password).toString('base64');
|
|
|
|
|
const res = await fetch(url, {
|
|
|
|
|
headers: {
|
|
|
|
|
authorization
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const user = await res.json();
|
|
|
|
|
console.log({ user });
|
|
|
|
|
|
|
|
|
|
if (user && !user.error && user.id) {
|
|
|
|
|
return user;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-12-02 10:55:56 +00:00
|
|
|
|
2023-06-26 10:07:12 +00:00
|
|
|
const handler = NextAuth({
|
2023-08-25 07:11:33 +00:00
|
|
|
pages: {
|
|
|
|
|
signIn: "/login",
|
|
|
|
|
error: "/login",
|
|
|
|
|
signOut: "/logout",
|
|
|
|
|
},
|
2022-12-02 10:55:56 +00:00
|
|
|
providers: [
|
|
|
|
|
Google({
|
|
|
|
|
clientId: process.env.GOOGLE_CLIENT_ID,
|
|
|
|
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
|
|
|
|
|
}),
|
|
|
|
|
Apple({
|
|
|
|
|
clientId: process.env.APPLE_CLIENT_ID,
|
|
|
|
|
clientSecret: process.env.APPLE_CLIENT_SECRET
|
|
|
|
|
}),
|
2023-08-25 07:11:33 +00:00
|
|
|
Credentials({
|
|
|
|
|
name: "Zammad",
|
|
|
|
|
credentials: {
|
|
|
|
|
email: { label: "Email", type: "text", },
|
|
|
|
|
password: { label: "Password", type: "password" }
|
|
|
|
|
},
|
|
|
|
|
async authorize(credentials, req) {
|
|
|
|
|
const user = await login(credentials.email, credentials.password);
|
|
|
|
|
if (user) {
|
|
|
|
|
return user;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
2022-12-02 10:55:56 +00:00
|
|
|
],
|
|
|
|
|
secret: process.env.NEXTAUTH_SECRET,
|
2023-08-25 07:11:33 +00:00
|
|
|
|
|
|
|
|
callbacks: {
|
|
|
|
|
signIn: async ({ user, account, profile }) => {
|
2023-09-06 16:42:52 +02:00
|
|
|
console.log("SIGN IN");
|
2023-08-28 14:36:34 +02:00
|
|
|
const roles = await getUserRoles(user.email) ?? [];
|
|
|
|
|
return roles.includes("admin") || roles.includes("agent") || process.env.SETUP_MODE === "true";
|
2023-08-25 07:11:33 +00:00
|
|
|
},
|
|
|
|
|
session: async ({ session, user, token }) => {
|
|
|
|
|
// @ts-ignore
|
2023-08-28 14:36:34 +02:00
|
|
|
session.user.roles = token.roles ?? [];
|
2023-08-25 07:11:33 +00:00
|
|
|
// @ts-ignore
|
2023-08-28 14:36:34 +02:00
|
|
|
session.user.leafcutter = token.leafcutter; // remove
|
2023-08-25 07:11:33 +00:00
|
|
|
return session;
|
|
|
|
|
},
|
|
|
|
|
jwt: async ({ token, user, account, profile, trigger }) => {
|
|
|
|
|
if (user) {
|
2023-08-28 14:36:34 +02:00
|
|
|
token.roles = await getUserRoles(user.email) ?? [];
|
2023-08-25 07:11:33 +00:00
|
|
|
}
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2022-12-02 10:55:56 +00:00
|
|
|
});
|
2023-06-26 10:07:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
export { handler as GET, handler as POST };
|