import type { NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { cookies } from "next/headers";
import { getAppcondaClient, getAppcondaClientSync } from "../getAppcondaClient";
import { getEnv } from "../lib/env";
import { getSDKForCurrentUser } from "../getSDKForCurrentUser";
import { Query } from "../query";
import { Account, AccountService, TUser } from "../modules";
/* const getEnv = () => {
return {
APPCONDA_ENDPOINT: 'process.env.APPCONDA_ENDPOINT',
APPCONDA_CLIENT_ENDPOINT: 'process.env.APPCONDA_CLIENT_ENDPOINT',
_SERVICE_TOKEN: 'process.env._SERVICE_TOKEN',
ENTERPRISE_LICENSE_KEY: 'process.env.ENTERPRISE_LICENSE_KEY',
};
}; */
const ENCRYPTION_KEY='51570cb4caa6c1f5f80e6e57a32efb397e9220434532697c94c8f821a7dd1951';
const ENTERPRISE_LICENSE_KEY = '';
const EMAIL_VERIFICATION_DISABLED = false;
export async function signIn({ userName, password }: { userName: string, password: string }) {
const adminClient = await getAppcondaClient();
const account = new Account(adminClient);
const session = await account.createEmailPasswordSession(userName, password);
const c = await cookies();
c.set('a_session', session.secret, {
path: "/",
httpOnly: true,
sameSite: "strict",
secure: true,
});
return session;
}
export const authOptions = (() => {
let options : any= null;
return () => {
if (options == null) {
options = {
providers: [
CredentialsProvider({
id: "credentials",
// The name to display on the sign in form (e.g. "Sign in with...")
name: "Credentials",
// The credentials is used to generate a suitable form on the sign in page.
// You can specify whatever fields you are expecting to be submitted.
// e.g. domain, username, password, 2FA token, etc.
// You can pass any HTML attribute to the tag through the object.
credentials: {
email: {
label: "Email Address",
type: "email",
placeholder: "Your email address",
},
password: {
label: "Password",
type: "password",
placeholder: "Your password",
},
totpCode: { label: "Two-factor Code", type: "input", placeholder: "Code from authenticator app" },
backupCode: { label: "Backup Code", type: "input", placeholder: "Two-factor backup code" },
},
async authorize(credentials, _req) {
const adminClient = getAppcondaClientSync();
const accountService = new AccountService(adminClient);
const session = await accountService.CredentialLogin(credentials as any);
return session;
},
}),
CredentialsProvider({
id: "token",
// The name to display on the sign in form (e.g. "Sign in with...")
name: "Token",
// The credentials is used to generate a suitable form on the sign in page.
// You can specify whatever fields you are expecting to be submitted.
// e.g. domain, username, password, 2FA token, etc.
// You can pass any HTML attribute to the tag through the object.
credentials: {
token: {
label: "Verification Token",
type: "string",
},
},
async authorize(credentials, _req) {
const adminClient = getAppcondaClientSync();
const accountService = new AccountService(adminClient);
const token = credentials?.token;
const user = await accountService.TokenLogin({token: token!});
return user;
},
}),
// Conditionally add enterprise SSO providers
// ...(ENTERPRISE_LICENSE_KEY ? getSSOProviders() : []),
],
callbacks: {
//@ts-ignore
async jwt({ token }) {
const adminClient = getAppcondaClientSync();
const accountService = new AccountService(adminClient);
const existingUser = await accountService.GetUserByEmail({email: token?.email!});
if (!existingUser) {
return token;
}
return {
...token,
profile: {
id: existingUser.id,
profile: existingUser,
email: existingUser.email,
// Eğer TUser'da başka alanlar varsa onları da ekleyebilirsiniz:
// image: existingUser.image,
// role: existingUser.role,
},
};
},
//@ts-ignore
async session({ session, token }) {
session.user = {
id: token.profile?.id,
profile: token.profile?.profile,
email: token.profile?.email,
// Diğer alanlar:
// image: token.profile?.image,
// role: token.profile?.role,
};
return session;
},
async signIn({ user, account }: { user: TUser; account: Account }) {
//@ts-ignore
if (account?.provider === "credentials" || account?.provider === "token") {
// check if user's email is verified or not
if (!user.emailVerified && !EMAIL_VERIFICATION_DISABLED) {
throw new Error("Email Verification is Pending");
}
return true;
}
/* if (ENTERPRISE_LICENSE_KEY) {
return handleSSOCallback({ user, account });
} */
return true;
},
},
pages: {
signIn: "/auth/login",
signOut: "/auth/logout",
error: "/auth/login", // Error code passed in query string as ?error=
},
}
}
return options;
}
})();