mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-08 19:16:37 +08:00
auth change to log on redisRestClient
This commit is contained in:
parent
145701dc3e
commit
1ad18ca1c8
19
app/auth.ts
19
app/auth.ts
@ -5,6 +5,8 @@ import {
|
|||||||
} from "next-auth";
|
} from "next-auth";
|
||||||
import AzureADProvider from "next-auth/providers/azure-ad";
|
import AzureADProvider from "next-auth/providers/azure-ad";
|
||||||
|
|
||||||
|
import { incrementSignInCount, incrementSessionRefreshCount } from '../utils/cloud/redisRestClient';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
|
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
|
||||||
* object and keep type safety.
|
* object and keep type safety.
|
||||||
@ -60,6 +62,12 @@ export const authOptions: NextAuthOptions = {
|
|||||||
export const authOptions: NextAuthOptions = {
|
export const authOptions: NextAuthOptions = {
|
||||||
callbacks: {
|
callbacks: {
|
||||||
async signIn({ user, account, profile }) {
|
async signIn({ user, account, profile }) {
|
||||||
|
if (!user?.email) {
|
||||||
|
console.error("Email is required for sign in");
|
||||||
|
return false; // Prevent sign-in
|
||||||
|
}
|
||||||
|
const dateKey = new Date().toISOString().slice(0, 7); // "YYYY-MM"
|
||||||
|
await incrementSignInCount(user.email, dateKey);
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
async session({ session, token }) {
|
async session({ session, token }) {
|
||||||
@ -71,6 +79,17 @@ export const authOptions: NextAuthOptions = {
|
|||||||
id: userId,
|
id: userId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Assuming the email is stored in the token and not directly in the session.user object
|
||||||
|
if (!token?.email) {
|
||||||
|
console.error("Email is required for session handling");
|
||||||
|
// Modify the session object as needed or return a modified session
|
||||||
|
// For example, you might want to set a flag indicating an incomplete session
|
||||||
|
session.error = "Email is missing";
|
||||||
|
return session; // Return the modified session
|
||||||
|
}
|
||||||
|
const dateKey = new Date().toISOString().slice(0, 7); // "YYYY-MM"
|
||||||
|
await incrementSessionRefreshCount(token.email, dateKey);
|
||||||
|
|
||||||
return session;
|
return session;
|
||||||
},
|
},
|
||||||
// Add other callbacks with async as needed
|
// Add other callbacks with async as needed
|
||||||
|
@ -14,6 +14,28 @@ const headers = {
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const incrementSignInCount = async (email: string | undefined, dateKey: string) => {
|
||||||
|
if (!email) {
|
||||||
|
console.error('Email is undefined, cannot increment sign-in count.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`${redisRestUrl}/hincrby`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: headers,
|
||||||
|
body: JSON.stringify({
|
||||||
|
key: `signin_count:${email}`,
|
||||||
|
field: dateKey,
|
||||||
|
increment: 1
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
console.error('Failed to increment sign-in count in Redis via REST API');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
export const incrementSessionRefreshCount = async (email: string | undefined, dateKey: string) => {
|
export const incrementSessionRefreshCount = async (email: string | undefined, dateKey: string) => {
|
||||||
if (!email) {
|
if (!email) {
|
||||||
console.error('Email is undefined, cannot increment session refresh count.');
|
console.error('Email is undefined, cannot increment session refresh count.');
|
||||||
|
Loading…
Reference in New Issue
Block a user