Created
March 30, 2024 15:59
-
-
Save RaheemJnr/2d426b466b08346b79fcf8146b7a4a6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*Variables Initialization | |
* Client Configuration | |
* client.plugin(HttpSend).intercept { request -> ... }: Adds an interceptor to the HttpSend plugin of the HttpClient. This interceptor will intercept every HTTP request made by the client, allowing for custom logic to be executed before the request is sent and after the response is received. | |
* Access Token Handling | |
* val access = localDatabase.getAccessToken(mainRepo.getUserId).first(): Retrieves the current access token for the user from the local database. It assumes that getAccessToken returns a flow or collection, and .first() gets the first element. | |
* | |
* request.headers { append("Authorization", "Bearer $access") }: Modifies the HTTP request headers to include the current access token in the Authorization header. | |
* | |
* Response Status Check and Refresh Token Handling | |
* if (originalCall.response.status.value == 401 && access.isNotEmpty()) { ... }: Checks if the original HTTP request resulted in a 401 Unauthorized response and the access token is not empty. If so, it proceeds to handle the refresh token logic. | |
* val refreshToken = localDatabase.getRefreshToken(mainRepo.getUserId): Retrieves the current refresh token for the user. | |
* val (newAccess, newRefresh) = authRepo.getRefreshToken(refreshToken): Calls the authRepo to obtain new access and refresh tokens by using the current refresh token. | |
* localDatabase.saveAccessToken(newAccess, userId = mainRepo.getUserId): Saves the new access token to the local database. | |
* localDatabase.saveRefreshToken(newRefresh, userId = mainRepo.getUserId): Saves the new refresh token to the local database. | |
* execute(request): Re-executes the original HTTP request with the new access token. | |
* Original Call Return | |
* If the original HTTP request does not result in a 401 response or if there is no access token, it simply returns the result of the original HTTP call. | |
* ***/ | |
fun Scope.authorizationIntercept(client: HttpClient) { | |
// | |
val localDatabase: //init local database | |
val mainRepo: // init the repo | |
client.plugin(HttpSend).intercept { request -> | |
val access = localDatabase.getAccessToken(mainRepo.getUserId) | |
request.headers { | |
append("Authorization", "Bearer $access") | |
} | |
val originalCall = execute(request) | |
if (originalCall.response.status.value == 401 && access.isNotEmpty()) { | |
val refreshToken = localDatabase.getRefreshToken() | |
val (newAccess, newRefresh) = mainRepo.getRefreshToken(refreshToken) | |
localDatabase.saveAccessToken(newAccess) | |
localDatabase.saveRefreshToken(newRefresh) | |
execute(request) | |
} else { | |
originalCall | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment