@@ -17,6 +17,7 @@ import {
1717 entryTagSchema ,
1818 newEntryTagSchema ,
1919 userSchema ,
20+ grantSchema ,
2021} from './schema.ts'
2122import { sql , snakeToCamel } from './utils.ts'
2223
@@ -34,18 +35,31 @@ export class DB {
3435 return db
3536 }
3637
37- async getUserByToken ( token : string ) {
38+ async createUnclaimedGrant ( grantUserId : string ) {
39+ const insertResult = await this . db
40+ . prepare ( sql `INSERT INTO grants (grant_user_id) VALUES (?1)` )
41+ . bind ( grantUserId )
42+ . run ( )
43+
44+ if ( ! insertResult . success || ! insertResult . meta . last_row_id ) {
45+ throw new Error ( 'Failed to create grant: ' + insertResult . error )
46+ }
47+
48+ return insertResult . meta . last_row_id
49+ }
50+
51+ async getUserByGrantId ( grantId : string ) {
3852 // TODO: I don't have internet, so turn this into a single query when I do...
3953 // also add TAKE 1 or whatever
40- const tokensResult = await this . db
41- . prepare ( sql `SELECT user_id FROM access_tokens WHERE token_value = ?1` )
42- . bind ( token )
54+ const grantsResult = await this . db
55+ . prepare ( sql `SELECT user_id FROM grants WHERE id = ?1` )
56+ . bind ( grantId )
4357 . first ( )
44- if ( ! tokensResult ) return null
58+ if ( ! grantsResult ) return null
4559
4660 const userResult = await this . db
4761 . prepare ( sql `SELECT * FROM users WHERE id = ?1` )
48- . bind ( tokensResult . user_id )
62+ . bind ( grantsResult . user_id )
4963 . first ( )
5064 if ( ! userResult ) return null
5165
@@ -62,35 +76,14 @@ export class DB {
6276 return userSchema . parse ( snakeToCamel ( userResult ) )
6377 }
6478
65- async getAccessTokenIdByValue ( tokenValue : string ) {
66- const tokenResult = await this . db
67- . prepare ( sql `SELECT id FROM access_tokens WHERE token_value = ?1` )
68- . bind ( tokenValue )
79+ async getGrant ( grantId : string ) {
80+ const grantResult = await this . db
81+ . prepare ( sql `SELECT * FROM grants WHERE id = ?1` )
82+ . bind ( grantId )
6983 . first ( )
70- if ( ! tokenResult ) return null
84+ if ( ! grantResult ) return null
7185
72- return tokenResult . id
73- }
74-
75- async createAccessTokenIfNecessary ( tokenValue : string ) {
76- const existingAccessTokenId = await this . getAccessTokenIdByValue ( tokenValue )
77- if ( existingAccessTokenId ) return existingAccessTokenId
78-
79- const insertResult = await this . db
80- . prepare (
81- sql `
82- INSERT INTO access_tokens (token_value)
83- VALUES (?1)
84- ` ,
85- )
86- . bind ( tokenValue )
87- . run ( )
88-
89- if ( ! insertResult . success || ! insertResult . meta . last_row_id ) {
90- throw new Error ( 'Failed to create access token: ' + insertResult . error )
91- }
92-
93- return insertResult . meta . last_row_id
86+ return grantSchema . parse ( snakeToCamel ( grantResult ) )
9487 }
9588
9689 async createValidationToken (
@@ -117,15 +110,15 @@ export class DB {
117110 return insertResult . meta . last_row_id
118111 }
119112
120- async validateAccessToken ( accessTokenId : number , validationToken : string ) {
113+ async validateTokenToGrant ( grantId : number , validationToken : string ) {
121114 const validationResult = await this . db
122115 . prepare (
123116 sql `
124- SELECT id, email, access_token_id FROM validation_tokens
125- WHERE access_token_id = ?1 AND token_value = ?2
117+ SELECT id, email, grant_id FROM validation_tokens
118+ WHERE grant_id = ?1 AND token_value = ?2
126119 ` ,
127120 )
128- . bind ( accessTokenId , validationToken )
121+ . bind ( grantId , validationToken )
129122 . first ( )
130123
131124 if ( ! validationResult ) {
@@ -150,22 +143,19 @@ export class DB {
150143 }
151144
152145 // set access token to user id
153- const claimAccessTokenResult = await this . db
146+ const claimGrantResult = await this . db
154147 . prepare (
155148 sql `
156- UPDATE access_tokens
157- SET user_id = ?2 , updated_at = CURRENT_TIMESTAMP
158- WHERE id = ?1
149+ UPDATE grants
150+ SET user_id = ?1 , updated_at = CURRENT_TIMESTAMP
151+ WHERE id = ?2
159152 ` ,
160153 )
161- . bind ( validationResult . access_token_id , userId )
154+ . bind ( userId , validationResult . grant_id )
162155 . run ( )
163156
164- if (
165- ! claimAccessTokenResult . success ||
166- ! claimAccessTokenResult . meta . last_row_id
167- ) {
168- throw new Error ( 'Failed to create user: ' + claimAccessTokenResult . error )
157+ if ( ! claimGrantResult . success ) {
158+ throw new Error ( 'Failed to claim grant: ' + claimGrantResult . error )
169159 }
170160
171161 // delete validation token (fire and forget)
@@ -181,16 +171,21 @@ export class DB {
181171 }
182172 }
183173
184- async deleteAccessToken ( userId : number , tokenValue : string ) {
185- await this . db
174+ async deleteGrant ( userId : number , grantId : string ) {
175+ const deleteResult = await this . db
186176 . prepare (
187177 sql `
188- DELETE FROM access_tokens
189- WHERE user_id = ?1 AND token_value = ?2
178+ DELETE FROM grants
179+ WHERE user_id = ?1 AND grant_user_id = ?2
190180 ` ,
191181 )
192- . bind ( userId , tokenValue )
182+ . bind ( userId , grantId )
193183 . run ( )
184+
185+ if ( ! deleteResult . success ) {
186+ throw new Error ( 'Failed to delete grant: ' + deleteResult . error )
187+ }
188+ // TODO: delete the grant from OAUTH_PROVIDER as well
194189 }
195190
196191 async createUserByEmail ( email : string ) {
@@ -403,9 +398,10 @@ export class DB {
403398 return tagSchema . parse ( snakeToCamel ( result ) )
404399 }
405400
406- async listTags ( ) {
401+ async listTags ( userId : number ) {
407402 const results = await this . db
408- . prepare ( sql `SELECT * FROM tags ORDER BY name` )
403+ . prepare ( sql `SELECT * FROM tags WHERE user_id = ?1 ORDER BY name` )
404+ . bind ( userId )
409405 . all ( )
410406
411407 return z
@@ -521,7 +517,7 @@ export class DB {
521517 async getEntryTag ( userId : number , id : number ) {
522518 const result = await this . db
523519 . prepare ( sql `SELECT * FROM entry_tags WHERE id = ?1 AND user_id = ?2` )
524- . bind ( id )
520+ . bind ( id , userId )
525521 . first ( )
526522
527523 if ( ! result ) return null
0 commit comments