Files
the-biergarten-app/config/nextConnect/middleware/getCurrentUser.ts
Aaron William Po 584e3b349f Implement confirm user functionality
This commit adds the necessary functionality to confirm a user's account.

It includes the addition of a new column in the user table to track whether an account is confirmed or not, and the implementation of JWT for confirmation tokens.

This commit integrates the SparkPost API as well as React Email to send dynamic emails for whatever purpose.

Upon user registration, a confirmation email will be sent to the user.
2023-03-13 22:35:57 -04:00

26 lines
757 B
TypeScript

import { NextApiResponse } from 'next';
import { NextHandler } from 'next-connect';
import findUserById from '@/services/User/findUserById';
import ServerError from '@/config/util/ServerError';
import { getLoginSession } from '../../auth/session';
import { UserExtendedNextApiRequest } from '../../auth/types';
/** Get the current user from the session. Adds the user to the request object. */
const getCurrentUser = async (
req: UserExtendedNextApiRequest,
res: NextApiResponse,
next: NextHandler,
) => {
const session = await getLoginSession(req);
const user = await findUserById(session?.id);
if (!user) {
throw new ServerError('User is not logged in.', 401);
}
req.user = user;
return next();
};
export default getCurrentUser;