finished reset password & other changes
This commit is contained in:
7
lib/server.ts
Normal file
7
lib/server.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
'use server'
|
||||
|
||||
import { readdir } from 'fs/promises'
|
||||
|
||||
export const getDirectories = async (source: string) => {
|
||||
return (await readdir(source, { withFileTypes: true })).filter(dirent => dirent.isDirectory()).map(dirent => dirent.name)
|
||||
}
|
||||
@@ -1,14 +1,38 @@
|
||||
import { v4 as uuid } from 'uuid'
|
||||
import { v4 as uuidV4 } from 'uuid'
|
||||
import {
|
||||
VERIFICATION_TOKEN_EXPIRATION_DURATION,
|
||||
} from '@/config/auth'
|
||||
import db from '@/lib/db'
|
||||
import { getVerificationTokenByEmail } from '@/data/verification-token'
|
||||
import { getPasswordResetTokenByEmail } from '@/data/password-reset-token'
|
||||
|
||||
export const generatePasswordResetToken = async (email: string) => {
|
||||
const token = uuidV4()
|
||||
const expires = new Date(new Date().getTime() + VERIFICATION_TOKEN_EXPIRATION_DURATION)
|
||||
const existingToken = await getPasswordResetTokenByEmail(email)
|
||||
|
||||
if (existingToken) {
|
||||
await db.passwordResetToken.delete({
|
||||
where: {
|
||||
id: existingToken.id,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const passwordResetToken = await db.passwordResetToken.create({
|
||||
data: {
|
||||
email,
|
||||
token,
|
||||
expires,
|
||||
},
|
||||
})
|
||||
|
||||
return passwordResetToken
|
||||
}
|
||||
|
||||
export const generateVerificationToken = async (email: string) => {
|
||||
const token = uuid()
|
||||
const expires = new Date(
|
||||
new Date().getTime() + VERIFICATION_TOKEN_EXPIRATION_DURATION)
|
||||
const token = uuidV4()
|
||||
const expires = new Date(new Date().getTime() + VERIFICATION_TOKEN_EXPIRATION_DURATION)
|
||||
|
||||
const existingToken = await getVerificationTokenByEmail(email)
|
||||
|
||||
|
||||
@@ -1,29 +1,75 @@
|
||||
import { type loc, locales, fallbackLocale } from '@/config/locales'
|
||||
'use server'
|
||||
|
||||
export const __c = async (key: string | null | undefined, locale?: loc) => {
|
||||
import { fallbackLocale, type loc, locales } from '@/config/locales'
|
||||
import { getCurrentLocale } from '@/locales/server'
|
||||
import { getDirectories } from '@/lib/server'
|
||||
|
||||
type Params = { [index: string]: number | string }
|
||||
|
||||
interface DoParamsProps {
|
||||
key: string;
|
||||
params?: Params | null | undefined;
|
||||
}
|
||||
|
||||
const doParams = async ({ key, params }: DoParamsProps): Promise<string> => {
|
||||
if (key.trim().length === 0 || Object?.keys({ params }).length === 0) return key
|
||||
|
||||
for (let val in params) {key = key.replace(`{${val}}`, params[val] as string)}
|
||||
|
||||
return key
|
||||
}
|
||||
|
||||
export const __ct = async ({ key, params }: { key: string | null | undefined, params?: {} }, locale?: loc) => {
|
||||
key = (key ?? '').trim()
|
||||
if (key.length === 0) return key
|
||||
|
||||
if (!locales.includes(locale ??= fallbackLocale)) {
|
||||
locale ??= getCurrentLocale()
|
||||
|
||||
if (!locales.includes(locale)) {
|
||||
locale = fallbackLocale
|
||||
}
|
||||
|
||||
let data: any = await import(`@/locales/custom.${locale}`).then(({ default: data }) => data).catch(() => false)
|
||||
const keys = key.split('.')
|
||||
const scopes = await getDirectories(`${process.cwd()}/locales/custom`)
|
||||
|
||||
if (keys.length < 2 && !scopes.includes(keys[0])) return key
|
||||
const scope = keys.shift()
|
||||
|
||||
let data: any = await import(`@/locales/custom/${scope}/${locale}`).then(({ default: data }) => data).catch(() => false)
|
||||
if (data === false) return key
|
||||
|
||||
const x = key.split('.')
|
||||
let c: number = x.length
|
||||
let c: number = keys.length
|
||||
|
||||
if (c === 1) {
|
||||
return data.hasOwn(x[0]) && typeof data[x[0]] === 'string' ? data[x[0]] : key
|
||||
const _ = data.hasOwnProperty(keys[0]) && typeof data[keys[0]] === 'string' ? data[keys[0]] : key
|
||||
return await doParams({ key: _, params })
|
||||
}
|
||||
|
||||
for (let i in x) {
|
||||
if (data.hasOwn(x[i])) {
|
||||
data = data[x[i]]
|
||||
for (let i in keys) {
|
||||
if (data.hasOwnProperty(keys[i])) {
|
||||
data = data[keys[i]]
|
||||
c--
|
||||
}
|
||||
}
|
||||
|
||||
return c === 0 ? data : key
|
||||
return await doParams({ key: c === 0 ? data : key, params })
|
||||
}
|
||||
|
||||
export const _ctBatch = async (keys: { [index: string]: string | [string, Params] }, scope?: string | null) => {
|
||||
|
||||
for (const k in keys) {
|
||||
let key: string = scope ? scope + '.' : ''
|
||||
let params: Params | undefined = undefined
|
||||
|
||||
if (Array.isArray(keys[k])) {
|
||||
key += keys[k][0]
|
||||
params = keys[k][1] as Params
|
||||
} else {
|
||||
key += keys[k]
|
||||
}
|
||||
|
||||
keys[k] = await __ct({ key, params })
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
14
lib/utils.ts
14
lib/utils.ts
@@ -1,6 +1,6 @@
|
||||
import { type ClassValue, clsx } from 'clsx'
|
||||
import { twMerge } from 'tailwind-merge'
|
||||
import { LC } from '@/config/locales'
|
||||
import { LC, locales } from '@/config/locales'
|
||||
|
||||
import { env as dotEnv } from 'process'
|
||||
|
||||
@@ -16,6 +16,14 @@ export function env (variable: string, defaultValue?: string | ''): string {
|
||||
return (dotEnv[variable] ?? defaultValue ?? '')
|
||||
}
|
||||
|
||||
export function tr (el: React.ReactNode, params: object) {
|
||||
export const testPathnameRegex = (
|
||||
pages: string[], pathName: string): boolean => {
|
||||
const pattern: string = `^(/(${locales.join('|')}))?(${pages.flatMap(
|
||||
(p) => (p === '/' ? ['', '/'] : p)).join('|')})/?$`
|
||||
|
||||
//console.log(pattern)
|
||||
|
||||
return RegExp(pattern, 'is').test(pathName)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user