Samy
Samy
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
is there any way to get the session data from the site?
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
my app works like, if accounts.google.com opens in the app, it opens the url in the users browser where they have their accounts, after they select their account they redirects to http://localhost:3939/oauth/callback that is a server created from the electron app, and when user redirects to this url, the app opens the api route ${DOMAIN}/api/auth/callback/google for signiin with the data
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
sorry, I don't understand what you meant. this is how im doing it
const currentUrl = generateUrl();

const callbackURL =
typeof window !== "undefined" && window.electron
? "http://localhost:3939/oauth/callback"
: currentUrl;

const { data: userData } = await client.signIn.social(
{
provider,
callbackURL,
},
{
onRequest: () => {
setIsLoading(true);
sendGTMEvent({
event: "Provider Sign-in",
method: provider,
from: "Dialog",
});
},
onSuccess: () => {
setIsLoading(false);
onOpenChange(false);
},
onFailure: () => {
setIsLoading(false);
toast.error("Sign-In failed!", {
description: `Failed to sign in with ${provider}`,
});
sendGTMEvent({
event: "Provider Sign-in Error",
method: provider,
from: "Dialog",
error: "Failed to sign in",
});
},
}
);
const currentUrl = generateUrl();

const callbackURL =
typeof window !== "undefined" && window.electron
? "http://localhost:3939/oauth/callback"
: currentUrl;

const { data: userData } = await client.signIn.social(
{
provider,
callbackURL,
},
{
onRequest: () => {
setIsLoading(true);
sendGTMEvent({
event: "Provider Sign-in",
method: provider,
from: "Dialog",
});
},
onSuccess: () => {
setIsLoading(false);
onOpenChange(false);
},
onFailure: () => {
setIsLoading(false);
toast.error("Sign-In failed!", {
description: `Failed to sign in with ${provider}`,
});
sendGTMEvent({
event: "Provider Sign-in Error",
method: provider,
from: "Dialog",
error: "Failed to sign in",
});
},
}
);
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
but then there is not state in the callback url:
app.get("/oauth/callback", (req, res) => {
const queryParams = new URLSearchParams(req.query).toString();

const newUrl = `${GOOGLE_CALLBACK_URL}?${queryParams}`;
console.log("Redirecting to:", newUrl);

mainWindow.loadURL(newUrl);

res.send("Redirecting...");
});
app.get("/oauth/callback", (req, res) => {
const queryParams = new URLSearchParams(req.query).toString();

const newUrl = `${GOOGLE_CALLBACK_URL}?${queryParams}`;
console.log("Redirecting to:", newUrl);

mainWindow.loadURL(newUrl);

res.send("Redirecting...");
});
2025-03-27T09:51:21.255Z ERROR [Better Auth]: State not found undefined it redirects to to http://localhost:3939/oauth/callback but with the data or state with it
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
is there any way to ignore this error?
2025-03-27T09:28:53.484Z ERROR [Better Auth]: {
error: 'redirect_uri_mismatch',
error_description: 'Bad Request',
status: 400,
statusText: 'Bad Request'
}
2025-03-27T09:28:53.484Z ERROR [Better Auth]: {
error: 'redirect_uri_mismatch',
error_description: 'Bad Request',
status: 400,
statusText: 'Bad Request'
}
because in electron js, I need to change the redirect url to http://localhost:3939/oauth/callback to redirect back from the browser to app
session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
const url = details.url;

if (url.includes("accounts.google.com")) {
let parsedUrl = new URL(url);
parsedUrl.searchParams.set(
"redirect_uri",
"http://localhost:3939/oauth/callback"
);

shell.openExternal(parsedUrl.toString());
callback({ cancel: true });
return;
}
})
session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
const url = details.url;

if (url.includes("accounts.google.com")) {
let parsedUrl = new URL(url);
parsedUrl.searchParams.set(
"redirect_uri",
"http://localhost:3939/oauth/callback"
);

shell.openExternal(parsedUrl.toString());
callback({ cancel: true });
return;
}
})
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
No description
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
it's working now thanks
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
it still not working
export const auth = betterAuth({
database: mongodbAdapter(client),
secondaryStorage: {
get: async (key) => {
try {
const value = await getCache(key);
return value ? value : null;
} catch (error) {
console.error("Error getting from redis", error);
return null;
}
},
set: async (key, value, ttl) => {
try {
if (ttl) await setCache(key, value, { EX: ttl });
else await setCache(key, value, {});
} catch (error) {
console.error("Error setting to redis", error);
}
},
delete: async (key) => {
try {
await delCache(key);
} catch (error) {
console.error("Error deleting from redis", error);
}
},
},
})

export const getCache = async (key: string): Promise<string | null> => {
try {
if (process.env.REDIS_PASSWORD && process.env.REDIS_URL) {
await connectRedis();

const data = await redis.get(key);

if (data) {
return data;
}
}

return null;
} catch (error) {
console.error("Error getting cache", error);
return null;
}
};
export const auth = betterAuth({
database: mongodbAdapter(client),
secondaryStorage: {
get: async (key) => {
try {
const value = await getCache(key);
return value ? value : null;
} catch (error) {
console.error("Error getting from redis", error);
return null;
}
},
set: async (key, value, ttl) => {
try {
if (ttl) await setCache(key, value, { EX: ttl });
else await setCache(key, value, {});
} catch (error) {
console.error("Error setting to redis", error);
}
},
delete: async (key) => {
try {
await delCache(key);
} catch (error) {
console.error("Error deleting from redis", error);
}
},
},
})

export const getCache = async (key: string): Promise<string | null> => {
try {
if (process.env.REDIS_PASSWORD && process.env.REDIS_URL) {
await connectRedis();

const data = await redis.get(key);

if (data) {
return data;
}
}

return null;
} catch (error) {
console.error("Error getting cache", error);
return null;
}
};
in development If i add the secondary storage and don't provide the the redis url (then it will return null), and try to login, my account is not showing, it still only checking from secondary storage and if I remove secondary storage, it works fine
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
ok thanks
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
I hope you will add it in the future
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
can I make it so, if the session is not available on redis it will search the primary db?
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
no, Im using secondary storage from the beginning, but I did switch to a new server
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
@Ping sorry for pinging but I really need to fix this fast
25 replies
BABetter Auth
Created by Samy on 3/27/2025 in #help
Auto logout
I think the problem is with secondary storage if it didn't find the data in secondary storage, it doesn't check on the db
25 replies