1- import type { FastifyInstance , FastifyRequest , FastifyReply } from 'fastify' ;
2- import { encrypt } from '../utils/encryption.js' ;
31import { buildOAuthState , getMobileRedirectUri } from '../services/authService.js' ;
2+ import { encrypt } from '../utils/encryption.js' ;
3+
4+ import type {
5+ FastifyInstance ,
6+ FastifyReply ,
7+ FastifyRequest ,
8+ } from 'fastify' ;
49
510const GITHUB_AUTH_URL = 'https://github.com/login/oauth/authorize' ;
611const GITHUB_TOKEN_URL = 'https://github.com/login/oauth/access_token' ;
@@ -14,7 +19,9 @@ interface OAuthCallbackQuery {
1419 state ?: string ;
1520}
1621
17- export async function authRoutes ( app : FastifyInstance ) {
22+ export async function authRoutes (
23+ app : FastifyInstance
24+ ) : Promise < void > {
1825 // Developer login bypass (development only)
1926 if ( process . env . NODE_ENV !== 'production' ) {
2027 app . post ( '/dev-login' , async ( request : FastifyRequest , reply : FastifyReply ) => {
@@ -289,13 +296,34 @@ if (existingUser) {
289296 } ) ;
290297
291298 // Current user
292- app . get ( '/me' , { preHandler : [ async ( request , reply ) => {
293- const server = request . server as any ;
294- if ( typeof server ?. authenticate === 'function' ) { await server . authenticate ( request , reply ) ; return }
295- if ( typeof ( app as any ) . authenticate === 'function' ) { await ( app as any ) . authenticate ( request , reply ) ; return }
296- try { await request . jwtVerify ( ) } catch ( e ) { reply . status ( 401 ) . send ( { error : 'Unauthorized' } ) }
297- } ] } , async ( request : FastifyRequest , reply : FastifyReply ) => {
299+ app . get (
300+ '/me' ,
301+ {
302+ preHandler : [
303+ async ( request , reply ) : Promise < void > => {
304+ const server = request . server as any ;
305+
306+ if ( typeof server ?. authenticate === 'function' ) {
307+ await server . authenticate ( request , reply ) ;
308+ return ;
309+ }
310+
311+ if ( typeof ( app as any ) . authenticate === 'function' ) {
312+ await ( app as any ) . authenticate ( request , reply ) ;
313+ return ;
314+ }
315+
316+ try {
317+ await request . jwtVerify ( ) ;
318+ } catch {
319+ reply . status ( 401 ) . send ( { error : 'Unauthorized' } ) ;
320+ }
321+ } ,
322+ ] ,
323+ } ,
324+ async ( request : FastifyRequest , reply : FastifyReply ) => {
298325 const userId = ( request . user as any ) . id ;
326+
299327 const user = await app . prisma . user . findUnique ( {
300328 where : { id : userId } ,
301329 select : {
@@ -310,20 +338,28 @@ if (existingUser) {
310338 avatarUrl : true ,
311339 accentColor : true ,
312340 createdAt : true ,
313- oauthTokens : { select : { platform : true , scopes : true , createdAt : true } } ,
341+ oauthTokens : {
342+ select : {
343+ platform : true ,
344+ scopes : true ,
345+ createdAt : true ,
346+ } ,
347+ } ,
314348 } ,
315349 } ) ;
316350
317351 if ( ! user ) {
318- return reply . status ( 404 ) . send ( { error : 'User not found' } ) ;
352+ return reply . status ( 404 ) . send ( {
353+ error : 'User not found' ,
354+ } ) ;
319355 }
320356
321357 const { oauthTokens, ...userData } = user ;
322- return { ...userData , connectedPlatforms : oauthTokens } ;
323- } ) ;
324358
325- app . post ( '/logout' , async ( request : FastifyRequest , reply : FastifyReply ) => {
326- reply . clearCookie ( 'token' , { path : '/' } ) ;
327- return { message : 'Logged out' } ;
328- } ) ;
359+ return {
360+ ...userData ,
361+ connectedPlatforms : oauthTokens ,
362+ } ;
363+ }
364+ ) ;
329365}
0 commit comments