Skip to content

Commit

Permalink
fix(sdk): fix broken types with DataResponse type
Browse files Browse the repository at this point in the history
  • Loading branch information
bahdcoder committed Oct 25, 2021
1 parent e6d3d2b commit 5d6843e
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"release": "./scripts/release.sh",
"commit": "yarn git-cz",
"setup": "yarn && yarn build && cd examples/blog && yarn && cd ../typescript && yarn",
"example": "wsrun -p '@examples/typescript' && yarn example:dev",
"example": "wsrun -p '@examples/typescript' -c example:dev",
"cms:dev": "wsrun -p '@tensei/cms' -c dev-client",
"cms:dev:server": "wsrun -p '@tensei/cms' -c dev",
"cms:build": "wsrun -p '@tensei/cms' -c build",
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ export class Auth implements AuthContract {
: resource.data.noTimestamps
? false
: {
created_at: Dayjs().add(1, 'month').toDate()
createdAt: Dayjs().add(1, 'month').toDate()
}
)
])
Expand Down
4 changes: 2 additions & 2 deletions packages/next-auth/src/handlers/check-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default async function handleCheckSession(

request.session.set('auth', {
refresh_token: apiResponse.data.data.refresh_token,
access_token_expires_at: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expires_in
accessTokenExpiresAt: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expiresIn
)
})

Expand Down
4 changes: 2 additions & 2 deletions packages/next-auth/src/handlers/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default async function handleLogin(

request.session.set('auth', {
refresh_token: apiResponse.data.data.refresh_token,
access_token_expires_at: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expires_in
accessTokenExpiresAt: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expiresIn
)
})

Expand Down
4 changes: 2 additions & 2 deletions packages/next-auth/src/handlers/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export default async function handleSignup(

request.session.set('auth', {
refresh_token: apiResponse.data.data.refresh_token,
access_token_expires_at: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expires_in
accessTokenExpiresAt: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expiresIn
)
})

Expand Down
6 changes: 3 additions & 3 deletions packages/next-auth/src/handlers/social-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export default async function handleSocialCallback(
})

request.session.set('auth', {
refresh_token: apiResponse.data.data.refresh_token,
access_token_expires_at: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expires_in
refreshToken: apiResponse.data.data.refresh_token,
accessTokenExpiresAt: getAccessTokenExpiryTimeStamp(
apiResponse.data.data.expiresIn
)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class Rest {
.filter(
field =>
!field.showHideFieldFromApi.hideOnCreateApi &&
!['id', '_id', 'created_at', 'updated_at'].includes(
!['id', '_id', 'createdAt', 'updatedAt'].includes(
field.databaseField
)
)
Expand Down
6 changes: 3 additions & 3 deletions packages/rest/src/sdk/generators/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const getAuthUserResponseInterface = (
authConfig: AuthPluginConfig
) => {
return `
export interface DataResponse<Response> {
data: Response
}
export type DataResponse<Response> = AxiosResponse<{
data: Response
}>
export interface AuthResponse {
${userResource.data.snakeCaseName}: ${userResource.data.pascalCaseName}
Expand Down
49 changes: 23 additions & 26 deletions packages/sdk/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ export interface TokenStorage {
}

export interface TokenStorageValue {
access_token_expires_in: number
refresh_token: string
current_time: string
accessTokenExpiresIn: number
refreshToken: string
currentTime: string
}

export interface AccessTokenStorageValue {
access_token_expires_at: string
accessTokenExpiresAt: string
accessToken: string
current_time: string
expires_in: number
currentTime: string
expiresIn: number
}

export interface DataResponse<Response> {
Expand All @@ -49,8 +49,8 @@ export interface DataResponse<Response> {
export interface AuthResponse {
customer: any
accessToken: string
expires_in: number
refresh_token: string
expiresIn: number
refreshToken: string
}

export type ResetPasswordResponse = true
Expand Down Expand Up @@ -149,7 +149,7 @@ export class AuthAPI {

this.auth_response = {
accessToken: session.accessToken,
expires_in: session.expires_in
expires_in: session.expiresIn
} as any

this.updateUser(response.data.data)
Expand Down Expand Up @@ -212,7 +212,7 @@ export class AuthAPI {
}

try {
const response = await this.refreshToken({ token: session.refresh_token })
const response = await this.refreshToken({ token: session.refreshToken })

this.auth_response = response.data.data
this.invokeAuthChange()
Expand Down Expand Up @@ -258,14 +258,14 @@ export class AuthAPI {
const token_expires_at = new Date()

token_expires_at.setSeconds(
token_expires_at.getSeconds() + this.auth_response.expires_in
token_expires_at.getSeconds() + this.auth_response.expiresIn
)

this.storage.set<AccessTokenStorageValue>({
current_time: new Date().toISOString(),
expires_in: this.auth_response.expires_in,
currentTime: new Date().toISOString(),
expiresIn: this.auth_response.expiresIn,
accessToken: this.auth_response.accessToken,
access_token_expires_at: token_expires_at.toISOString()
accessTokenExpiresAt: token_expires_at.toISOString()
})
}

Expand All @@ -281,19 +281,16 @@ export class AuthAPI {
}

// if refresh tokens are not turned on on the API:
if (
!this.auth_response?.refresh_token ||
!this.auth_response?.accessToken
) {
if (!this.auth_response?.refreshToken || !this.auth_response?.accessToken) {
return
}

const current_time = new Date().toISOString()
const currentTime = new Date().toISOString()

this.storage.set<TokenStorageValue>({
current_time,
refresh_token: this.auth_response.refresh_token,
access_token_expires_in: this.auth_response.expires_in
currentTime,
refreshToken: this.auth_response.refreshToken,
accessTokenExpiresIn: this.auth_response.expiresIn
})

if (this.session_interval) {
Expand All @@ -303,7 +300,7 @@ export class AuthAPI {
// Trigger a token refresh 10 seconds before the current access token expires.
this.session_interval = setInterval(() => {
this.silentLogin()
}, (this.auth_response.expires_in - 10) * 1000)
}, (this.auth_response.expiresIn - 10) * 1000)
}

refreshToken(payload: { token: string }) {
Expand All @@ -315,16 +312,16 @@ export class AuthAPI {
}

private isSessionValid(session: AccessTokenStorageValue) {
const token_expires_at = new Date(session.access_token_expires_at)
const token_expires_at = new Date(session.accessTokenExpiresAt)

return token_expires_at > new Date()
}

isRefreshSessionValid(session: TokenStorageValue) {
const token_created_at = new Date(session.current_time)
const token_created_at = new Date(session.currentTime)

token_created_at.setSeconds(
token_created_at.getSeconds() + session.access_token_expires_in
token_created_at.getSeconds() + session.accessTokenExpiresIn
)

return token_created_at > new Date()
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/tests/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ test('can login with access tokens', async () => {
const storage = new LocalStorageStore('___tensei__session___')

expect(storage.get()).toEqual({
current_time: expect.any(String),
access_token_expires_at: expect.any(String),
expires_in: responsePayload.data.data.expires_in,
currentTime: expect.any(String),
accessTokenExpiresAt: expect.any(String),
expiresIn: responsePayload.data.data.expires_in,
accessToken: responsePayload.data.data.accessToken
})
})
Expand Down

0 comments on commit 5d6843e

Please sign in to comment.