1
- import {
2
- BadRequestException ,
3
- HttpException ,
4
- UnauthorizedException ,
5
- } from "nextlove"
1
+ import { BadRequestException , HttpException } from "nextlove"
6
2
import { z } from "zod"
7
3
8
4
import { withRouteSpec } from "lib/middleware/index.ts"
9
5
10
6
import { client_session } from "lib/zod/client_session.ts"
11
7
12
8
export default withRouteSpec ( {
13
- auth : "none" ,
9
+ auth : [
10
+ "api_key" ,
11
+ "pat_with_workspace" ,
12
+ "console_session_with_workspace" ,
13
+ "publishable_key" ,
14
+ ] ,
14
15
methods : [ "POST" , "PUT" ] ,
15
16
middlewares : [ ] ,
16
- jsonBody : z . union ( [
17
- z . any ( ) ,
18
- z
19
- . object ( {
20
- connected_account_ids : z . array ( z . string ( ) ) . optional ( ) ,
21
- connect_webview_ids : z . array ( z . string ( ) ) . optional ( ) ,
22
- user_identifier_key : z . string ( ) . optional ( ) ,
23
- } )
24
- . optional ( ) ,
25
- ] ) ,
17
+ jsonBody : z . object ( {
18
+ connected_account_ids : z . array ( z . string ( ) ) . optional ( ) ,
19
+ connect_webview_ids : z . array ( z . string ( ) ) . optional ( ) ,
20
+ user_identifier_key : z . string ( ) . optional ( ) ,
21
+ } ) ,
26
22
jsonResponse : z . object ( {
27
23
client_session,
28
24
ok : z . literal ( true ) ,
29
25
} ) ,
30
26
} as const ) ( async ( req , res ) => {
31
- const user_identifier_key =
32
- req . body ?. user_identifier_key ??
33
- ( req . headers [ "user-identifier-key" ] as string | undefined ) ??
34
- ( req . headers [ "seam-user-identifier-key" ] as string | undefined )
35
-
36
- const publishable_key = req . headers [ "seam-publishable-key" ] as
37
- | string
38
- | undefined
39
-
40
- const token =
41
- req . headers [ "seam-api-key" ] ??
42
- req . headers . authorization ?. split ( "Bearer " ) ?. [ 1 ]
43
-
44
- if ( publishable_key == null && token == null ) {
45
- throw new BadRequestException ( {
46
- type : "seam_api_or_publishable_key_header_required" ,
47
- message : "Seam-Api-Key or Seam-Publishable-Key header required" ,
48
- } )
49
- }
50
-
51
- if ( publishable_key != null && user_identifier_key == null ) {
52
- throw new UnauthorizedException ( {
53
- type : "missing_user_identifier_key" ,
54
- message :
55
- "You must provide a user_identifier_key when using a publishable key" ,
56
- } )
57
- }
58
-
59
- if ( token == null && user_identifier_key == null ) {
60
- throw new UnauthorizedException ( {
61
- type : "missing_user_identifier_key" ,
62
- message : "You must provide a user_identifier_key when using an api key" ,
63
- } )
64
- }
65
-
66
- const { connect_webview_ids, connected_account_ids } = req . body
27
+ const { connect_webview_ids, connected_account_ids, user_identifier_key } =
28
+ req . body
67
29
68
30
if (
69
- publishable_key != null &&
31
+ req . auth . type === "publishable_key" &&
70
32
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
71
33
( connect_webview_ids != null || connected_account_ids != null )
72
34
) {
@@ -77,39 +39,11 @@ export default withRouteSpec({
77
39
} )
78
40
}
79
41
80
- let workspace_id : string | null = null
81
- let api_key
82
- if ( token != null && publishable_key == null ) {
83
- api_key = req . db . api_keys . find ( ( a ) => a . token === token )
84
-
85
- if ( api_key == null ) {
86
- throw new BadRequestException ( {
87
- type : "invalid_api_key" ,
88
- message : "Invalid api key" ,
89
- } )
90
- }
91
- workspace_id = api_key . workspace_id
92
- }
93
-
94
- if ( publishable_key != null && token == null ) {
95
- const workspace = req . db . workspaces . find (
96
- ( w ) => w . publishable_key === publishable_key ,
97
- )
98
-
99
- if ( workspace == null ) {
100
- throw new BadRequestException ( {
101
- type : "Workspace not found" ,
102
- message : "Workspace not found" ,
103
- } )
104
- }
105
- workspace_id = workspace . workspace_id
106
- }
107
-
108
42
if ( user_identifier_key != null ) {
109
43
const existing_cs = req . db . client_sessions . find (
110
44
( cst ) =>
111
45
cst . user_identifier_key === user_identifier_key &&
112
- cst . workspace_id === workspace_id ,
46
+ cst . workspace_id === req . auth . workspace_id ,
113
47
)
114
48
115
49
if ( existing_cs != null ) {
@@ -128,20 +62,16 @@ export default withRouteSpec({
128
62
}
129
63
}
130
64
131
- if ( workspace_id == null ) {
132
- throw new BadRequestException ( {
133
- type : "workspace_id_not_found" ,
134
- message : "Workspace id not found" ,
135
- } )
136
- }
137
-
138
65
const client_session = req . db . addClientSession ( {
139
- workspace_id,
66
+ workspace_id : req . auth . workspace_id ,
140
67
connect_webview_ids,
141
68
connected_account_ids,
142
69
user_identifier_key,
143
- publishable_key,
144
- api_key_id : api_key ?. api_key_id ,
70
+ publishable_key :
71
+ req . auth . type === "publishable_key"
72
+ ? req . auth . publishable_key
73
+ : undefined ,
74
+ api_key_id : req . auth . type === "api_key" ? req . auth . api_key_id : undefined ,
145
75
} )
146
76
const device_count = req . db . devices . filter (
147
77
( d ) =>
0 commit comments