mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 19:44:15 +00:00
handle token regeneration for customRequest
This commit is contained in:
parent
a4f5509770
commit
d04c1f028a
@ -94,31 +94,54 @@ export default class ApiCore extends Core {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async customRequest(
|
async customRequest(
|
||||||
payload = {
|
request = {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
},
|
},
|
||||||
...args
|
...args
|
||||||
) {
|
) {
|
||||||
if (typeof payload === "string") {
|
// handle before request
|
||||||
payload = {
|
await this.handleBeforeRequest(request)
|
||||||
url: payload,
|
|
||||||
|
if (typeof request === "string") {
|
||||||
|
request = {
|
||||||
|
url: request,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof payload.headers !== "object") {
|
if (typeof request.headers !== "object") {
|
||||||
payload.headers = {}
|
request.headers = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const sessionToken = await SessionModel.token
|
let result = null
|
||||||
|
|
||||||
if (sessionToken) {
|
const makeRequest = async () => {
|
||||||
payload.headers["Authorization"] = `Bearer ${sessionToken}`
|
const sessionToken = await SessionModel.token
|
||||||
|
|
||||||
} else {
|
if (sessionToken) {
|
||||||
console.warn("Making a request with no session token")
|
request.headers["Authorization"] = `Bearer ${sessionToken}`
|
||||||
|
} else {
|
||||||
|
console.warn("Making a request with no session token")
|
||||||
|
}
|
||||||
|
|
||||||
|
const _result = await this.instance.httpInterface(request, ...args)
|
||||||
|
.catch((error) => {
|
||||||
|
return error
|
||||||
|
})
|
||||||
|
|
||||||
|
result = _result
|
||||||
}
|
}
|
||||||
|
|
||||||
return await this.instance.httpInterface(payload, ...args)
|
await makeRequest()
|
||||||
|
|
||||||
|
// handle after request
|
||||||
|
await this.handleAfterRequest(result, makeRequest)
|
||||||
|
|
||||||
|
// if error, throw it
|
||||||
|
if (result instanceof Error) {
|
||||||
|
throw result
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
request(method, endpoint, ...args) {
|
request(method, endpoint, ...args) {
|
||||||
@ -129,7 +152,7 @@ export default class ApiCore extends Core {
|
|||||||
return this.instance.endpoints
|
return this.instance.endpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleBeforeRequest(request) {
|
handleBeforeRequest = async (request) => {
|
||||||
if (this.onExpiredExceptionEvent) {
|
if (this.onExpiredExceptionEvent) {
|
||||||
if (this.excludedExpiredExceptionURL.includes(request.url)) return
|
if (this.excludedExpiredExceptionURL.includes(request.url)) return
|
||||||
|
|
||||||
@ -142,7 +165,29 @@ export default class ApiCore extends Core {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleRegenerationEvent(refreshToken, makeRequest) {
|
handleAfterRequest = async (data, callback) => {
|
||||||
|
// handle 401 responses
|
||||||
|
if (data instanceof Error) {
|
||||||
|
if (data.response.status === 401) {
|
||||||
|
// check if the server issue a refresh token on data
|
||||||
|
if (data.response.data.refreshToken) {
|
||||||
|
console.log(`Session expired, but the server issued a refresh token, handling regeneration event`)
|
||||||
|
|
||||||
|
// handle regeneration event
|
||||||
|
await this.handleRegenerationEvent(data.response.data.refreshToken)
|
||||||
|
|
||||||
|
return await callback()
|
||||||
|
} else {
|
||||||
|
return window.app.eventBus.emit("session.invalid", "Session expired, but the server did not issue a refresh token")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (data.response.status === 403) {
|
||||||
|
return window.app.eventBus.emit("session.invalid", "Session not valid or not existent")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleRegenerationEvent = async (refreshToken) => {
|
||||||
window.app.eventBus.emit("session.expiredExceptionEvent", refreshToken)
|
window.app.eventBus.emit("session.expiredExceptionEvent", refreshToken)
|
||||||
|
|
||||||
this.onExpiredExceptionEvent = true
|
this.onExpiredExceptionEvent = true
|
||||||
@ -166,8 +211,12 @@ export default class ApiCore extends Core {
|
|||||||
return window.app.eventBus.emit("session.invalid", "Failed to regenerate token")
|
return window.app.eventBus.emit("session.invalid", "Failed to regenerate token")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!response.data?.token) {
|
||||||
|
return window.app.eventBus.emit("session.invalid", "Failed to regenerate token, invalid server response.")
|
||||||
|
}
|
||||||
|
|
||||||
// set new token
|
// set new token
|
||||||
SessionModel.token = response.token
|
SessionModel.token = response.data.token
|
||||||
|
|
||||||
//this.namespaces["main"].internalAbortController.abort()
|
//this.namespaces["main"].internalAbortController.abort()
|
||||||
|
|
||||||
@ -192,25 +241,6 @@ export default class ApiCore extends Core {
|
|||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleResponse = async (data, makeRequest) => {
|
|
||||||
// handle 401 responses
|
|
||||||
if (data instanceof Error) {
|
|
||||||
if (data.response.status === 401) {
|
|
||||||
// check if the server issue a refresh token on data
|
|
||||||
if (data.response.data.refreshToken) {
|
|
||||||
// handle regeneration event
|
|
||||||
await this.handleRegenerationEvent(data.response.data.refreshToken, makeRequest)
|
|
||||||
return await makeRequest()
|
|
||||||
} else {
|
|
||||||
return window.app.eventBus.emit("session.invalid", "Session expired, but the server did not issue a refresh token")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (data.response.status === 403) {
|
|
||||||
return window.app.eventBus.emit("session.invalid", "Session not valid or not existent")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof params !== "object") {
|
if (typeof params !== "object") {
|
||||||
throw new Error("Params must be an object")
|
throw new Error("Params must be an object")
|
||||||
}
|
}
|
||||||
@ -221,7 +251,7 @@ export default class ApiCore extends Core {
|
|||||||
},
|
},
|
||||||
onBeforeRequest: this.handleBeforeRequest,
|
onBeforeRequest: this.handleBeforeRequest,
|
||||||
onRequest: getSessionContext,
|
onRequest: getSessionContext,
|
||||||
onResponse: handleResponse,
|
onResponse: this.handleAfterRequest,
|
||||||
...params,
|
...params,
|
||||||
origin: params.httpAddress ?? config.remotes.mainApi,
|
origin: params.httpAddress ?? config.remotes.mainApi,
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ export default class Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getAllSessions() {
|
static async getAllSessions() {
|
||||||
const response = await app.cores.api.customRequest( {
|
const response = await app.cores.api.customRequest({
|
||||||
method: "get",
|
method: "get",
|
||||||
url: "/session/all"
|
url: "/session/all"
|
||||||
})
|
})
|
||||||
@ -37,7 +37,7 @@ export default class Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getCurrentSession() {
|
static async getCurrentSession() {
|
||||||
const response = await app.cores.api.customRequest( {
|
const response = await app.cores.api.customRequest({
|
||||||
method: "get",
|
method: "get",
|
||||||
url: "/session/current"
|
url: "/session/current"
|
||||||
})
|
})
|
||||||
@ -48,7 +48,7 @@ export default class Session {
|
|||||||
static async getTokenValidation() {
|
static async getTokenValidation() {
|
||||||
const session = await Session.token
|
const session = await Session.token
|
||||||
|
|
||||||
const response = await app.cores.api.customRequest( {
|
const response = await app.cores.api.customRequest({
|
||||||
method: "get",
|
method: "get",
|
||||||
url: "/session/validate",
|
url: "/session/validate",
|
||||||
data: {
|
data: {
|
||||||
@ -71,7 +71,7 @@ export default class Session {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await app.cores.api.customRequest( {
|
const response = await app.cores.api.customRequest({
|
||||||
method: "delete",
|
method: "delete",
|
||||||
url: "/session/current"
|
url: "/session/current"
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
@ -94,7 +94,7 @@ export default class Session {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await app.cores.api.customRequest( {
|
const response = await app.cores.api.customRequest({
|
||||||
method: "delete",
|
method: "delete",
|
||||||
url: "/session/all"
|
url: "/session/all"
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user