-
Notifications
You must be signed in to change notification settings - Fork 84
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
Showing
19 changed files
with
486 additions
and
287 deletions.
There are no files selected for viewing
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,17 @@ | ||
interface ButtonProps { | ||
onClick?(): any; | ||
} | ||
|
||
export default function DropboxButton({ onClick }: ButtonProps) { | ||
return ( | ||
<button | ||
onClick={onClick} | ||
className="shadow flex items-center justify-center font-roboto transition-colors py-2 px-4 gap-4 text-neutral-500 bg-white rounded active:bg-neutral-200 hover:ring-2 focus:outline-none" | ||
> | ||
<svg xmlns="http://www.w3.org/2000/svg" className="w-6 h-6" viewBox="0 0 256 218"> | ||
<path fill="#0061FF" d="M63.995 0L0 40.771l63.995 40.772L128 40.771zM192 0l-64 40.775l64 40.775l64.001-40.775zM0 122.321l63.995 40.772L128 122.321L63.995 81.55zM192 81.55l-64 40.775l64 40.774l64-40.774zM64 176.771l64.005 40.772L192 176.771L128.005 136z"/> | ||
Check warning on line 12 in apps/dashboard/components/dropboxButton.tsx GitHub Actions / Lint source code
|
||
</svg> | ||
<span className="font-semibold">Sign in with Dropbox</span> | ||
</button> | ||
); | ||
} |
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,7 @@ | ||
export default function DropboxLogo(props) { | ||
return ( | ||
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 256 218" {...props}> | ||
<path fill="#0061FF" d="M63.995 0L0 40.771l63.995 40.772L128 40.771zM192 0l-64 40.775l64 40.775l64.001-40.775zM0 122.321l63.995 40.772L128 122.321L63.995 81.55zM192 81.55l-64 40.775l64 40.774l64-40.774zM64 176.771l64.005 40.772L192 176.771L128.005 136z"/> | ||
Check warning on line 4 in apps/dashboard/components/svg/dropbox.tsx GitHub Actions / Lint source code
|
||
</svg> | ||
); | ||
} |
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,18 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import prisma from '../../../lib/prisma'; | ||
import { parseUser } from '../../../utils'; | ||
import { setNextAvailableService } from '../../../utils/prisma'; | ||
|
||
export default async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.method !== 'GET') return res.redirect('/'); | ||
const user = parseUser(req); | ||
if (!user) return res.redirect('/'); | ||
|
||
const dbUser = await prisma.user.findUnique({ where: { id: user.id } }); | ||
await setNextAvailableService(dbUser, 'dropbox'); | ||
|
||
await prisma.dropboxUser.delete({ where: { id: user.id } }); | ||
|
||
res.redirect('/?r=dropbox_unlinked'); | ||
}; |
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,61 @@ | ||
import { Dropbox, DropboxAuth } from 'dropbox'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import prisma from '../../../lib/prisma'; | ||
import { parseUser } from '../../../utils'; | ||
import { config } from '../../../utils/config'; | ||
|
||
const REDIRECT_URI = `${config.appUri}/api/dropbox/oauth`; | ||
const scopes = ['account_info.read', 'files.content.write']; | ||
const auth = new DropboxAuth({ | ||
clientId: config.dropboxClientId, | ||
clientSecret: config.dropboxClientSecret | ||
}); | ||
|
||
export default async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.method !== 'GET') return res.redirect('/'); | ||
const user = parseUser(req); | ||
if (!user) return res.redirect('/'); | ||
const dbUser = await prisma.user.findFirst({ where: { id: user.id } }); | ||
if (!dbUser) return res.redirect('/'); | ||
if (dbUser.rewardTier === 0) return res.redirect('/'); | ||
|
||
const { code = null, error = null } = req.query; | ||
if (error) return res.redirect(`/?error=${req.query.error}&from=dropbox`); | ||
|
||
if (!code || typeof code !== 'string') | ||
return res.redirect((await auth.getAuthenticationUrl(REDIRECT_URI, null, 'code', 'offline', scopes, 'none', false)) as string); | ||
|
||
try { | ||
const response = await auth.getAccessTokenFromCode(REDIRECT_URI, code); | ||
const tokens: { access_token: string; refresh_token: string; scope: string } = response.result as any; | ||
const scopesRecieved = tokens.scope.split(' '); | ||
|
||
if (scopes.find((s) => !scopesRecieved.includes(s))) return res.redirect(`/?error=invalid_scope&from=dropbox`); | ||
|
||
const dbx = new Dropbox({ | ||
accessToken: tokens.access_token, | ||
clientId: config.dropboxClientId, | ||
clientSecret: config.dropboxClientSecret | ||
}); | ||
|
||
const dropboxUser = await dbx.usersGetCurrentAccount(); | ||
|
||
await prisma.dropboxUser.upsert({ | ||
where: { id: user.id }, | ||
update: { token: tokens.access_token, refreshToken: tokens.refresh_token, name: dropboxUser.result.name.display_name }, | ||
create: { id: user.id, token: tokens.access_token, refreshToken: tokens.refresh_token, name: dropboxUser.result.name.display_name } | ||
}); | ||
} catch (e) { | ||
console.log(e) | ||
return res.redirect(`/?error=${encodeURIComponent('Could not get user data, please sign in again.')}&from=dropbox`); | ||
} | ||
|
||
await prisma.user.upsert({ | ||
where: { id: user.id }, | ||
update: { driveService: 'dropbox' }, | ||
create: { id: user.id, driveService: 'dropbox' } | ||
}); | ||
|
||
res.redirect('/?r=dropbox_linked'); | ||
}; |
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
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
Oops, something went wrong.