I’ve hassle about utilizing http request with Alamofire
my sever makes use of JWT to authenticate person.
so when JWT would not exist request must create one.
so I do that job in Adapter in Alamofire
step 1. examine tokens are in reminiscence
step 2. if not exists reqeust new token to server (new request)
step 3. bought token and calls completion(.success( request with token ))
drawback is that, though step3 is completed properly, request would not work (I checked that request doen’t made in Proxyman)
I do not get what’s improper.. assist me please
under code is my adapter
lazy non-public(set) var tokenAdapter = Adapter { [weak self] request, session, completion in
var modifiedRequest = request
✋✋✋ Step1
if let accessToken = self?.credential.accessToken {
let bearerToken = "Bearer (accessToken)"
modifiedRequest
.addValue("Authorization", forHTTPHeaderField: bearerToken)
completion(.success(modifiedRequest))
} else {
// 토큰을 발행한 적이 없는 경우
guard let deviceId = self?.credential.devideIdentifier,
let requestBox = self?.issueToken(deviceIdentity: DeviceIdentityDTO(imei: deviceId))
else {
return completion(.failure(RequestGenerationError.crendentialIsNotAvailable))
}
do {
let issueTokenRequest = strive requestBox.endPoint.toRequest()
✋✋✋ Step2
session
.request(issueTokenRequest)
.responseDecodable(of: TokenResponse.self) { response in
if let error = response.error {
return completion(.failure(error))
}
if let responseData = strive? response.outcome.get().information {
let accessToken = responseData.accessToken
let refreshToken = responseData.refreshToken
self?.credential.renewalTokens(
accessToken: accessToken,
refreshToken: refreshToken
)
let bearerToken = "Bearer (accessToken)"
modifiedRequest
.addValue("Authorization", forHTTPHeaderField: bearerToken)
✋✋✋ Step3
return completion(.success(modifiedRequest))
}
return completion(.failure(ResponseError.dataIsNotFound))
}
} catch {
completion(.failure(error))
}
}
}
It was confirmed that the request didn’t begin after calling the Adapter’s completion closure.