"use strict";
exports.id = 559;
exports.ids = [559];
exports.modules = {

/***/ 4812:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {


// EXPORTS
__webpack_require__.d(__webpack_exports__, {
  prisma: () => (/* binding */ prisma)
});

// UNUSED EXPORTS: AccountModel, RelatedAccountModel, RelatedSessionModel, RelatedUserModel, SessionModel, UserModel, VerificationRequestModel

// EXTERNAL MODULE: external "@prisma/client"
var client_ = __webpack_require__(53524);
// EXTERNAL MODULE: ../../node_modules/.pnpm/zod@3.21.4/node_modules/zod/lib/index.mjs
var lib = __webpack_require__(45948);
;// CONCATENATED MODULE: ../../configurations/prisma/src/zod/account.ts


const AccountModel = lib/* object */.Ry({
    id: lib/* string */.Z_(),
    userId: lib/* string */.Z_(),
    providerType: lib/* string */.Z_(),
    providerId: lib/* string */.Z_(),
    providerAccountId: lib/* string */.Z_(),
    refreshToken: lib/* string */.Z_().nullish(),
    accessToken: lib/* string */.Z_().nullish(),
    accessTokenExpires: lib/* date */.hT().nullish(),
    createdAt: lib/* date */.hT(),
    updatedAt: lib/* date */.hT()
});
/**
 * RelatedAccountModel contains all relations on your model in addition to the scalars
 *
 * NOTE: Lazy required in case of potential circular dependencies within schema
 */ const RelatedAccountModel = lib/* lazy */.Vo(()=>AccountModel.extend({
        user: RelatedUserModel
    }));

;// CONCATENATED MODULE: ../../configurations/prisma/src/zod/session.ts


const SessionModel = lib/* object */.Ry({
    id: lib/* string */.Z_(),
    userId: lib/* string */.Z_(),
    expires: lib/* date */.hT(),
    sessionToken: lib/* string */.Z_(),
    accessToken: lib/* string */.Z_(),
    createdAt: lib/* date */.hT(),
    updatedAt: lib/* date */.hT()
});
/**
 * RelatedSessionModel contains all relations on your model in addition to the scalars
 *
 * NOTE: Lazy required in case of potential circular dependencies within schema
 */ const RelatedSessionModel = lib/* lazy */.Vo(()=>SessionModel.extend({
        user: RelatedUserModel
    }));

;// CONCATENATED MODULE: ../../configurations/prisma/src/zod/user.ts


const UserModel = lib/* object */.Ry({
    id: lib/* string */.Z_(),
    name: lib/* string */.Z_().nullish(),
    email: lib/* string */.Z_().nullish(),
    emailVerified: lib/* date */.hT().nullish(),
    image: lib/* string */.Z_().nullish(),
    createdAt: lib/* date */.hT(),
    updatedAt: lib/* date */.hT()
});
/**
 * RelatedUserModel contains all relations on your model in addition to the scalars
 *
 * NOTE: Lazy required in case of potential circular dependencies within schema
 */ const RelatedUserModel = lib/* lazy */.Vo(()=>UserModel.extend({
        accounts: RelatedAccountModel.array(),
        sessions: RelatedSessionModel.array()
    }));

;// CONCATENATED MODULE: ../../configurations/prisma/src/zod/verificationrequest.ts

const VerificationRequestModel = lib/* object */.Ry({
    id: lib/* string */.Z_(),
    identifier: lib/* string */.Z_(),
    token: lib/* string */.Z_(),
    expires: lib/* date */.hT(),
    createdAt: lib/* date */.hT(),
    updatedAt: lib/* date */.hT()
});

;// CONCATENATED MODULE: ../../configurations/prisma/src/zod/index.ts





;// CONCATENATED MODULE: ../../configurations/prisma/index.ts
// Copyright (C) 2023 Light, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

const prisma = global.prisma || new client_.PrismaClient({
    log: [
        "query",
        "info",
        "warn",
        "error"
    ]
});
if (false) {}

// eslint-disable-next-line import/no-unresolved



/***/ }),

