Skip to content

Commit ae3bef2

Browse files
PrincekumarofficialSoniHarsh1JXP25web-team-iiti
committed
Recruiter login r- fix bugs and add recaptcha (#272)
* fix the headings of the Job Table in Recruiter View (#267) * Revert "Add Sentry integration for error tracking and monitoring" (#251) * Revert "Add Sentry integration for error tracking and monitoring (#244)" This reverts commit 5a603ee. * fix the headings of the Job Table in Recruiter View (#267) --------- Co-authored-by: Jai Pannu <142983705+JaiPannu-IITI@users.noreply.github.com> Co-authored-by: Harsh Soni <98403303+SoniHarsh1@users.noreply.github.com> * make login components robust * fixed bugs in login and add recaptcha * remove redundant status check for rate limiting in login component --------- Co-authored-by: Harsh Soni <98403303+SoniHarsh1@users.noreply.github.com> Co-authored-by: Jai Pannu <142983705+JaiPannu-IITI@users.noreply.github.com> Co-authored-by: Web Team <95338452+web-team-iiti@users.noreply.github.com>
1 parent b572c00 commit ae3bef2

2 files changed

Lines changed: 50 additions & 40 deletions

File tree

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,73 @@
1-
import React from "react";
1+
"use client";
2+
3+
import React, { useRef, useState } from "react";
24
import { Button } from "@/components/ui/button";
3-
import { Mail, Send } from "lucide-react";
5+
import { Send } from "lucide-react";
46
import toast from "react-hot-toast";
57
import { loginWithEmail } from "@/helpers/api";
68
import { handleApiError } from "@/utils/errorHandling";
9+
import ReCAPTCHA from "react-google-recaptcha";
10+
11+
export const LoginWithEmail = ({ email }: { email: string }) => {
12+
const [captchaToken, setCaptchaToken] = useState<string>("");
13+
const [loading, setLoading] = useState(false);
14+
const recaptchaRef = useRef<ReCAPTCHA | null>(null);
715

8-
export const LoginWithEmail = (params: { email: string }) => {
916
const onClick = async () => {
1017
try {
11-
const { email } = params;
12-
1318
if (!email || !email.includes("@")) {
1419
toast.error("Please enter a valid email address");
1520
return;
1621
}
1722

18-
console.log(email);
19-
const response = await loginWithEmail(email);
23+
if (!captchaToken) {
24+
toast.error("Please complete the captcha");
25+
return;
26+
}
27+
28+
setLoading(true);
29+
const response = await loginWithEmail(email, captchaToken);
2030

21-
if (response) {
31+
console.log("Login response:", response);
32+
33+
if (
34+
response?.statusCode === 429 ||
35+
response?.message?.includes("Too Many Requests")
36+
) {
37+
toast.error("Too many requests. Please wait and try again.");
38+
} else if (response.success) {
2239
toast.success("Login link sent to your email!");
2340
} else {
2441
toast.error("Unable to send login link. Please try again.");
2542
}
2643

2744
return response;
2845
} catch (error: any) {
29-
console.error("Login email error:", error);
30-
31-
// Handle rate limiting specifically
32-
if (
33-
error?.response?.status === 429 ||
34-
error?.status === 429 ||
35-
error?.statusCode === 429
36-
) {
37-
toast.error(
38-
"Too many requests. Please wait a moment before trying again.",
39-
);
40-
} else if (
41-
error?.response?.data?.message?.includes("Too Many Requests") ||
42-
error?.message?.includes("Too Many Requests")
43-
) {
44-
toast.error(
45-
"Too many requests. Please wait a moment before trying again.",
46-
);
47-
} else {
48-
toast.error("An error occurred while sending the email");
49-
}
46+
handleApiError(error, "An error occurred while sending the email");
5047
return false;
48+
} finally {
49+
setLoading(false);
50+
setCaptchaToken("");
51+
recaptchaRef.current?.reset();
5152
}
5253
};
5354

5455
return (
55-
<Button
56-
onClick={onClick}
57-
className="w-full h-12 bg-slate-700 hover:bg-slate-800 text-white font-medium rounded-lg transition-colors duration-200"
58-
disabled={!params.email || !params.email.includes("@")}
59-
>
60-
<Send className="mr-2 h-4 w-4" />
61-
Send Login Link
62-
</Button>
56+
<div className="space-y-4">
57+
<ReCAPTCHA
58+
ref={recaptchaRef}
59+
sitekey={process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY || ""}
60+
onChange={(token) => setCaptchaToken(token || "")}
61+
/>
62+
63+
<Button
64+
onClick={onClick}
65+
disabled={!email || !email.includes("@") || !captchaToken || loading}
66+
className="w-full h-12 bg-slate-700 hover:bg-slate-800 text-white font-medium rounded-lg transition-colors duration-200"
67+
>
68+
<Send className="mr-2 h-4 w-4" />
69+
{loading ? "Sending..." : "Send Login Link"}
70+
</Button>
71+
</div>
6372
);
6473
};

src/helpers/api.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export const apiCall = async (
8484
const res = await fetch(requestUrl, req);
8585
if (method === "GET" || recieveResponse) {
8686
if (res.ok) return await res.json();
87-
else throw new Error("Cannot fetch");
87+
else return res.json();
8888
} else return res.ok;
8989
};
9090

@@ -600,11 +600,12 @@ export const deleteEvent = async (jobId: string, eventId: string) => {
600600
}
601601
};
602602

603-
export const loginWithEmail = async (email: string) => {
603+
export const loginWithEmail = async (email: string, token: string) => {
604604
return apiCall("/auth/passwordless", {
605605
method: "POST",
606-
body: { email },
606+
body: { email, token },
607607
isAuth: false,
608+
recieveResponse: true,
608609
});
609610
};
610611

0 commit comments

Comments
 (0)