Implemented email verification

This commit is contained in:
2024-04-12 13:52:16 +03:00
parent 78107d4ec7
commit b1ad7b5c3e
51 changed files with 604 additions and 213 deletions

View File

@@ -1,7 +1,7 @@
import NextAuth from 'next-auth'
import { UserRole } from '@prisma/client'
import { PrismaAdapter } from '@auth/prisma-adapter'
import { db } from '@/lib/db'
import db from '@/lib/db'
import authConfig from '@/auth.config'
import { getUserById } from '@/data/user'
import { AUTH_ERROR_URL, AUTH_LOGIN_URL } from '@/config/routes'
@@ -61,7 +61,7 @@ export const {
if (!token.sub) return token
const existingUser = await getUserById(token.sub)
if (!existingUser) return token
token.role = existingUser.role

View File

@@ -1 +0,0 @@
export const bg: string = 'bg-[radial-gradient(ellipse_at_top,_var(--tw-gradient-stops))] from-sky-400 to-blue-800'

View File

@@ -1,13 +1,23 @@
// @https://www.localeplanet.com/icu/index.html
type loc = ('uk' | 'en')
const defaultLocale = 'uk'
type Locale = {
id: string,
java: string,
iso: string,
code: loc,
name: string,
originalName: string,
}
export type loc = ('uk' | 'en')
const defaultLocale: loc = 'uk'
const fallbackLocale: loc = 'en'
const importLocales = {
uk: () => import('@/locales/uk'), en: () => import('@/locales/en'),
}
const LC = [
} as const
const LC: Locale[] = [
{
id: 'uk_UA',
java: 'uk-UA',
@@ -23,8 +33,8 @@ const LC = [
code: 'en',
name: 'English',
originalName: 'English',
}]
}] as const
const locales = LC.map(locale => locale.code)
const locales: loc[] = LC.map((locale: Locale) => locale.code)
export { locales, defaultLocale, LC, importLocales }
export { locales, defaultLocale, fallbackLocale, LC, importLocales, type loc }

View File

@@ -1,16 +1,16 @@
import { env } from 'process'
import SMTPTransport from 'nodemailer/lib/smtp-transport'
import { env } from '@/lib/utils'
export const from: string = `"${env.MAIL_SERVER_SENDER_NAME}" <${env.MAIL_SERVER_USERNAME}>`
export const from: string = `"${env('MAIL_SERVER_SENDER_NAME')}" <${env('MAIL_SERVER_USERNAME')}>`
export const transportOptions: SMTPTransport | SMTPTransport.Options | string = {
host: env.MAIL_SERVER_HOST,
debug: env.MAIL_SERVER_DEBUG === 'true',
logger: env.MAIL_SERVER_LOG === 'true',
port: parseInt(env.MAIL_SERVER_PORT as string),
secure: env.MAIL_SERVER_PORT === '465', // Use `true` for port 465, `false` for all other ports
host: env('MAIL_SERVER_HOST'),
debug: env('MAIL_SERVER_DEBUG') === 'true' && env('NODE_ENV') !== 'production',
logger: env('MAIL_SERVER_LOG') === 'true' && env('NODE_ENV') !== 'production',
port: parseInt(env('MAIL_SERVER_PORT')),
secure: env('MAIL_SERVER_PORT') === '465', // Use `true` for port 465, `false` for all other ports
auth: {
user: env.MAIL_SERVER_USERNAME, pass: env.MAIL_SERVER_PASSWORD,
user: env('MAIL_SERVER_USERNAME'), pass: env('MAIL_SERVER_PASSWORD'),
},
}

View File

@@ -1,10 +1,11 @@
import { locales } from '@/config/locales'
import { UUID_V4_REGEX } from '@/config/validation'
export const USER_PROFILE_URL: string = '/cabinet'
export const AUTH_LOGIN_URL: string = '/auth/login'
export const AUTH_REGISTER_URL: string = '/auth/register'
export const AUTH_ERROR_URL: string = '/auth/error'
export const AUTH_EMAIL_VERIFICATION_URL: string = '/auth/email-verification'
export const AUTH_USER_VERIFICATION_URL: string = '/auth/user-verification/'
/**
* An array of routes that accessible to the public.
@@ -12,7 +13,7 @@ export const AUTH_EMAIL_VERIFICATION_URL: string = '/auth/email-verification'
* @type {string[]}
*/
export const publicRoutes: string[] = [
'/', '/about']
'/', '/about', `${AUTH_USER_VERIFICATION_URL}${UUID_V4_REGEX}`]
/**
* An array of routes that are used for authentication.
@@ -20,7 +21,7 @@ export const publicRoutes: string[] = [
* @type {string[]}
*/
export const authRoutes: string[] = [
AUTH_LOGIN_URL, AUTH_REGISTER_URL, AUTH_ERROR_URL, AUTH_EMAIL_VERIFICATION_URL]
AUTH_LOGIN_URL, AUTH_REGISTER_URL, AUTH_ERROR_URL]
/**
* The prefix for API authentication routes.
@@ -39,6 +40,5 @@ export const testPathnameRegex = (
pages: string[], pathName: string): boolean => {
const pattern: string = `^(/(${locales.join('|')}))?(${pages.flatMap(
(p) => (p === '/' ? ['', '/'] : p)).join('|')})/?$`
return RegExp(pattern, 'is').test(pathName)
}

View File

@@ -1,2 +1,8 @@
export const MIN_PASSWORD_LENGTH: number = 6
export const PASSWORD_SALT_LENGTH: number = 10
export const MAX_PASSWORD_LENGTH: number = 15
export const PASSWORD_SALT_LENGTH: number = 10
export const UUID_V4_REGEX: string = '[\x30-\x39\x61-\x66]{8}-[\x30-\x39\x61-\x66]{4}-4[\x30-\x39\x61-\x66]{3}-[\x30-\x39\x61-\x66]{4}-[\x30-\x39\x61-\x66]{12}'
export const PASSWORD_STRENGTH_ACME: string = `(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E])` //.{${MIN_PASSWORD_LENGTH},${MAX_PASSWORD_LENGTH}
export const PASSWORD_STRENGTH_STRONG: string = `^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=|.*?[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E])$`