1
Мне нужно передать токен JWT, чтобы получить доступ к данным. Как мне это сделать? Я вижу токен в консоли, но ничего нет в таблицеView. То, что я на самом деле хочу это: - аутентифицировать пользователь - Получить JWT лексемы - Просмотр соответствующих курсовЧтение и прохождение JWT в Swift 3
import UIKit
import Alamofire
class AvailableCoursesTableViewController: UITableViewController {
let url = "https://api.sis.kemoke.net/auth/login"
var parameters = ["email": "[email protected]", "password": "passwd"]
var token : HTTPHeaders = ["X-Auth-Token": ""]
//Custom struct for the data
struct Courses {
let course : String
init(dictionary: [String:String]) {
self.course = dictionary["course"] ?? ""
}
}
//Array which holds the courses
var courseData = [Courses]()
func authenticate() {
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: nil).responseJSON {
(response) in
self.token["X-Auth-Token"] = response.description
print(self.token)
}
}
// Download the courses
func downloadData() {
Alamofire.request("https://api.sis.kemoke.net/student/course", headers: token).responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
//Optional binding to handle exceptions
self.courseData.removeAll() // clean the data source array
if let json = response.result.value as? [[String:String]] {
for course in json {
self.courseData.append(Courses(dictionary: course))
}
self.tableView.reloadData()
}
}
}