oopsie daisy

This commit is contained in:
Omer Sabic 2024-06-11 21:37:50 +02:00
parent ffdf58e818
commit 72fec82ff6

View File

@ -15,15 +15,15 @@ import { createCustomer } from '../utils/stripe.js';
* @param {() => void} done * @param {() => void} done
*/ */
export const authRoutes = (fastify, _, done) => { export const authRoutes = (fastify, _, done) => {
fastify.get("/google/callback", async (request, response) => { fastify.get("/google/callback", async (request, reply) => {
let token = ""; let token = "";
try { try {
/** @type {{token: import('@fastify/oauth2').Token}} */ /** @type {{token: import('@fastify/oauth2').Token}} */
const response = await fastify.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request); const token_response = await fastify.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request);
token = response.token; token = token_response.token;
const user_info = await getUserInfo(token.access_token); const user_info = await getUserInfo(token.access_token);
if (!user_info.verified_email) { if (!user_info.verified_email) {
response.status(400).send({ success: false, message: "Provider email is not verified" }); reply.status(400).send({ success: false, message: "Provider email is not verified" });
return; return;
} }
const channel_info = await getChannelInfo(token.access_token); const channel_info = await getChannelInfo(token.access_token);
@ -64,7 +64,7 @@ export const authRoutes = (fastify, _, done) => {
if (user.length == 0) { if (user.length == 0) {
response.status(400).send({ success: false, message: "Problem when creating user account" }); reply.status(400).send({ success: false, message: "Problem when creating user account" });
return; return;
} }
} }
@ -82,7 +82,7 @@ export const authRoutes = (fastify, _, done) => {
else { else {
let session_info = await tx.select().from(users).leftJoin(sessions, eq(sessions.user_id, users.id)).where(eq(users.google_id, user_info.id)); let session_info = await tx.select().from(users).leftJoin(sessions, eq(sessions.user_id, users.id)).where(eq(users.google_id, user_info.id));
if (!session_info || session_info.length == 0) { if (!session_info || session_info.length == 0) {
response.status(400).send({ success: false, message: "Problem when creating user account" }); reply.status(400).send({ success: false, message: "Problem when creating user account" });
await tx.rollback(); await tx.rollback();
return; return;
} }
@ -104,7 +104,7 @@ export const authRoutes = (fastify, _, done) => {
// domain: ".omersabic.com" // domain: ".omersabic.com"
// }).redirect(env.FRONTEND_URL); // }).redirect(env.FRONTEND_URL);
response.redirect(env.FRONTEND_URL + "/auth?token=" + session_id); reply.redirect(env.FRONTEND_URL + "/auth?token=" + session_id);
// response.send({ // response.send({
// token: session_id // token: session_id
@ -112,11 +112,11 @@ export const authRoutes = (fastify, _, done) => {
return; return;
} catch (e) { } catch (e) {
if(e.message === "no_channel") { if(e.message === "no_channel") {
response.status(400).send("Your account does not have a youtube channel. Please make one.") reply.status(400).send("Your account does not have a youtube channel. Please make one.")
} }
// fastify.googleOAuth2.revokeToken(token, "refresh_token"); // fastify.googleOAuth2.revokeToken(token, "refresh_token");
console.log(e); console.log(e);
response.send({ success: false, message: "There was a problem when making your account." }); reply.send({ success: false, message: "There was a problem when making your account." });
return; return;
} }
}); });