next-auth
Version:
Authentication for Next.js
206 lines (180 loc) • 5.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = callbackHandler;
var _errors = require("../errors");
var _utils = require("./utils");
var _crypto = require("crypto");
async function callbackHandler(params) {
const {
sessionToken,
profile,
account,
options
} = params;
if (!(account !== null && account !== void 0 && account.providerAccountId) || !account.type) throw new Error("Missing or invalid provider account");
if (!["email", "oauth"].includes(account.type)) throw new Error("Provider not supported");
const {
adapter,
jwt,
events,
session: {
strategy: sessionStrategy
}
} = options;
if (!adapter) {
return {
user: profile,
account,
session: {}
};
}
const {
createUser,
updateUser,
getUser,
getUserByAccount,
getUserByEmail,
linkAccount,
createSession,
getSessionAndUser,
deleteSession
} = adapter;
let session = null;
let user = null;
let isNewUser = false;
const useJwtSession = sessionStrategy === "jwt";
if (sessionToken) {
if (useJwtSession) {
try {
session = await jwt.decode({ ...jwt,
token: sessionToken
});
if (session && "sub" in session && session.sub) {
user = await getUser(session.sub);
}
} catch (_unused) {}
} else {
const userAndSession = await getSessionAndUser(sessionToken);
if (userAndSession) {
session = userAndSession.session;
user = userAndSession.user;
}
}
}
if (account.type === "email") {
const userByEmail = profile.email ? await getUserByEmail(profile.email) : null;
if (userByEmail) {
var _user, _events$updateUser;
if (((_user = user) === null || _user === void 0 ? void 0 : _user.id) !== userByEmail.id && !useJwtSession && sessionToken) {
await deleteSession(sessionToken);
}
user = await updateUser({
id: userByEmail.id,
emailVerified: new Date()
});
await ((_events$updateUser = events.updateUser) === null || _events$updateUser === void 0 ? void 0 : _events$updateUser.call(events, {
user
}));
} else {
var _events$createUser;
const newUser = { ...profile,
emailVerified: new Date()
};
delete newUser.id;
user = await createUser(newUser);
await ((_events$createUser = events.createUser) === null || _events$createUser === void 0 ? void 0 : _events$createUser.call(events, {
user
}));
isNewUser = true;
}
session = useJwtSession ? {} : await createSession({
sessionToken: generateSessionToken(),
userId: user.id,
expires: (0, _utils.fromDate)(options.session.maxAge)
});
return {
session,
user,
isNewUser
};
} else if (account.type === "oauth") {
const userByAccount = await getUserByAccount({
providerAccountId: account.providerAccountId,
provider: account.provider
});
if (userByAccount) {
if (user) {
if (userByAccount.id === user.id) {
return {
session,
user,
isNewUser
};
}
throw new _errors.AccountNotLinkedError("The account is already associated with another user");
}
session = useJwtSession ? {} : await createSession({
sessionToken: generateSessionToken(),
userId: userByAccount.id,
expires: (0, _utils.fromDate)(options.session.maxAge)
});
return {
session,
user: userByAccount,
isNewUser
};
} else {
var _events$createUser2, _events$linkAccount2;
if (user) {
var _events$linkAccount;
await linkAccount({ ...account,
userId: user.id
});
await ((_events$linkAccount = events.linkAccount) === null || _events$linkAccount === void 0 ? void 0 : _events$linkAccount.call(events, {
user,
account
}));
return {
session,
user,
isNewUser
};
}
const userByEmail = profile.email ? await getUserByEmail(profile.email) : null;
if (userByEmail) {
throw new _errors.AccountNotLinkedError("Another account already exists with the same e-mail address");
}
const newUser = { ...profile,
emailVerified: null
};
delete newUser.id;
user = await createUser(newUser);
await ((_events$createUser2 = events.createUser) === null || _events$createUser2 === void 0 ? void 0 : _events$createUser2.call(events, {
user
}));
await linkAccount({ ...account,
userId: user.id
});
await ((_events$linkAccount2 = events.linkAccount) === null || _events$linkAccount2 === void 0 ? void 0 : _events$linkAccount2.call(events, {
user,
account
}));
session = useJwtSession ? {} : await createSession({
sessionToken: generateSessionToken(),
userId: user.id,
expires: (0, _utils.fromDate)(options.session.maxAge)
});
return {
session,
user,
isNewUser: true
};
}
}
}
function generateSessionToken() {
var _randomUUID;
return (_randomUUID = _crypto.randomUUID === null || _crypto.randomUUID === void 0 ? void 0 : (0, _crypto.randomUUID)()) !== null && _randomUUID !== void 0 ? _randomUUID : (0, _crypto.randomBytes)(32).toString("hex");
}