🐛 Fix handle incorrect 6 digit code

This commit is contained in:
Luke Vella 2023-10-23 16:09:44 +01:00
parent cdcd2a2305
commit ac1d844b7a
2 changed files with 20 additions and 9 deletions

View file

@ -7,6 +7,7 @@ import React from "react";
import { useForm } from "react-hook-form";
import { createGlobalState } from "react-use";
import { absoluteUrl } from "@/utils/absolute-url";
import { usePostHog } from "@/utils/posthog";
import { trpc } from "@/utils/trpc/client";
@ -16,10 +17,11 @@ import { TextInput } from "../text-input";
export const useDefaultEmail = createGlobalState("");
const verifyCode = async (options: { email: string; token: string }) => {
const res = await fetch(
"/api/auth/callback/email?" + new URLSearchParams(options),
);
return res.status === 200;
const url = absoluteUrl("/api/auth/callback/email", options);
const res = await fetch(url);
return !res.url.includes("auth/error");
};
export const VerifyCode: React.FunctionComponent<{

View file

@ -15,12 +15,21 @@ function joinPath(baseUrl: string, subpath = "") {
return baseUrl;
}
export function objectToQueryString(obj: Record<string, string>) {
const parts = [];
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key];
if (value !== undefined) {
parts.push(encodeURIComponent(key) + "=" + encodeURIComponent(value));
}
}
}
return parts.join("&");
}
export function absoluteUrl(subpath = "", query?: Record<string, string>) {
const queryString = query
? `?${Object.entries(query)
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
.join("&")}`
: "";
const queryString = query ? `?${objectToQueryString(query)}` : "";
const baseUrl =
process.env.NEXT_PUBLIC_BASE_URL ??
getVercelUrl() ??