/***/ 15559:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   Nh: () => (/* binding */ nextAuthOptions),
/* harmony export */   nu: () => (/* binding */ getNextAuthServerSession)
/* harmony export */ });
/* unused harmony exports commonAuthOptions, authOptions, getAuthServerSession */
/* harmony import */ var next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(47449);
/* harmony import */ var next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var next_auth_react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(41649);
/* harmony import */ var next_auth_react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(next_auth_react__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var siwe__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(96703);
/* harmony import */ var siwe__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(siwe__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _lightdotso_prisma__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(4812);
/* harmony import */ var _next_auth_prisma_adapter__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(32104);
/* harmony import */ var _next_auth_prisma_adapter__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(_next_auth_prisma_adapter__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var next_auth__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(73227);
/* harmony import */ var next_auth__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(next_auth__WEBPACK_IMPORTED_MODULE_5__);
/* harmony import */ var viem__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(48534);
/* harmony import */ var viem__WEBPACK_IMPORTED_MODULE_6___default = /*#__PURE__*/__webpack_require__.n(viem__WEBPACK_IMPORTED_MODULE_6__);
// Copyright (C) 2023 Light, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.







const commonAuthOptions = {
    session: {
        strategy: "jwt"
    },
    adapter: (0,_next_auth_prisma_adapter__WEBPACK_IMPORTED_MODULE_4__.PrismaAdapter)(_lightdotso_prisma__WEBPACK_IMPORTED_MODULE_3__.prisma),
    secret: process.env.NEXTAUTH_SECRET,
    callbacks: {
        async session ({ session, token }) {
            session.address = token.sub;
            session.user.name = token.sub;
            session.user.image = "/";
            return session;
        }
    }
};
const authOptions = {
    ...commonAuthOptions,
    providers: [
        next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_0___default()({
            id: "eth",
            name: "Ethereum",
            credentials: {
                message: {
                    label: "Message",
                    type: "text",
                    placeholder: "0x0"
                },
                signature: {
                    label: "Signature",
                    type: "text",
                    placeholder: "0x0"
                }
            },
            async authorize (credentials, req) {
                // console.log(JSON.stringify(credentials, null, 2));
                try {
                    const siwe = new siwe__WEBPACK_IMPORTED_MODULE_2__.SiweMessage(JSON.parse(credentials?.message || "{}"));
                    const nextAuthUrl = new URL(process.env.NEXTAUTH_URL);
                    const result = await siwe.verify({
                        signature: credentials?.signature,
                        domain: nextAuthUrl.host,
                        nonce: await (0,next_auth_react__WEBPACK_IMPORTED_MODULE_1__.getCsrfToken)({
                            req: {
                                headers: req?.headers
                            }
                        })
                    });
                    if (!result.success) {
                        throw new Error("Invalid Signature");
                    }
                    if (result.data.statement !== "Login") {
                        throw new Error("Statement Mismatch");
                    }
                    if (result.success) {
                        console.log("success");
                        return {
                            id: siwe.address
                        };
                    }
                    return null;
                } catch (err) {
                    console.error(err);
                    return null;
                }
            }
        })
    ]
};
const nextAuthOptions = ({ req })=>({
        ...commonAuthOptions,
        providers: [
            next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_0___default()({
                id: "eth",
                name: "Ethereum",
                credentials: {
                    message: {
                        label: "Message",
                        type: "text",
                        placeholder: "0x0"
                    },
                    signature: {
                        label: "Signature",
                        type: "text",
                        placeholder: "0x0"
                    }
                },
                async authorize (credentials) {
                    // console.log(JSON.stringify(credentials, null, 2));
                    try {
                        const siwe = new siwe__WEBPACK_IMPORTED_MODULE_2__.SiweMessage(JSON.parse(credentials?.message || "{}"));
                        const nextAuthUrl = new URL(process.env.NEXTAUTH_URL);
                        const result = await siwe.verify({
                            signature: credentials?.signature,
                            domain: nextAuthUrl.host,
                            nonce: await (0,next_auth_react__WEBPACK_IMPORTED_MODULE_1__.getCsrfToken)({
                                req: {
                                    headers: req?.headers
                                }
                            })
                        });
                        if (!result.success) {
                            throw new Error("Invalid Signature");
                        }
                        if (result.data.statement !== "Login") {
                            throw new Error("Statement Mismatch");
                        }
                        // Get the address in checksum format
                        const address = (0,viem__WEBPACK_IMPORTED_MODULE_6__.getAddress)(result.data.address);
                        // Check if the account already exists
                        const existingAccount = await _lightdotso_prisma__WEBPACK_IMPORTED_MODULE_3__.prisma.account.findFirst({
                            where: {
                                providerAccountId: address
                            },
                            select: {
                                userId: true,
                                providerAccountId: true,
                                user: {
                                    select: {
                                        image: true,
                                        name: true
                                    }
                                }
                            }
                        });
                        if (existingAccount) {
                            return {
                                id: existingAccount.userId,
                                name: existingAccount.user.name,
                                image: existingAccount.user.image
                            };
                        }
                        // Create new user and account sequentially
                        const { user } = await _lightdotso_prisma__WEBPACK_IMPORTED_MODULE_3__.prisma.$transaction(async (tx)=>{
                            const user = await tx.user.create({
                                data: {
                                    name: address
                                }
                            });
                            const account = await tx.account.create({
                                data: {
                                    userId: user.id,
                                    providerType: "credentials",
                                    providerId: "eth",
                                    providerAccountId: address
                                }
                            });
                            return {
                                user,
                                account
                            };
                        });
                        return {
                            id: user.id,
                            name: user.id
                        };
                    } catch (err) {
                        console.error(err);
                        return null;
                    }
                }
            })
        ]
    });
const getAuthServerSession = async ()=>{
    return await getServerSession(authOptions);
};
const getNextAuthServerSession = async (ctx)=>{
    // Changed from authOptions to authOption(ctx)
    // This allows use to retrieve the csrf token to verify as the nonce
    return (0,next_auth__WEBPACK_IMPORTED_MODULE_5__.getServerSession)(ctx.req, ctx.res, nextAuthOptions(ctx));
};


/***/ })

};
;