Skip to content

Commit 4aa4d7c

Browse files
chore: wip
1 parent 0a1517f commit 4aa4d7c

File tree

11 files changed

+56
-47
lines changed

11 files changed

+56
-47
lines changed

app/Middleware/Api.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,38 @@ export default new Middleware({
88
async handle() {
99
const bearerToken = request.bearerToken() || ''
1010

11-
const parts = bearerToken.split('|')
11+
if (!bearerToken) {
12+
throw { message: 'Unauthorized.', status: 401 }
13+
}
14+
15+
const parts = bearerToken.split(':')
1216

13-
if (parts.length !== 2) {
17+
if (parts.length !== 3) {
1418
throw { message: 'Invalid bearer token format', status: 401 }
1519
}
1620

17-
const teamToken = parts[0]
18-
const plainString = parts[1] as string
21+
const tokenId = Number(parts[0])
22+
const teamId = parts[1] as string
23+
const plainString = parts[2] as string
1924

20-
const team = await Team.find(Number(teamToken))
25+
const team = await Team.find(Number(teamId))
2126

2227
if (!team) {
2328
throw { message: 'Invalid bearer token', status: 401 }
2429
}
2530

31+
const accessTokens = await team.teamAccessTokens()
32+
33+
if (!accessTokens.length) {
34+
throw { message: 'Invalid bearer token', status: 401 }
35+
}
36+
37+
const accessTokenIds = accessTokens.map((accessToken) => accessToken.id)
38+
39+
if (!accessTokenIds.includes(tokenId)) {
40+
throw { message: 'Invalid bearer token', status: 401 }
41+
}
42+
2643
const teamBearerToken = await AccessToken.where('token', plainString).first()
2744

2845
if (!teamBearerToken) {

app/Middleware/Team.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default new Middleware({
1818
const teamId = parts[1] as string
1919
const plainString = parts[2] as string
2020

21-
const team = await Team.find(Number(teamToken))
21+
const team = await Team.find(Number(teamId))
2222

2323
if (!team) {
2424
throw { message: 'Invalid bearer token', status: 401 }

storage/framework/core/actions/src/orm/generate-model.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { log } from '@stacksjs/logging'
22
import { getModelName, getTableName } from '@stacksjs/orm'
33
import { path } from '@stacksjs/path'
44
import { fs, glob } from '@stacksjs/storage'
5-
import { camelCase, pascalCase, plural, singular } from '@stacksjs/strings'
5+
import { camelCase, pascalCase, plural, singular, snakeCase } from '@stacksjs/strings'
66
import type { Attributes, Model, RelationConfig } from '@stacksjs/types'
77
import { isString } from '@stacksjs/validation'
88
export interface FieldArrayElement {
@@ -829,6 +829,9 @@ async function generateModelString(
829829
830830
const tableRelationIds = results.map(result => result.${singular(tableRelation)}_id)
831831
832+
if (! tableRelationIds.length)
833+
throw new Error('Relation Error!')
834+
832835
const relationResults = await ${modelRelation}.whereIn('id', tableRelationIds).get()
833836
834837
return relationResults
@@ -869,9 +872,9 @@ async function generateModelString(
869872

870873
fieldString += ` ${attribute.field}: ${entity}\n `
871874

872-
declareFields += `public ${attribute.field}: ${entity} | undefined \n `
875+
declareFields += `public ${snakeCase(attribute.field)}: ${entity} | undefined \n `
873876

874-
constructorFields += `this.${attribute.field} = ${formattedModelName}?.${attribute.field}\n `
877+
constructorFields += `this.${snakeCase(attribute.field)} = ${formattedModelName}?.${snakeCase(attribute.field)}\n `
875878

876879
whereStatements += `static where${pascalCase(attribute.field)}(value: string | number | boolean | undefined | null): ${modelName}Model {
877880
const instance = new this(null)

storage/framework/core/auth/src/authentication.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export async function authToken() {
6868

6969
const team = teams[0]
7070

71-
const accessToken = await AccessToken.where('team_id', team.id).first()
71+
const accessTokens = await team.teamAccessTokens()
72+
const accessToken = accessTokens[0]
7273

7374
const tokenId = accessToken?.id
7475

storage/framework/core/cloud/src/helpers.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,14 +606,12 @@ export async function getOrCreateTimestamp(): Promise<string> {
606606
}
607607
}
608608

609-
610609
// get the CloudFront distribution ID of the current stack
611610
export async function getCloudFrontDistributionId(): Promise<string> {
612611
return ''
613-
// return await runCommand(`aws cloudfront list-distributions --query "DistributionList.Items[?Origins.Items[0].DomainName=='${config.app.url}'].Id"`)
612+
// return await runCommand(`aws cloudfront list-distributions --query "DistributionList.Items[?Origins.Items[0].DomainName=='${config.app.url}'].Id"`)
614613
}
615614

616-
617615
// function isProductionEnv(env: string) {
618616
// return env === 'production' || env === 'prod'
619617
// }
@@ -630,5 +628,5 @@ export async function getCloudFrontDistributionId(): Promise<string> {
630628
// catch (error) {
631629
// console.error('Error fetching buckets', error)
632630
// return `${prefix}-${timestamp}`
633-
// }
631+
// }
634632
// }

storage/framework/core/utils/src/hash.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ export function websiteSourceHash() {
2121
}
2222

2323
export function docsSourceHash() {
24-
const docsSrc = [
25-
p.projectPath('docs'),
26-
p.projectPath('config/docs.ts')
27-
]
24+
const docsSrc = [p.projectPath('docs'), p.projectPath('config/docs.ts')]
2825

2926
return hashPaths(docsSrc)
3027
}

storage/framework/orm/src/models/AccessToken.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class AccessTokenModel {
6262
public id: number | undefined
6363
public name: string | undefined
6464
public token: string | undefined
65-
public plainTextToken: string | undefined
65+
public plain_text_token: string | undefined
6666
public abilities: string[] | undefined
6767
public team_id: number | undefined
6868

@@ -71,7 +71,7 @@ export class AccessTokenModel {
7171
this.id = accesstoken?.id
7272
this.name = accesstoken?.name
7373
this.token = accesstoken?.token
74-
this.plainTextToken = accesstoken?.plainTextToken
74+
this.plain_text_token = accesstoken?.plain_text_token
7575
this.abilities = accesstoken?.abilities
7676
this.team_id = accesstoken?.team_id
7777

storage/framework/orm/src/models/Deployment.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,25 @@ export class DeploymentModel {
6363
protected query: any
6464
protected hasSelect: boolean
6565
public id: number | undefined
66-
public commitSha: string | undefined
67-
public commitMessage: string | undefined
66+
public commit_sha: string | undefined
67+
public commit_message: string | undefined
6868
public branch: string | undefined
6969
public status: string | undefined
70-
public executionTime: number | undefined
71-
public deployScript: string | undefined
72-
public terminalOutput: string | undefined
70+
public execution_time: number | undefined
71+
public deploy_script: string | undefined
72+
public terminal_output: string | undefined
7373
public user_id: number | undefined
7474

7575
constructor(deployment: Partial<DeploymentType> | null) {
7676
this.deployment = deployment
7777
this.id = deployment?.id
78-
this.commitSha = deployment?.commitSha
79-
this.commitMessage = deployment?.commitMessage
78+
this.commit_sha = deployment?.commit_sha
79+
this.commit_message = deployment?.commit_message
8080
this.branch = deployment?.branch
8181
this.status = deployment?.status
82-
this.executionTime = deployment?.executionTime
83-
this.deployScript = deployment?.deployScript
84-
this.terminalOutput = deployment?.terminalOutput
82+
this.execution_time = deployment?.execution_time
83+
this.deploy_script = deployment?.deploy_script
84+
this.terminal_output = deployment?.terminal_output
8585
this.user_id = deployment?.user_id
8686

8787
this.query = db.selectFrom('deployments')

storage/framework/orm/src/models/Team.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,27 @@ export class TeamModel {
6868
protected hasSelect: boolean
6969
public id: number | undefined
7070
public name: string | undefined
71-
public companyName: string | undefined
71+
public company_name: string | undefined
7272
public email: string | undefined
73-
public billingEmail: string | undefined
73+
public billing_email: string | undefined
7474
public status: string | undefined
7575
public description: string | undefined
7676
public path: string | undefined
77-
public isPersonal: boolean | undefined
77+
public is_personal: boolean | undefined
7878
public accesstoken_id: number | undefined
7979
public user_id: number | undefined
8080

8181
constructor(team: Partial<TeamType> | null) {
8282
this.team = team
8383
this.id = team?.id
8484
this.name = team?.name
85-
this.companyName = team?.companyName
85+
this.company_name = team?.company_name
8686
this.email = team?.email
87-
this.billingEmail = team?.billingEmail
87+
this.billing_email = team?.billing_email
8888
this.status = team?.status
8989
this.description = team?.description
9090
this.path = team?.path
91-
this.isPersonal = team?.isPersonal
91+
this.is_personal = team?.is_personal
9292
this.accesstoken_id = team?.accesstoken_id
9393
this.user_id = team?.user_id
9494

@@ -498,6 +498,8 @@ export class TeamModel {
498498

499499
const tableRelationIds = results.map((result) => result.personal_access_token_id)
500500

501+
if (!tableRelationIds.length) return []
502+
501503
const relationResults = await AccessToken.whereIn('id', tableRelationIds).get()
502504

503505
return relationResults

storage/framework/orm/src/models/User.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class UserModel {
7272
public two_factor_secret: string | undefined
7373
public name: string | undefined
7474
public email: string | undefined
75-
public jobTitle: string | undefined
75+
public job_title: string | undefined
7676
public password: string | undefined
7777
public team_id: number | undefined
7878
public deployment_id: number | undefined
@@ -84,7 +84,7 @@ export class UserModel {
8484
this.two_factor_secret = user?.two_factor_secret
8585
this.name = user?.name
8686
this.email = user?.email
87-
this.jobTitle = user?.jobTitle
87+
this.job_title = user?.job_title
8888
this.password = user?.password
8989
this.team_id = user?.team_id
9090
this.deployment_id = user?.deployment_id

0 commit comments

Comments
 (0)