Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions apps/dashboard/app/auth/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import {
errorMessages,
} from "@/lib/auth/types";
import { requireEmailMatch } from "@/lib/auth/utils";
import { env } from "@/lib/env";
import { Ratelimit } from "@unkey/ratelimit";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

// Authentication Actions
export async function signUpViaEmail(params: UserData): Promise<EmailAuthResult> {
return await auth.signUpViaEmail(params);
Expand Down Expand Up @@ -88,6 +89,23 @@ export async function verifyEmail(code: string): Promise<VerificationResult> {
}

export async function resendAuthCode(email: string): Promise<EmailAuthResult> {
const rl = new Ratelimit({
namespace: "resend_code",
duration: "5m",
limit: 5,
rootKey: env().UNKEY_ROOT_KEY!,
});

const { success } = await rl.limit(email);

if (!success) {
return {
success: false,
code: AuthErrorCode.RATE_ERROR,
message: "Sorry we can't send another code. Please contact support",
};
}

if (!email.trim()) {
return {
success: false,
Expand Down Expand Up @@ -169,7 +187,10 @@ export async function completeOrgSelection(
}

// Call auth provider with token and orgId
const result = await auth.completeOrgSelection({ pendingAuthToken: tempSession.value, orgId });
const result = await auth.completeOrgSelection({
pendingAuthToken: tempSession.value,
orgId,
});

if (result.success) {
cookies().delete(PENDING_SESSION_COOKIE);
Expand Down
6 changes: 5 additions & 1 deletion apps/dashboard/app/auth/hooks/useSignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ export function useSignIn() {

const handleResendCode = async (): Promise<void> => {
try {
await resendAuthCode(context.email);
const result = await resendAuthCode(context.email);
if (!result.success) {
setError(errorMessages[AuthErrorCode.UNKNOWN_ERROR]);
return;
}
} catch (error) {
setError(errorMessages[AuthErrorCode.UNKNOWN_ERROR]);
throw error;
Expand Down
2 changes: 2 additions & 0 deletions apps/dashboard/lib/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export enum AuthErrorCode {
INVALID_EMAIL = "INVALID_EMAIL",
NETWORK_ERROR = "NETWORK_ERROR",
UNKNOWN_ERROR = "UNKNOWN_ERROR",
RATE_ERROR = "RATE_ERROR",
ACCOUNT_NOT_FOUND = "ACCOUNT_NOT_FOUND",
ORGANIZATION_SELECTION_REQUIRED = "ORGANIZATION_SELECTION_REQUIRED",
EMAIL_VERIFICATION_REQUIRED = "EMAIL_VERIFICATION_REQUIRED",
Expand All @@ -213,6 +214,7 @@ export const errorMessages: Record<AuthErrorCode, string> = {
"Email address not verified. Please check your email for a verification code.",
[AuthErrorCode.PENDING_SESSION_EXPIRED]:
"Pending Authentication has expired. Please sign-in again.",
[AuthErrorCode.RATE_ERROR]: "Limited OTP attempts",
};

export interface MiddlewareConfig {
Expand Down