Skip to content

Commit

Permalink
MythXJS fixed token refresh issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Gtonizuka committed Sep 5, 2019
1 parent 5eb42e1 commit 32c14f5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
24 changes: 12 additions & 12 deletions src/apiServices/AnalysesService.ts
Expand Up @@ -30,8 +30,8 @@ export class AnalysesService {

public async getAnalysesList() {
try {
const { headers, accessToken } = await getHeaders(this.jwtTokens)
this.jwtTokens.access = accessToken
const { headers, tokens } = await getHeaders(this.jwtTokens)
this.jwtTokens = tokens

const result = await getRequest(`${this.API_URL}/analyses`, headers)

Expand All @@ -43,8 +43,8 @@ export class AnalysesService {

public async getAnalysisStatus(uuid: string) {
try {
const { headers, accessToken } = await getHeaders(this.jwtTokens)
this.jwtTokens.access = accessToken
const { headers, tokens } = await getHeaders(this.jwtTokens)
this.jwtTokens = tokens

const result = await getRequest(`${this.API_URL}/analyses/${uuid}`, headers)

Expand All @@ -56,8 +56,8 @@ export class AnalysesService {

public async getDetectedIssues(uuid: string) {
try {
const { headers, accessToken } = await getHeaders(this.jwtTokens)
this.jwtTokens.access = accessToken
const { headers, tokens } = await getHeaders(this.jwtTokens)
this.jwtTokens = tokens

const getStatus = await this.getAnalysisStatus(uuid)
if (getStatus.status === 'Queued' || getStatus.status === 'In progress') {
Expand All @@ -82,8 +82,8 @@ export class AnalysesService {

public async submitBytecode(bytecode: string): Promise<SubmitContractRes | void> {
try {
const { headers, accessToken } = await getHeaders(this.jwtTokens)
this.jwtTokens.access = accessToken
const { headers, tokens } = await getHeaders(this.jwtTokens)
this.jwtTokens = tokens

const request = generateBytecodeRequest(bytecode, this.toolName)

Expand All @@ -97,8 +97,8 @@ export class AnalysesService {

public async submitSourceCode(sourceCode: string, contractName: string): Promise<SubmitContractRes | void> {
try {
const { headers, accessToken } = await getHeaders(this.jwtTokens)
this.jwtTokens.access = accessToken
const { headers, tokens } = await getHeaders(this.jwtTokens)
this.jwtTokens = tokens

const request = generateSourceCodeRequest(sourceCode, contractName, this.toolName)

Expand All @@ -112,8 +112,8 @@ export class AnalysesService {

public async analyze(options: AnalyzeOptions): Promise<any> {
try {
const { headers, accessToken } = await getHeaders(this.jwtTokens)
this.jwtTokens.access = accessToken
const { headers, tokens } = await getHeaders(this.jwtTokens)
this.jwtTokens = tokens

const request = generateAnalysisRequest(options, this.toolName)

Expand Down
12 changes: 6 additions & 6 deletions src/apiServices/AuthService.ts
Expand Up @@ -67,8 +67,8 @@ export class AuthService {
public async logout() {
if (this.isUserLoggedIn()) {
try {
const { headers, accessToken } = await getHeaders(this.jwtTokens)
this.jwtTokens.access = accessToken
const { headers, tokens } = await getHeaders(this.jwtTokens)
this.jwtTokens = tokens

const result = await postRequest(`${this.API_URL}/auth/logout`, {}, headers)
this.jwtTokens.access = this.jwtTokens.refresh = ''
Expand Down Expand Up @@ -115,8 +115,8 @@ export class AuthService {
public async getStats(queryString?: string) {
if (this.isUserLoggedIn()) {
try {
const { headers, accessToken } = await getHeaders(this.jwtTokens)
this.jwtTokens.access = accessToken
const { headers, tokens } = await getHeaders(this.jwtTokens)
this.jwtTokens = tokens

const result = await getRequest(`${this.API_URL}/stats/users-analyses?${queryString}`, headers)

Expand Down Expand Up @@ -154,8 +154,8 @@ export class AuthService {
public async getUsers(queryString: string = '') {
if (this.isUserLoggedIn()) {
try {
const { headers, accessToken } = await getHeaders(this.jwtTokens)
this.jwtTokens.access = accessToken
const { headers, tokens } = await getHeaders(this.jwtTokens)
this.jwtTokens = tokens

const result = await getRequest(`${this.API_URL}/users?${queryString}`, headers)

Expand Down
6 changes: 3 additions & 3 deletions src/util/getHeaders.ts
Expand Up @@ -2,11 +2,11 @@ import { validateToken } from './validateToken'
import { JwtTokensInterface } from '..'

export async function getHeaders(jwtTokens: JwtTokensInterface) {
const accessToken = await validateToken(jwtTokens)
const tokens = await validateToken(jwtTokens)
const headers = {
Authorization: `Bearer ${accessToken}`,
Authorization: `Bearer ${tokens.access}`,
'Content-Type': 'application/json',
}

return { accessToken, headers }
return { tokens, headers }
}
6 changes: 3 additions & 3 deletions src/util/validateToken.ts
Expand Up @@ -11,13 +11,13 @@ interface jwtInterface {
iat: number
}

export async function validateToken(tokens: JwtTokensInterface) {
export async function validateToken(tokens: JwtTokensInterface): Promise<JwtTokensInterface> {
if (isTokenValid(tokens.access)) {
return tokens.access
return tokens
} else {
// else return refreshed token
const returnT = (await refreshToken(tokens)) as JwtTokensInterface
return returnT.access
return returnT
}
}

Expand Down

0 comments on commit 32c14f5

Please sign in to comment.