Update deps
This commit is contained in:
parent
6663e8ed1e
commit
486f989494
33 changed files with 776 additions and 695 deletions
|
|
@ -9,6 +9,7 @@ const fetchRoles = async () => {
|
|||
const url = `${process.env.ZAMMAD_URL}/api/v1/roles`;
|
||||
const res = await fetch(url, { headers });
|
||||
const roles = await res.json();
|
||||
console.log({ roles });
|
||||
const formattedRoles = roles.reduce((acc: any, role: any) => {
|
||||
acc[role.id] = role.name;
|
||||
return acc;
|
||||
|
|
@ -17,17 +18,25 @@ const fetchRoles = async () => {
|
|||
};
|
||||
|
||||
const fetchUser = async (email: string) => {
|
||||
const url = `${process.env.ZAMMAD_URL}/api/v1/users/search?query=email:${email}&limit=1`;
|
||||
console.log({ email });
|
||||
const url = `${process.env.ZAMMAD_URL}/api/v1/users/search?query=login:${email}&limit=1`;
|
||||
console.log({ url });
|
||||
const res = await fetch(url, { headers });
|
||||
console.log({ res });
|
||||
const users = await res.json();
|
||||
console.log({ users });
|
||||
const user = users?.[0];
|
||||
return user;
|
||||
|
||||
// temp
|
||||
return { role_ids: [1] };
|
||||
};
|
||||
|
||||
const getUserRoles = async (email: string) => {
|
||||
try {
|
||||
const user = await fetchUser(email);
|
||||
console.log({ user });
|
||||
const allRoles = await fetchRoles();
|
||||
console.log({ allRoles });
|
||||
const roles = user.role_ids.map((roleID: number) => {
|
||||
const role = allRoles[roleID];
|
||||
return role ? role.toLowerCase().replace(" ", "_") : null;
|
||||
|
|
@ -42,11 +51,12 @@ const getUserRoles = async (email: string) => {
|
|||
const login = async (email: string, password: string) => {
|
||||
const url = `${process.env.ZAMMAD_URL}/api/v1/users/me`;
|
||||
console.log({ url });
|
||||
const authorization = 'Basic ' + Buffer.from(email + ":" + password).toString('base64');
|
||||
const authorization =
|
||||
"Basic " + Buffer.from(email + ":" + password).toString("base64");
|
||||
const res = await fetch(url, {
|
||||
headers: {
|
||||
authorization
|
||||
}
|
||||
authorization,
|
||||
},
|
||||
});
|
||||
const user = await res.json();
|
||||
console.log({ user });
|
||||
|
|
@ -71,13 +81,13 @@ const handler = NextAuth({
|
|||
}),
|
||||
Apple({
|
||||
clientId: process.env.APPLE_CLIENT_ID,
|
||||
clientSecret: process.env.APPLE_CLIENT_SECRET
|
||||
clientSecret: process.env.APPLE_CLIENT_SECRET,
|
||||
}),
|
||||
Credentials({
|
||||
name: "Zammad",
|
||||
credentials: {
|
||||
email: { label: "Email", type: "text", },
|
||||
password: { label: "Password", type: "password" }
|
||||
email: { label: "Email", type: "text" },
|
||||
password: { label: "Password", type: "password" },
|
||||
},
|
||||
async authorize(credentials, req) {
|
||||
const user = await login(credentials.email, credentials.password);
|
||||
|
|
@ -86,15 +96,19 @@ const handler = NextAuth({
|
|||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
}),
|
||||
],
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
|
||||
callbacks: {
|
||||
signIn: async ({ user, account, profile }) => {
|
||||
const roles = await getUserRoles(user.email) ?? [];
|
||||
return roles.includes("admin") || roles.includes("agent") || process.env.SETUP_MODE === "true";
|
||||
const roles = (await getUserRoles(user.email)) ?? [];
|
||||
return (
|
||||
roles.includes("admin") ||
|
||||
roles.includes("agent") ||
|
||||
process.env.SETUP_MODE === "true"
|
||||
);
|
||||
},
|
||||
session: async ({ session, user, token }) => {
|
||||
// @ts-ignore
|
||||
|
|
@ -105,13 +119,11 @@ const handler = NextAuth({
|
|||
},
|
||||
jwt: async ({ token, user, account, profile, trigger }) => {
|
||||
if (user) {
|
||||
token.roles = await getUserRoles(user.email) ?? [];
|
||||
token.roles = (await getUserRoles(user.email)) ?? [];
|
||||
}
|
||||
return token;
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
|
||||
export { handler as GET, handler as POST };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue