user2924482
user2924482

Reputation: 9120

Swift: How to decode Base64Url into json object like JWT

I have the following JWT encode payload:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJUaXRsZSI6Ik5pY2UiLCJuYW1lIjoiSmltbXkiLCJhZ2UiOjU1fQ.DSdqRFRPM4Hep704s3cvWkpH5FFpnIc82uVUswHbaz4

But I haven't found a way to decode this string like JWT does.

By any chance any of you knows a way in swift to decode this payload?

I'll really appreciate your help.

Upvotes: 0

Views: 253

Answers (1)

Safeer Raees
Safeer Raees

Reputation: 410

Add dependencies Add the Swift-JWT package to the dependencies within your application’s Package.swift file. Substitute "x.x.x" with the latest Swift-JWT release.

.package(url: "https://github.com/IBM-Swift/Swift-JWT.git", from: "x.x.x")

Add SwiftJWT to your target's dependencies:

.target(name: "example", dependencies: ["SwiftJWT"]),

Import package

import SwiftJWT

Cocoapods To include Swift-JWT in a project using CocoaPods, add SwiftJWT to your Podfile:

pod 'SwiftJWT'

Try this code:

 let jwtEncoder = JWTEncoder(jwtSigner: jwtSigner)

 let jwtString = try jwtEncoder.encodeToString(myJWT)

 let jwtDecoder = JWTDecoder(jwtVerifier: jwtVerifier)
 let jwt = try jwtDecoder.decode(JWT<MyClaims>.self, fromString: jwtString)

For documentation

Upvotes: 3

Related Questions