1- import * as jose from "jose" ;
21import { prisma } from "./db" ;
32import express from "express" ;
43import {
@@ -12,45 +11,40 @@ import { authenticated } from "./auth";
1211import { activeConnections } from "./webrtc-signaling" ;
1312
1413export const List = async ( req : express . Request , res : express . Response ) => {
15- const idToken = req . session ?. id_token ;
16- const { iss, sub } = jose . decodeJwt ( idToken ) ;
17-
18- // Authorization server’s identifier for the user
19- const isGoogle = iss === "https://accounts.google.com" ;
20- if ( isGoogle ) {
21- const devices = await prisma . device . findMany ( {
22- where : { user : { googleId : sub } } ,
23- select : { id : true , name : true , lastSeen : true } ,
24- } ) ;
14+ const { subject } = req ;
15+ if ( ! subject ) throw new UnauthorizedError ( "Missing subject in token" ) ;
2516
26- return res . json ( {
27- devices : devices . map ( device => {
28- const activeDevice = activeConnections . get ( device . id ) ;
29- const version = activeDevice ?. [ 2 ] || null ;
30-
31- return {
32- ...device ,
33- online : ! ! activeDevice ,
34- version,
35- } ;
36- } ) ,
37- } ) ;
38- } else {
39- throw new BadRequestError ( "Token is not from Google" ) ;
40- }
17+ const devices = await prisma . device . findMany ( {
18+ where : { user : { googleId : subject } } ,
19+ select : { id : true , name : true , lastSeen : true } ,
20+ } ) ;
21+
22+ return res . json ( {
23+ devices : devices . map ( device => {
24+ const activeDevice = activeConnections . get ( device . id ) ;
25+ const version = activeDevice ?. [ 2 ] || null ;
26+
27+ return {
28+ ...device ,
29+ online : ! ! activeDevice ,
30+ version,
31+ } ;
32+ } ) ,
33+ } ) ;
4134} ;
4235
4336export const Retrieve = async (
4437 req : express . Request < { id : string } > ,
4538 res : express . Response
4639) => {
47- const idToken = req . session ?. id_token ;
48- const { sub } = jose . decodeJwt ( idToken ) ;
40+ const { subject } = req ;
41+ if ( ! subject ) throw new UnauthorizedError ( "Missing subject in token" ) ;
42+
4943 const { id } = req . params ;
5044 if ( ! id ) throw new UnprocessableEntityError ( "Missing device id in params" ) ;
5145
5246 const device = await prisma . device . findUnique ( {
53- where : { id, user : { googleId : sub } } ,
47+ where : { id, user : { googleId : subject } } ,
5448 select : { id : true , name : true , user : { select : { googleId : true } } } ,
5549 } ) ;
5650
@@ -62,18 +56,17 @@ export const Update = async (
6256 req : express . Request < { id : string } > ,
6357 res : express . Response
6458) => {
65- const idToken = req . session ?. id_token ;
66- const { sub } = jose . decodeJwt ( idToken ) ;
67- if ( ! sub ) throw new UnauthorizedError ( "Missing sub in token" ) ;
59+ const { subject } = req ;
60+ if ( ! subject ) throw new UnauthorizedError ( "Missing subject in token" ) ;
6861
6962 const { id } = req . params ;
7063 if ( ! id ) throw new UnprocessableEntityError ( "Missing device id in params" ) ;
7164
7265 const { name } = req . body as { name : string } ;
73- if ( ! name ) throw new UnprocessableEntityError ( "Missing name in body" ) ;
66+ if ( ! name ) throw new UnprocessableEntityError ( "Missing device name in body" ) ;
7467
7568 const device = await prisma . device . update ( {
76- where : { id, user : { googleId : sub } } ,
69+ where : { id, user : { googleId : subject } } ,
7770 data : { name } ,
7871 select : { id : true } ,
7972 } ) ;
@@ -125,14 +118,13 @@ export const Delete = async (
125118 throw new BadRequestError ( "Unauthorized" ) ;
126119 }
127120
128- const idToken = req . session ?. id_token ;
129- const { sub } = jose . decodeJwt ( idToken ) ;
130- if ( ! sub ) throw new UnauthorizedError ( "Missing sub in token" ) ;
121+ const { subject } = req ;
122+ if ( ! subject ) throw new UnauthorizedError ( "Missing subject in token" ) ;
131123
132124 const { id } = req . params ;
133125 if ( ! id ) throw new UnprocessableEntityError ( "Missing device id in params" ) ;
134126
135- await prisma . device . delete ( { where : { id, user : { googleId : sub } } } ) ;
127+ await prisma . device . delete ( { where : { id, user : { googleId : subject } } } ) ;
136128
137129 // We just removed the device, so we should close any running open socket connections
138130 const conn = activeConnections . get ( id ) ;
0 commit comments