@@ -3,10 +3,12 @@ import { sign } from 'jsonwebtoken';
3
3
import { NextApiRequest , NextApiResponse } from 'next' ;
4
4
import fetch from 'node-fetch' ;
5
5
6
+ import { getAuthHeader } from '../../lib/oauth' ;
7
+ import prisma from '../../lib/prisma' ;
6
8
import { config } from '../../utils/config' ;
7
- import { DiscordUser } from '../../utils/types' ;
9
+ import { DiscordUser , TrelloMember } from '../../utils/types' ;
8
10
9
- const scope = [ 'identify' ] . join ( ' ' ) ;
11
+ const scope = [ 'identify' , 'role_connections.write' ] . join ( ' ' ) ;
10
12
const REDIRECT_URI = `${ config . appUri } /api/login` ;
11
13
12
14
const OAUTH_QS = new URLSearchParams ( {
@@ -36,7 +38,11 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
36
38
scope
37
39
} ) . toString ( ) ;
38
40
39
- const { access_token = null , token_type = 'Bearer' } = await fetch ( 'https://discord.com/api/oauth2/token' , {
41
+ const {
42
+ access_token = null ,
43
+ refresh_token = null ,
44
+ token_type = 'Bearer'
45
+ } = await fetch ( 'https://discord.com/api/oauth2/token' , {
40
46
headers : { 'Content-Type' : 'application/x-www-form-urlencoded' , 'User-Agent' : 'TacoAuth (https://github.com/trello-talk/TacoAuth, v1.0.0)' } ,
41
47
method : 'POST' ,
42
48
body
@@ -53,6 +59,52 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
53
59
54
60
if ( ! ( 'id' in me ) ) return res . redirect ( OAUTH_URI ) ;
55
61
62
+ const user = await prisma . user . upsert ( {
63
+ where : { userID : me . id } ,
64
+ create : {
65
+ userID : me . id ,
66
+ discordToken : access_token ,
67
+ discordRefresh : refresh_token
68
+ } ,
69
+ update : {
70
+ discordToken : access_token ,
71
+ discordRefresh : refresh_token
72
+ }
73
+ } ) ;
74
+
75
+ let trelloMember : TrelloMember | null = null ;
76
+
77
+ if ( user . trelloID && user . trelloToken ) {
78
+ const params = new URLSearchParams ( {
79
+ fields : [ 'id' , 'username' , 'fullName' , 'avatarUrl' , 'initials' , 'url' ] . join ( ',' )
80
+ } ) ;
81
+
82
+ trelloMember = await fetch ( `https://api.trello.com/1/members/me/?${ params . toString ( ) } ` , {
83
+ headers : { Authorization : await getAuthHeader ( user . trelloToken , 'GET' , 'https://api.trello.com/1/members/me' ) }
84
+ } )
85
+ . then ( ( res ) => res . json ( ) as unknown as TrelloMember )
86
+ . catch ( ( ) => null ) ;
87
+ }
88
+
89
+ const memberConnected = trelloMember && ! ! trelloMember . id && ! ! trelloMember . username ;
90
+ await fetch ( `https://discord.com/api/users/@me/applications/${ config . clientId } /role-connection` , {
91
+ method : 'PUT' ,
92
+ headers : {
93
+ Authorization : `${ token_type } ${ access_token } ` ,
94
+ 'User-Agent' : 'TacoAuth (https://github.com/trello-talk/TacoAuth, v1.0.0)' ,
95
+ 'Content-Type' : 'application/json'
96
+ } ,
97
+ body : JSON . stringify (
98
+ memberConnected
99
+ ? {
100
+ platform_name : 'Trello' ,
101
+ platform_username : trelloMember . username ,
102
+ metadata : { connected : true }
103
+ }
104
+ : { metadata : { connected : false } }
105
+ )
106
+ } ) ;
107
+
56
108
const token = sign ( me , config . jwtSecret , { expiresIn : '7d' } ) ;
57
109
58
110
res . setHeader (
0 commit comments