-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fe6a6d
commit d5708f5
Showing
12 changed files
with
207 additions
and
39 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
apps/dashboard-app/actions/connections/youtube-connections.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use server' | ||
|
||
import { createYoutube, getYoutubeByAccessToken, getYoutubeByUserId } from '@repo/prisma-db/repo/youtube' | ||
import { get } from 'http' | ||
|
||
|
||
export const onYoutubeConnection = async ({access_token, refresh_token, scopes, userId}: any) => { | ||
if(access_token){ | ||
console.log('access_token in actions', access_token) | ||
const connected = await getYoutubeByAccessToken(access_token) | ||
console.log('connected', connected) | ||
if (!connected){ | ||
const youtube = await createYoutube({access_token, refresh_token, scopes, userId}) | ||
} | ||
} | ||
|
||
} | ||
|
||
export const getYoutubeConnection = async (userId: string) => { | ||
const connection = await getYoutubeByUserId(userId) | ||
return connection | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import axios from 'axios'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { google } from 'googleapis'; | ||
|
||
|
||
export async function GET(req: NextRequest) { | ||
|
||
const oauth2Client = new google.auth.OAuth2( | ||
process.env.NEXT_PUBLIC_YOUTUBE_CLIENT_ID, | ||
process.env.NEXT_PUBLIC_YOUTUBE_CLIENT_SECRET, | ||
process.env.NEXT_PUBLIC_YOUTUBE_REDIRECT_URI | ||
); | ||
|
||
// generate a url that asks permissions for Blogger and Google Calendar scopes | ||
const scopes = [ | ||
'https://www.googleapis.com/auth/youtube', | ||
'https://www.googleapis.com/auth/youtube.force-ssl', | ||
'https://www.googleapis.com/auth/youtube.readonly', | ||
'https://www.googleapis.com/auth/youtube.upload', | ||
]; | ||
|
||
const url = oauth2Client.generateAuthUrl({ | ||
// 'online' (default) or 'offline' (gets refresh_token) | ||
access_type: 'offline', | ||
|
||
// If you only need one scope you can pass it as a string | ||
scope: scopes | ||
}); | ||
return NextResponse.redirect(url); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/prisma-db/prisma/migrations/20240718031435_added_youtube_oauth/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-- AlterTable | ||
ALTER TABLE "connection_schema"."Connections" ADD COLUMN "youtubeId" TEXT; | ||
|
||
-- CreateTable | ||
CREATE TABLE "connection_schema"."Youtube" ( | ||
"id" TEXT NOT NULL, | ||
"accessToken" TEXT NOT NULL, | ||
"refreshToken" TEXT NOT NULL, | ||
"scope" TEXT NOT NULL, | ||
"userId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Youtube_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Youtube_accessToken_key" ON "connection_schema"."Youtube"("accessToken"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Youtube_refreshToken_key" ON "connection_schema"."Youtube"("refreshToken"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "connection_schema"."Youtube" ADD CONSTRAINT "Youtube_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user_schema"."User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "connection_schema"."Connections" ADD CONSTRAINT "Connections_youtubeId_fkey" FOREIGN KEY ("youtubeId") REFERENCES "connection_schema"."Youtube"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
10 changes: 10 additions & 0 deletions
10
packages/prisma-db/prisma/migrations/20240718034932_added_youtube_oauth/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `scope` on the `Youtube` table. All the data in the column will be lost. | ||
- Added the required column `scopes` to the `Youtube` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "connection_schema"."Youtube" DROP COLUMN "scope", | ||
ADD COLUMN "scopes" TEXT NOT NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import db from './index' | ||
|
||
export const createYoutube = async ({access_token,refresh_token,scopes,userId}:any) =>{ | ||
const youtube = await db.youtube.create({ | ||
data:{ | ||
userId: userId, | ||
accessToken: access_token, | ||
refreshToken: refresh_token, | ||
scopes: scopes, | ||
connections: { | ||
create: { | ||
userId: userId, | ||
type: "Youtube" | ||
} | ||
} | ||
} | ||
}) | ||
return youtube | ||
} | ||
|
||
export const getYoutubeByAccessToken = async (access_token: string) => { | ||
if(access_token){ | ||
try { | ||
const connected = await db.youtube.findUnique({ | ||
where:{ | ||
accessToken: access_token, | ||
}, | ||
include:{ | ||
connections: { | ||
select: { | ||
type: true | ||
} | ||
} | ||
} | ||
}) | ||
return connected; | ||
} catch (error) { | ||
return null | ||
} | ||
} | ||
return null | ||
} | ||
|
||
export const getYoutubeByUserId = async (userId: string) => { | ||
const connection = await db.youtube.findFirst({ | ||
where:{ | ||
userId | ||
} | ||
}) | ||
return connection | ||
} |