fix(auth): handle signout and status update for deleted users
All checks were successful
Deploy Waveform to VPS / deploy (push) Successful in 1m14s
All checks were successful
Deploy Waveform to VPS / deploy (push) Successful in 1m14s
This commit is contained in:
parent
0d695f9fc0
commit
bbc5ec6648
1 changed files with 21 additions and 37 deletions
58
auth.ts
58
auth.ts
|
|
@ -10,9 +10,7 @@ import { users } from "@/db/schema";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { verifyPassword } from "@/lib/password";
|
import { verifyPassword } from "@/lib/password";
|
||||||
|
|
||||||
/**
|
/** NextAuth handlers, authentication methods, and auth utility exports. */
|
||||||
* NextAuth handlers, authentication methods, and auth utility exports.
|
|
||||||
*/
|
|
||||||
export const { handlers, signIn, signOut, auth } = NextAuth({
|
export const { handlers, signIn, signOut, auth } = NextAuth({
|
||||||
providers: [
|
providers: [
|
||||||
Credentials({
|
Credentials({
|
||||||
|
|
@ -20,13 +18,7 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
|
||||||
email: { label: "Email", type: "email" },
|
email: { label: "Email", type: "email" },
|
||||||
password: { label: "Password", type: "password" },
|
password: { label: "Password", type: "password" },
|
||||||
},
|
},
|
||||||
/**
|
/** Authorizes user credentials against database records. */
|
||||||
* Authorizes user credentials against database records.
|
|
||||||
*
|
|
||||||
* @function authorize
|
|
||||||
* @param {Record<string, unknown> | undefined} credentials - The incoming sign-in credentials containing email and password.
|
|
||||||
* @returns {Promise<{ id: string } | null>} The authenticated user object containing only the user ID, or null if validation fails.
|
|
||||||
*/
|
|
||||||
authorize: async (credentials) => {
|
authorize: async (credentials) => {
|
||||||
const email = credentials?.email as string | undefined;
|
const email = credentials?.email as string | undefined;
|
||||||
const password = credentials?.password as string | undefined;
|
const password = credentials?.password as string | undefined;
|
||||||
|
|
@ -57,30 +49,14 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
callbacks: {
|
callbacks: {
|
||||||
/**
|
/** Populates the JWT token with the user ID upon initial sign in. */
|
||||||
* Populates the JWT token with the user ID upon initial sign in.
|
async jwt({ token, user }) {
|
||||||
*
|
|
||||||
* @function jwt
|
|
||||||
* @param {Object} params - The callback parameters.
|
|
||||||
* @param {import("next-auth/jwt").JWT} params.token - The current JSON Web Token.
|
|
||||||
* @param {import("next-auth").User} [params.user] - The authenticated user object (available on first sign in).
|
|
||||||
* @returns {import("next-auth/jwt").JWT} The updated JWT token.
|
|
||||||
*/
|
|
||||||
jwt({ token, user }) {
|
|
||||||
if (user) {
|
if (user) {
|
||||||
token.id = user.id;
|
token.id = user.id;
|
||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
},
|
},
|
||||||
/**
|
/** Attaches the user ID from the JWT token to the active session. */
|
||||||
* Attaches the user ID from the JWT token to the active session.
|
|
||||||
*
|
|
||||||
* @function session
|
|
||||||
* @param {Object} params - The callback parameters.
|
|
||||||
* @param {import("next-auth").Session} params.session - The current user session object.
|
|
||||||
* @param {import("next-auth/jwt").JWT} params.token - The active JSON Web Token.
|
|
||||||
* @returns {import("next-auth").Session} The updated session object.
|
|
||||||
*/
|
|
||||||
session({ session, token }) {
|
session({ session, token }) {
|
||||||
if (token && session.user) {
|
if (token && session.user) {
|
||||||
session.user.id = token.id as string;
|
session.user.id = token.id as string;
|
||||||
|
|
@ -89,14 +65,7 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
events: {
|
events: {
|
||||||
/**
|
/** Updates the user's status to ONLINE and refreshes lastSeenAt upon successful sign in. */
|
||||||
* Updates the user's status to ONLINE and refreshes lastSeenAt upon successful sign in.
|
|
||||||
*
|
|
||||||
* @function signIn
|
|
||||||
* @param {Object} params - The event parameters.
|
|
||||||
* @param {import("next-auth").User} params.user - The signed-in user object.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
async signIn({ user }) {
|
async signIn({ user }) {
|
||||||
if (user?.id) {
|
if (user?.id) {
|
||||||
await db
|
await db
|
||||||
|
|
@ -108,6 +77,21 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
|
||||||
.where(eq(users.id, user.id));
|
.where(eq(users.id, user.id));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
/** Updates the user's status to OFFLINE upon explicit sign out. */
|
||||||
|
async signOut(message) {
|
||||||
|
if ("token" in message && message.token?.id) {
|
||||||
|
await db
|
||||||
|
.update(users)
|
||||||
|
.set({
|
||||||
|
status: "OFFLINE",
|
||||||
|
lastSeenAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(users.id, message.token.id as string))
|
||||||
|
.catch(() => {
|
||||||
|
// Silently ignore if user was already deleted from DB
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
pages: {
|
pages: {
|
||||||
signIn: "/login",
|
signIn: "/login",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue