mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-20 08:15:26 +02:00
added axios client; fixed "validate-email" -> "verify-email"
This commit is contained in:
parent
455ecf2a37
commit
5e4e491483
@ -17,6 +17,7 @@
|
|||||||
"@mui/material": "^5.10.9",
|
"@mui/material": "^5.10.9",
|
||||||
"@tanstack/react-query": "^4.12.0",
|
"@tanstack/react-query": "^4.12.0",
|
||||||
"axios": "^1.1.2",
|
"axios": "^1.1.2",
|
||||||
|
"axios-case-converter": "^0.11.1",
|
||||||
"crypto-js": "^4.1.1",
|
"crypto-js": "^4.1.1",
|
||||||
"date-fns": "^2.29.3",
|
"date-fns": "^2.29.3",
|
||||||
"formik": "^2.2.9",
|
"formik": "^2.2.9",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import {ReactElement, ReactNode, useCallback, useEffect, useMemo} from "react"
|
import {ReactElement, ReactNode, useCallback, useEffect, useMemo} from "react"
|
||||||
import {useLocalStorage} from "react-use"
|
import {useLocalStorage} from "react-use"
|
||||||
import axios, {AxiosError} from "axios"
|
import {AxiosError} from "axios"
|
||||||
|
|
||||||
import {useMutation} from "@tanstack/react-query"
|
import {useMutation} from "@tanstack/react-query"
|
||||||
|
|
||||||
@ -11,6 +11,7 @@ import {
|
|||||||
logout as logoutUser,
|
logout as logoutUser,
|
||||||
refreshToken,
|
refreshToken,
|
||||||
} from "~/apis"
|
} from "~/apis"
|
||||||
|
import {client} from "~/constants/axios-client"
|
||||||
|
|
||||||
import AuthContext, {AuthContextType} from "./AuthContext"
|
import AuthContext, {AuthContextType} from "./AuthContext"
|
||||||
|
|
||||||
@ -59,7 +60,7 @@ export default function AuthContextProvider({
|
|||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const interceptor = axios.interceptors.response.use(
|
const interceptor = client.interceptors.response.use(
|
||||||
response => response,
|
response => response,
|
||||||
async (error: AxiosError) => {
|
async (error: AxiosError) => {
|
||||||
if (error.isAxiosError) {
|
if (error.isAxiosError) {
|
||||||
@ -80,7 +81,7 @@ export default function AuthContextProvider({
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
return () => axios.interceptors.response.eject(interceptor)
|
return () => client.interceptors.response.eject(interceptor)
|
||||||
}, [logout, refresh])
|
}, [logout, refresh])
|
||||||
|
|
||||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
|
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import axios from "axios"
|
import {client} from "~/constants/axios-client"
|
||||||
|
|
||||||
export default async function checkIsDomainDisposable(
|
export default async function checkIsDomainDisposable(
|
||||||
domain: string,
|
domain: string,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const {data} = await axios.get(`https://api.mailcheck.ai/domain/${domain}`)
|
const {data} = await client.get(`https://api.mailcheck.ai/domain/${domain}`)
|
||||||
|
|
||||||
return !data.mx || data.disposable
|
return !data.mx || data.disposable
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
import axios from "axios"
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Alias,
|
Alias,
|
||||||
AliasType,
|
AliasType,
|
||||||
ImageProxyFormatType,
|
ImageProxyFormatType,
|
||||||
ProxyUserAgentType,
|
ProxyUserAgentType,
|
||||||
} from "~/server-types"
|
} from "~/server-types"
|
||||||
import parseAlias from "~/apis/helpers/parse-alias"
|
import {client} from "~/constants/axios-client"
|
||||||
|
|
||||||
interface CreateAliasDataOther {
|
interface CreateAliasDataOther {
|
||||||
isActive?: boolean
|
isActive?: boolean
|
||||||
@ -40,10 +38,10 @@ export type CreateAliasData =
|
|||||||
export default async function createAlias(
|
export default async function createAlias(
|
||||||
aliasData: CreateAliasData,
|
aliasData: CreateAliasData,
|
||||||
): Promise<Alias> {
|
): Promise<Alias> {
|
||||||
const {data} = await axios.post(
|
const {data} = await client.post(
|
||||||
`${import.meta.env.VITE_SERVER_BASE_URL}/alias`,
|
`${import.meta.env.VITE_SERVER_BASE_URL}/alias`,
|
||||||
{},
|
aliasData,
|
||||||
)
|
)
|
||||||
|
|
||||||
return parseAlias(data)
|
return data
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
import axios from "axios"
|
|
||||||
|
|
||||||
import {Alias} from "~/server-types"
|
import {Alias} from "~/server-types"
|
||||||
import parseAlias from "~/apis/helpers/parse-alias"
|
import {client} from "~/constants/axios-client"
|
||||||
|
|
||||||
export default async function getAliases(): Promise<Array<Alias>> {
|
export default async function getAliases(): Promise<Array<Alias>> {
|
||||||
const {data} = await axios.get(
|
const {data} = await client.get(
|
||||||
`${import.meta.env.VITE_SERVER_BASE_URL}/alias`,
|
`${import.meta.env.VITE_SERVER_BASE_URL}/alias`,
|
||||||
)
|
)
|
||||||
|
|
||||||
return data.map(parseAlias)
|
return data
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import axios from "axios"
|
|
||||||
|
|
||||||
import {ServerSettings} from "~/server-types"
|
import {ServerSettings} from "~/server-types"
|
||||||
|
import {client} from "~/constants/axios-client"
|
||||||
|
|
||||||
export default async function getServerSettings(): Promise<ServerSettings> {
|
export default async function getServerSettings(): Promise<ServerSettings> {
|
||||||
return (await axios.get(`${import.meta.env.VITE_SERVER_BASE_URL}/settings`))
|
return (
|
||||||
.data
|
await client.get(`${import.meta.env.VITE_SERVER_BASE_URL}/settings`)
|
||||||
|
).data
|
||||||
}
|
}
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
import {Alias} from "~/server-types"
|
|
||||||
|
|
||||||
export default function parseAlias(alias: any): Alias {
|
|
||||||
return {
|
|
||||||
id: alias.id,
|
|
||||||
domain: alias.domain,
|
|
||||||
local: alias.local,
|
|
||||||
isActive: alias.is_active,
|
|
||||||
encryptedNotes: alias.encrypted_notes,
|
|
||||||
removeTrackers: alias.remove_trackers,
|
|
||||||
createMailReport: alias.create_mail_report,
|
|
||||||
proxyImages: alias.proxy_images,
|
|
||||||
imageProxyFormat: alias.image_proxy_format,
|
|
||||||
imageProxyUserAgent: alias.image_proxy_user_agent,
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,19 +2,7 @@ import {User} from "~/server-types"
|
|||||||
|
|
||||||
export default function parseUser(user: any): User {
|
export default function parseUser(user: any): User {
|
||||||
return {
|
return {
|
||||||
id: user.id,
|
...user,
|
||||||
createdAt: new Date(user.created_at),
|
createdAt: new Date(user.createdAt),
|
||||||
email: {
|
|
||||||
address: user.email.address,
|
|
||||||
isVerified: user.email.is_verified,
|
|
||||||
},
|
|
||||||
preferences: {
|
|
||||||
aliasRemoveTrackers: user.preferences.alias_remove_trackers,
|
|
||||||
aliasCreateMailReport: user.preferences.alias_create_mail_report,
|
|
||||||
aliasProxyImages: user.preferences.alias_proxy_images,
|
|
||||||
aliasImageProxyFormat: user.preferences.alias_image_proxy_format,
|
|
||||||
aliasImageProxyUserAgent:
|
|
||||||
user.preferences.alias_image_proxy_user_agent,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,8 @@ export * from "./get-server-settings"
|
|||||||
export {default as getServerSettings} from "./get-server-settings"
|
export {default as getServerSettings} from "./get-server-settings"
|
||||||
export * from "./signup"
|
export * from "./signup"
|
||||||
export {default as signup} from "./signup"
|
export {default as signup} from "./signup"
|
||||||
export * from "./validate-email"
|
export * from "./verify-email"
|
||||||
export {default as validateEmail} from "./validate-email"
|
export {default as verifyEmail} from "./verify-email"
|
||||||
export * from "./resend-email-verification-code"
|
export * from "./resend-email-verification-code"
|
||||||
export {default as resendEmailVerificationCode} from "./resend-email-verification-code"
|
export {default as resendEmailVerificationCode} from "./resend-email-verification-code"
|
||||||
export * from "./refresh-token"
|
export * from "./refresh-token"
|
||||||
@ -16,3 +16,5 @@ export * from "./get-aliases"
|
|||||||
export {default as getAliases} from "./get-aliases"
|
export {default as getAliases} from "./get-aliases"
|
||||||
export * from "./create-alias"
|
export * from "./create-alias"
|
||||||
export {default as createAlias} from "./create-alias"
|
export {default as createAlias} from "./create-alias"
|
||||||
|
export * from "./update-account"
|
||||||
|
export {default as updateAccount} from "./update-account"
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import axios from "axios"
|
|
||||||
|
|
||||||
import {MinimumServerResponse} from "~/server-types"
|
import {MinimumServerResponse} from "~/server-types"
|
||||||
|
import {client} from "~/constants/axios-client"
|
||||||
|
|
||||||
export default async function logout(): Promise<MinimumServerResponse> {
|
export default async function logout(): Promise<MinimumServerResponse> {
|
||||||
const {data} = await axios.post(
|
const {data} = await client.post(
|
||||||
`${import.meta.env.VITE_SERVER_BASE_URL}/auth/logout`,
|
`${import.meta.env.VITE_SERVER_BASE_URL}/auth/logout`,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import axios from "axios"
|
|
||||||
|
|
||||||
import {User} from "~/server-types"
|
import {User} from "~/server-types"
|
||||||
|
import {client} from "~/constants/axios-client"
|
||||||
|
|
||||||
export interface RefreshTokenResult {
|
export interface RefreshTokenResult {
|
||||||
user: User
|
user: User
|
||||||
@ -12,7 +11,7 @@ export const REFRESH_TOKEN_URL = `${
|
|||||||
}/api/refresh-token`
|
}/api/refresh-token`
|
||||||
|
|
||||||
export default async function refreshToken(): Promise<RefreshTokenResult> {
|
export default async function refreshToken(): Promise<RefreshTokenResult> {
|
||||||
const {data} = await axios.post(REFRESH_TOKEN_URL)
|
const {data} = await client.post(REFRESH_TOKEN_URL)
|
||||||
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
import axios from "axios"
|
|
||||||
|
|
||||||
import {MinimumServerResponse} from "~/server-types"
|
import {MinimumServerResponse} from "~/server-types"
|
||||||
|
import {client} from "~/constants/axios-client"
|
||||||
|
|
||||||
export default async function resendEmailVerificationCode(
|
export default async function resendEmailVerificationCode(
|
||||||
email: string,
|
email: string,
|
||||||
): Promise<MinimumServerResponse> {
|
): Promise<MinimumServerResponse> {
|
||||||
const {data} = await axios.post(
|
const {data} = await client.post(
|
||||||
`${import.meta.env.VITE_SERVER_BASE_URL}/auth/resend-email`,
|
`${import.meta.env.VITE_SERVER_BASE_URL}/auth/resend-email`,
|
||||||
{
|
{
|
||||||
email,
|
email,
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import axios from "axios"
|
import {client} from "~/constants/axios-client"
|
||||||
|
|
||||||
export interface SignupResult {
|
export interface SignupResult {
|
||||||
normalized_email: string
|
normalized_email: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function signup(email: string): Promise<SignupResult> {
|
export default async function signup(email: string): Promise<SignupResult> {
|
||||||
const {data} = await axios.post(
|
const {data} = await client.post(
|
||||||
`${import.meta.env.VITE_SERVER_BASE_URL}/auth/signup`,
|
`${import.meta.env.VITE_SERVER_BASE_URL}/auth/signup`,
|
||||||
{
|
{
|
||||||
email,
|
email,
|
||||||
|
@ -1,4 +1,24 @@
|
|||||||
|
import {Language} from "~/server-types"
|
||||||
|
import {client} from "~/constants/axios-client"
|
||||||
|
import parseUser from "~/apis/helpers/parse-user"
|
||||||
|
|
||||||
export interface UpdateAccountData {
|
export interface UpdateAccountData {
|
||||||
password: string
|
password: string
|
||||||
publicKey: string
|
publicKey: string
|
||||||
|
encryptedPrivateKey: string
|
||||||
|
language: Language
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function updateAccount(
|
||||||
|
updateData: Partial<UpdateAccountData>,
|
||||||
|
): Promise<void> {
|
||||||
|
const {data} = await client.patch(
|
||||||
|
`${import.meta.env.VITE_SERVER_BASE_URL}/account`,
|
||||||
|
updateData,
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
...data,
|
||||||
|
user: parseUser(data.user),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,17 @@
|
|||||||
import axios from "axios"
|
|
||||||
|
|
||||||
import {AuthenticationDetails} from "~/server-types"
|
import {AuthenticationDetails} from "~/server-types"
|
||||||
|
import {client} from "~/constants/axios-client"
|
||||||
import parseUser from "~/apis/helpers/parse-user"
|
import parseUser from "~/apis/helpers/parse-user"
|
||||||
|
|
||||||
export interface ValidateEmailData {
|
export interface VerifyEmailData {
|
||||||
email: string
|
email: string
|
||||||
token: string
|
token: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function validateEmail({
|
export default async function verifyEmail({
|
||||||
email,
|
email,
|
||||||
token,
|
token,
|
||||||
}: ValidateEmailData): Promise<AuthenticationDetails> {
|
}: VerifyEmailData): Promise<AuthenticationDetails> {
|
||||||
const {data} = await axios.post(
|
const {data} = await client.post(
|
||||||
`${import.meta.env.VITE_SERVER_BASE_URL}/auth/verify-email`,
|
`${import.meta.env.VITE_SERVER_BASE_URL}/auth/verify-email`,
|
||||||
{
|
{
|
||||||
email: email,
|
email: email,
|
4
src/constants/axios-client.ts
Normal file
4
src/constants/axios-client.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import applyCaseMiddleware from "axios-case-converter"
|
||||||
|
import axios from "axios"
|
||||||
|
|
||||||
|
export const client = applyCaseMiddleware(axios.create())
|
@ -9,7 +9,7 @@ import {Grid, Paper, Typography, useTheme} from "@mui/material"
|
|||||||
import {useMutation} from "@tanstack/react-query"
|
import {useMutation} from "@tanstack/react-query"
|
||||||
|
|
||||||
import {AuthenticationDetails, ServerSettings} from "~/server-types"
|
import {AuthenticationDetails, ServerSettings} from "~/server-types"
|
||||||
import {ValidateEmailData, validateEmail} from "~/apis"
|
import {VerifyEmailData, verifyEmail} from "~/apis"
|
||||||
import {useQueryParams} from "~/hooks"
|
import {useQueryParams} from "~/hooks"
|
||||||
import AuthContext from "~/AuthContext/AuthContext"
|
import AuthContext from "~/AuthContext/AuthContext"
|
||||||
|
|
||||||
@ -39,11 +39,11 @@ export default function VerifyEmailRoute(): ReactElement {
|
|||||||
|
|
||||||
return token.split("").every(char => chars.includes(char))
|
return token.split("").every(char => chars.includes(char))
|
||||||
})
|
})
|
||||||
const {mutateAsync: verifyEmail} = useMutation<
|
const {mutateAsync} = useMutation<
|
||||||
AuthenticationDetails,
|
AuthenticationDetails,
|
||||||
AxiosError,
|
AxiosError,
|
||||||
ValidateEmailData
|
VerifyEmailData
|
||||||
>(validateEmail, {
|
>(verifyEmail, {
|
||||||
onSuccess: async ({user}) => {
|
onSuccess: async ({user}) => {
|
||||||
setEmail("")
|
setEmail("")
|
||||||
await login(user, () => navigate("/auth/complete-account"))
|
await login(user, () => navigate("/auth/complete-account"))
|
||||||
@ -53,7 +53,7 @@ export default function VerifyEmailRoute(): ReactElement {
|
|||||||
await emailSchema.validate(email)
|
await emailSchema.validate(email)
|
||||||
await tokenSchema.validate(token)
|
await tokenSchema.validate(token)
|
||||||
|
|
||||||
await verifyEmail({
|
await mutateAsync({
|
||||||
email,
|
email,
|
||||||
token,
|
token,
|
||||||
})
|
})
|
||||||
|
@ -18,6 +18,10 @@ export enum AliasType {
|
|||||||
CUSTOM = "custom",
|
CUSTOM = "custom",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum Language {
|
||||||
|
EN_US = "en_US",
|
||||||
|
}
|
||||||
|
|
||||||
export interface User {
|
export interface User {
|
||||||
id: string
|
id: string
|
||||||
createdAt: Date
|
createdAt: Date
|
||||||
|
Loading…
x
Reference in New Issue
Block a user