verivy email before log in

This commit is contained in:
Dobromir Popov
2024-05-24 19:01:24 +03:00
parent 0f6e094f24
commit a9deca22f0
6 changed files with 128 additions and 12 deletions

View File

@ -13,11 +13,20 @@ const logger = require('../../src/logger');
import fs from 'fs';
import path from 'path';
import { log } from "console";
const handlebars = require("handlebars");
const router = createRouter<NextApiRequest, NextApiResponse>();
// programatically sign in
import { getSession } from "next-auth/react";
import { signIn } from "next-auth/react";
import { authOptions } from './auth/[...nextauth]';
// import NextAuth from 'next-auth';
// import { setCookie } from 'nookies';
//action to accept coverme request from email
@ -33,10 +42,54 @@ export default async function handler(req, res) {
const emailaction = req.query.emailaction;
// Retrieve and validate the JWT token
let email = req.body.email || req.query.email;
//response is a special action that does not require a token
//PUBLIC
if (action == "email_response" || action == "account") {
switch (emailaction) {
case "validateEmail":
let token = req.query.token;
let user = await prisma.user.findUnique({
where: {
email: email
}
});
let pub = await prisma.publisher.findUnique({
where: {
email: email
}
});
if (!user && !pub) {
return res.status(400).json({ message: "Invalid user" });
}
if (user) {
await prisma.user.update({
where: {
email: email
},
data: {
emailVerified: new Date()
}
});
}
logger.info("User: " + email + " validated his email.");
console.log("User: " + email + " validated his email. Logging in...");
return res.redirect("/dash");
// log in the user using nextauth and redirect to the dashboard
//how to login the user with nextauth? maybe use the signIn callback
// const result = await signIn("credentials", {
// // redirect: false,
// email,
// account: user
// });
// if (result.error) {
// return res.status(401).json({ message: "Invalid credentials" });
// }
// return res.status(200).json({ message: "Signed in successfully" });
case "coverMeAccept":
//validate shiftId and assignmentId
let shiftId = req.query.shiftId;
@ -208,7 +261,6 @@ export default async function handler(req, res) {
// Send password reset form to the user
//parse the request body
let email = req.body.email || req.query.email;
let actualUser = await prisma.publisher.findUnique({
where: {
email: email
@ -285,6 +337,9 @@ export default async function handler(req, res) {
// const emailResponse = await common.sendEmail(user.email, "Email Action Processed",
// "Your email action was processed successfully");
}
// ########################
// PRIVATE API
// ########################
else {
const token = await getToken({ req: req });
@ -301,6 +356,17 @@ export default async function handler(req, res) {
//PRIVATE ACTIONS
switch (action) {
//in nextauth.ts
// case "validateEmail":
// let publisher = await prisma.publisher.findUnique({
// where: {
// email: token.email
// }
// });
// if (!publisher) {
// return res.status(400).json({ message: "Invalid user" });
// }
case "sendCoverMeRequestByEmail":
// Send CoverMe request to the users
//get from POST data: shiftId, assignmentId, date