Maqed
Maqed
BABetter Auth
Created by Maqed on 2/11/2025 in #help
Link phone number to existing users
As title says. Is there a way to link phone number to existing users? And after linking the phone number the user can sign in with this phone number.
2 replies
BABetter Auth
Created by Maqed on 2/5/2025 in #help
Can I use Phone Number plugin to just add phone number to existing users?
Basically I just wanna add phone number to existing users. Can I use Phone number plugin to do so or should I implement it myself? because Everytime I verify the phone number. callbackOnVerification return null for the user. Here is my code: client:
await authClient.phoneNumber.verify({
phoneNumber,
code,
disableSession: true,
fetchOptions: {
onError: (ctx) => {
form.setError("code", {
message: `phone-number.OTP.${ctx.error.code}`,
});
},
onSuccess: () => {
setIsDialogOpen(false);
setDialogState("InputNumber");
},
},
});
});
await authClient.phoneNumber.verify({
phoneNumber,
code,
disableSession: true,
fetchOptions: {
onError: (ctx) => {
form.setError("code", {
message: `phone-number.OTP.${ctx.error.code}`,
});
},
onSuccess: () => {
setIsDialogOpen(false);
setDialogState("InputNumber");
},
},
});
});
/lib/auth.ts:
export const auth = betterAuth({
database: prismaAdapter(db, {
provider: "postgresql",
}),
plugins: [
phoneNumber({
sendOTP: ({ phoneNumber, code }, request) => {
// TODO: Implement sending OTP code via Whatsapp
console.log({
phoneNumber,
code,
});
},
callbackOnVerification: async ({ phoneNumber, user }, request) => {
console.log({
user,
});
// await db.user.update({
// where: {
// id: user?.id,
// },
// data: {
// phoneNumber,
// phoneNumberVerified: true,
// },
// });
},
}),
],
});
export const auth = betterAuth({
database: prismaAdapter(db, {
provider: "postgresql",
}),
plugins: [
phoneNumber({
sendOTP: ({ phoneNumber, code }, request) => {
// TODO: Implement sending OTP code via Whatsapp
console.log({
phoneNumber,
code,
});
},
callbackOnVerification: async ({ phoneNumber, user }, request) => {
console.log({
user,
});
// await db.user.update({
// where: {
// id: user?.id,
// },
// data: {
// phoneNumber,
// phoneNumberVerified: true,
// },
// });
},
}),
],
});
11 replies
BABetter Auth
Created by Maqed on 2/2/2025 in #help
Session fetched in the middleware doesn't update on every request.
As shown in the video, After signing in. I can go back to /login page even though I protected it using middleware.ts. I even tried no-storing the session but in vain. Here is middleware.ts:
import createMiddleware from "next-intl/middleware";
import { locales } from "./config";
import { NextRequest, NextResponse } from "next/server";
import {
ONLY_UNAUTHENTICATED_ROUTES,
DEFAULT_UNAUTHENTICATED_REDIRECT,
PROTECTED_ROUTES,
DEFAULT_LOGIN_REDIRECT,
} from "./consts/routes";
import { absoluteURL } from "./lib/utils";

const intlMiddleware = createMiddleware({
// A list of all locales that are supported
locales,

// Used when no locale matches
defaultLocale: "ar",
});

export default async function middleware(req: NextRequest) {
const data = await fetch(absoluteURL("/api/auth/get-session"), {
headers: {
cookie: req.headers.get("cookie") || "",
},
cache: "no-cache",
});
const session = await data.json();

// Remove locale and clean pathname
const localeMatch = req.nextUrl.pathname.match(/^\/(ar|en)(\/|$)/);
const locale = localeMatch ? localeMatch[1] : null;
const pathnameWithoutLocale = locale
? req.nextUrl.pathname.replace(`/${locale}`, "") || "/"
: req.nextUrl.pathname;

const isProtectedRoute = PROTECTED_ROUTES.some((route) =>
pathnameWithoutLocale.startsWith(route),
);

if (isProtectedRoute && !session) {
return NextResponse.redirect(
new URL(DEFAULT_UNAUTHENTICATED_REDIRECT, req.url),
);
}

if (
session &&
ONLY_UNAUTHENTICATED_ROUTES.some((route) =>
pathnameWithoutLocale.startsWith(route),
)
) {
return NextResponse.redirect(new URL(DEFAULT_LOGIN_REDIRECT, req.url));
}

return intlMiddleware(req);
}
import createMiddleware from "next-intl/middleware";
import { locales } from "./config";
import { NextRequest, NextResponse } from "next/server";
import {
ONLY_UNAUTHENTICATED_ROUTES,
DEFAULT_UNAUTHENTICATED_REDIRECT,
PROTECTED_ROUTES,
DEFAULT_LOGIN_REDIRECT,
} from "./consts/routes";
import { absoluteURL } from "./lib/utils";

const intlMiddleware = createMiddleware({
// A list of all locales that are supported
locales,

// Used when no locale matches
defaultLocale: "ar",
});

export default async function middleware(req: NextRequest) {
const data = await fetch(absoluteURL("/api/auth/get-session"), {
headers: {
cookie: req.headers.get("cookie") || "",
},
cache: "no-cache",
});
const session = await data.json();

// Remove locale and clean pathname
const localeMatch = req.nextUrl.pathname.match(/^\/(ar|en)(\/|$)/);
const locale = localeMatch ? localeMatch[1] : null;
const pathnameWithoutLocale = locale
? req.nextUrl.pathname.replace(`/${locale}`, "") || "/"
: req.nextUrl.pathname;

const isProtectedRoute = PROTECTED_ROUTES.some((route) =>
pathnameWithoutLocale.startsWith(route),
);

if (isProtectedRoute && !session) {
return NextResponse.redirect(
new URL(DEFAULT_UNAUTHENTICATED_REDIRECT, req.url),
);
}

if (
session &&
ONLY_UNAUTHENTICATED_ROUTES.some((route) =>
pathnameWithoutLocale.startsWith(route),
)
) {
return NextResponse.redirect(new URL(DEFAULT_LOGIN_REDIRECT, req.url));
}

return intlMiddleware(req);
}
NOTE: same issue occurs when I sign out and I'm in a protected route. I don't get routed to /login for some reason.
45 replies
BABetter Auth
Created by Maqed on 2/2/2025 in #help
Client side session isn't removed after deleting user
No description
4 replies