Skip to content

Commit 332e237

Browse files
manishrcbalazsorban44adambrgmn
authored
feat(provider): add Dropbox (#1756)
Co-authored-by: Balázs Orbán <[email protected]> Co-authored-by: Adam Bergman <[email protected]>
1 parent 2fce08c commit 332e237

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

src/providers/dropbox.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @param {import("../server").Provider} options
3+
* @example
4+
*
5+
* ```js
6+
* // pages/api/auth/[...nextauth].js
7+
* import Providers from `next-auth/providers`
8+
* ...
9+
* providers: [
10+
* Providers.Dropbox({
11+
* clientId: process.env.DROPBOX_CLIENT_ID,
12+
* clientSecret: process.env.DROPBOX_CLIENT_SECRET
13+
* })
14+
* ]
15+
* ...
16+
*
17+
* // pages/index
18+
* import { signIn } from "next-auth/client"
19+
* ...
20+
* <button onClick={() => signIn("dropbox")}>
21+
* Sign in
22+
* </button>
23+
* ...
24+
* ```
25+
* *Resources:*
26+
* - [NextAuth.js Documentation](https://next-auth.js.org/providers/dropbox)
27+
* - [Dropbox Documentation](https://developers.dropbox.com/oauth-guide)
28+
* - [Configuration](https://www.dropbox.com/developers/apps)
29+
*/
30+
export default function Dropbox(options) {
31+
return {
32+
id: 'dropbox',
33+
name: 'Dropbox',
34+
type: 'oauth',
35+
version: '2.0',
36+
scope: 'account_info.read',
37+
params: { grant_type: 'authorization_code' },
38+
accessTokenUrl: 'https://api.dropboxapi.com/oauth2/token',
39+
authorizationUrl:
40+
'https://www.dropbox.com/oauth2/authorize?token_access_type=offline&response_type=code',
41+
profileUrl: 'https://api.dropboxapi.com/2/users/get_current_account',
42+
profile: (profile) => {
43+
return {
44+
id: profile.account_id,
45+
name: profile.name.display_name,
46+
email: profile.email,
47+
image: profile.profile_photo_url,
48+
email_verified: profile.email_verified
49+
}
50+
},
51+
protection: ["state", "pkce"],
52+
...options
53+
}
54+
}

src/server/lib/oauth/client.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ async function getOAuth2AccessToken (code, provider, codeVerifier) {
215215
*/
216216
async function getOAuth2 (provider, accessToken, results) {
217217
let url = provider.profileUrl
218+
let httpMethod = 'GET'
218219
const headers = { ...provider.headers }
219220

220221
if (this._useAuthorizationHeaderForGET) {
@@ -238,8 +239,15 @@ async function getOAuth2 (provider, accessToken, results) {
238239
url = prepareProfileUrl({ provider, url, results })
239240
}
240241

242+
/** Dropbox requires POST instead of GET
243+
* Read more: https://www.dropbox.com/developers/reference/auth-types#user
244+
*/
245+
if (provider.id === 'dropbox') {
246+
httpMethod = 'POST'
247+
}
248+
241249
return new Promise((resolve, reject) => {
242-
this._request('GET', url, headers, null, accessToken, (error, profileData) => {
250+
this._request(httpMethod, url, headers, null, accessToken, (error, profileData) => {
243251
if (error) {
244252
return reject(error)
245253
}

types/providers.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export type OAuthProviderType =
6464
| "Bungie"
6565
| "Cognito"
6666
| "Discord"
67+
| "Dropbox"
6768
| "EVEOnline"
6869
| "Facebook"
6970
| "FACEIT"

www/docs/providers/dropbox.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
id: dropbox
3+
title: Dropbox
4+
---
5+
6+
## Documentation
7+
8+
https://developers.dropbox.com/oauth-guide
9+
10+
## Configuration
11+
12+
https://www.dropbox.com/developers/apps
13+
14+
## Example
15+
16+
```js
17+
import Providers from `next-auth/providers`
18+
...
19+
providers: [
20+
Providers.Dropbox({
21+
clientId: process.env.DROPBOX_CLIENT_ID,
22+
clientSecret: process.env.DROPBOX_CLIENT_SECRET
23+
})
24+
]
25+
...
26+
```

0 commit comments

Comments
 (0)