Skip to content

Commit

Permalink
fixed bug with auth button still showing if user signed in with disco…
Browse files Browse the repository at this point in the history
…rd and other minor bugs
  • Loading branch information
MistahSanta committed Sep 3, 2024
1 parent 8344def commit ab30202
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
11 changes: 9 additions & 2 deletions src/components/events/EventCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { type RouterOutputs } from '@src/trpc/shared';
import EventLikeButton from '../EventLikeButton';
import { getServerAuthSession } from '@src/server/auth';
import dynamic from 'next/dynamic';

import { and, eq } from 'drizzle-orm';
const EventTimeAlert = dynamic(() => import('./EventTimeAlert'), {
ssr: false,
});
import AddToCalendarButton from './calendar/AddToCalendarButton';
import AddToCalendarAuthorizedButton from './calendar/AddToCalendarAuthorizedButton';
import { api } from '@src/trpc/server';
import { db } from '@src/server/db';

type EventCardProps = {
event: RouterOutputs['event']['findByFilters']['events'][number];
Expand Down Expand Up @@ -70,7 +71,13 @@ const HorizontalCard = async ({
<EventLikeButton liked={event.liked} eventId={event.id} />

)}
{ session
{ (session && db.query.accounts.findFirst({
where: (accounts) =>
and(
eq(accounts.userId, session.user.id),
eq(accounts.provider, "google"),
) ,
}) !== undefined )
? <AddToCalendarAuthorizedButton event={event} tokens={ await api.account.getToken({ userId: session.user.id, provider:"google"}) } />
: <AddToCalendarButton event={event} /> }

Expand Down
10 changes: 4 additions & 6 deletions src/server/api/routers/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,17 @@ export const calendarRouter = createTRPCRouter({
description: description,
location: location,
start: {
dateTime: startTime.toDateString()
dateTime: startTime.toISOString()
},
end: {
dateTime: endTime.toDateString()
dateTime: endTime.toISOString()
},
}
}, (error, response) => {
}, (error) => {
if (error) {
console.error("Error adding event:", error);
throw error;
} else {
console.log("Event added successfully:", response);
}
}
})

return response
Expand Down
6 changes: 3 additions & 3 deletions src/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const authOptions: NextAuthOptions = {
// There's something wrong with NextAuth where it doesn't automatically save the Google's refresh token ( but it will save Discord refresh token )
// into our database. So we have to manually save refreshToken into the database

if ( account?.provider.toLowerCase() != "google" ) return true
if ( account?.provider.toLowerCase() != "google" ) return account

// Reach here, then we know the user log in with Google
// After the user Sign in, NextAuth will automatically create the session object and user object into our database for us,
Expand All @@ -153,7 +153,7 @@ export const authOptions: NextAuthOptions = {
// database, it's not the end of the world, so we just continue, and hope next time user log in, we are able to save the refresh token
}

return true
return account
}
},

Expand All @@ -168,7 +168,7 @@ export const authOptions: NextAuthOptions = {
authorization: {
url: "https://accounts.google.com/o/oauth2/auth",
params: {
scope: "https://www.googleapis.com/auth/calendar",
scope: "openid https://www.googleapis.com/auth/calendar",
prompt: "consent",
access_type: "offline",
response_type: "code",
Expand Down

0 comments on commit ab30202

Please sign in to comment.