auth
This commit is contained in:
parent
0f80a9729e
commit
fc64540723
8
NetworkApi/.gitignore
vendored
Normal file
8
NetworkApi/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
.DS_Store
|
||||
/.build
|
||||
/Packages
|
||||
xcuserdata/
|
||||
DerivedData/
|
||||
.swiftpm/configuration/registries.json
|
||||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
||||
.netrc
|
24
NetworkApi/Package.swift
Normal file
24
NetworkApi/Package.swift
Normal file
@ -0,0 +1,24 @@
|
||||
// swift-tools-version: 6.0
|
||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "NetworkApi",
|
||||
products: [
|
||||
// Products define the executables and libraries a package produces, making them visible to other packages.
|
||||
.library(
|
||||
name: "NetworkApi",
|
||||
targets: ["NetworkApi"]),
|
||||
],
|
||||
targets: [
|
||||
// Targets are the basic building blocks of a package, defining a module or a test suite.
|
||||
// Targets can depend on other targets in this package and products from dependencies.
|
||||
.target(
|
||||
name: "NetworkApi"),
|
||||
.testTarget(
|
||||
name: "NetworkApiTests",
|
||||
dependencies: ["NetworkApi"]
|
||||
),
|
||||
]
|
||||
)
|
2
NetworkApi/Sources/NetworkApi/NetworkApi.swift
Normal file
2
NetworkApi/Sources/NetworkApi/NetworkApi.swift
Normal file
@ -0,0 +1,2 @@
|
||||
// The Swift Programming Language
|
||||
// https://docs.swift.org/swift-book
|
12
NetworkApi/Tests/NetworkApiTests/NetworkApiTests.swift
Normal file
12
NetworkApi/Tests/NetworkApiTests/NetworkApiTests.swift
Normal file
@ -0,0 +1,12 @@
|
||||
import XCTest
|
||||
@testable import NetworkApi
|
||||
|
||||
final class NetworkApiTests: XCTestCase {
|
||||
func testExample() throws {
|
||||
// XCTest Documentation
|
||||
// https://developer.apple.com/documentation/xctest
|
||||
|
||||
// Defining Test Cases and Test Methods
|
||||
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
|
||||
}
|
||||
}
|
15
Package.resolved
Normal file
15
Package.resolved
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"originHash" : "068ba4d664f0065a1b9c7d7d2d9ac8794687caa353c7943f0ca8dd1e432db8e1",
|
||||
"pins" : [
|
||||
{
|
||||
"identity" : "alamofire",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/Alamofire/Alamofire",
|
||||
"state" : {
|
||||
"revision" : "513364f870f6bfc468f9d2ff0a95caccc10044c5",
|
||||
"version" : "5.10.2"
|
||||
}
|
||||
}
|
||||
],
|
||||
"version" : 3
|
||||
}
|
27
Package.swift
Normal file
27
Package.swift
Normal file
@ -0,0 +1,27 @@
|
||||
// swift-tools-version: 6.0
|
||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "NetworkApi",
|
||||
platforms: [.iOS(.v17)],
|
||||
products: [
|
||||
// Products define the executables and libraries a package produces, making them visible to other packages.
|
||||
.library(
|
||||
name: "NetworkApi",
|
||||
targets: ["NetworkApi"]),
|
||||
],
|
||||
dependencies: [.package(url: "https://github.com/Alamofire/Alamofire", from: "5.8.0")],
|
||||
targets: [
|
||||
// Targets are the basic building blocks of a package, defining a module or a test suite.
|
||||
// Targets can depend on other targets in this package and products from dependencies.
|
||||
.target(
|
||||
name: "NetworkApi",
|
||||
dependencies: ["Alamofire"]),
|
||||
.testTarget(
|
||||
name: "NetworkApiTests",
|
||||
dependencies: ["NetworkApi"]
|
||||
),
|
||||
]
|
||||
)
|
28
Sources/NetworkApi/Common/Headers.swift
Normal file
28
Sources/NetworkApi/Common/Headers.swift
Normal file
@ -0,0 +1,28 @@
|
||||
//
|
||||
// File.swift
|
||||
// NetworkApi
|
||||
//
|
||||
// Created by User on 27.05.2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Alamofire
|
||||
|
||||
public final class Headers {
|
||||
|
||||
static func headers() -> HTTPHeaders {
|
||||
return [
|
||||
.contentType("application/json"),
|
||||
.accept("application/json")
|
||||
]
|
||||
}
|
||||
|
||||
static func headerWithToken(token: String) -> HTTPHeaders {
|
||||
return [
|
||||
.contentType("application/json"),
|
||||
.accept("application/json"),
|
||||
.authorization(bearerToken: token)
|
||||
]
|
||||
}
|
||||
|
||||
}
|
18
Sources/NetworkApi/Common/URLS.swift
Normal file
18
Sources/NetworkApi/Common/URLS.swift
Normal file
@ -0,0 +1,18 @@
|
||||
//
|
||||
// File.swift
|
||||
// NetworkApi
|
||||
//
|
||||
// Created by User on 27.05.2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct URLS {
|
||||
static let baseURL = "https://api.matule.ru/api/collections"
|
||||
static let register = "users/records"
|
||||
static let getUserInfo = "users/records/"
|
||||
static let updateUserInfo = "users/records/"
|
||||
static let login = "users/auth-with-password"
|
||||
static let getIdToken = "_authOrigins/records"
|
||||
static let deleteUser = "_authOrigins/records/"
|
||||
}
|
29
Sources/NetworkApi/Interfaces/BaseNetworkService.swift
Normal file
29
Sources/NetworkApi/Interfaces/BaseNetworkService.swift
Normal file
@ -0,0 +1,29 @@
|
||||
// The Swift Programming Language
|
||||
// https://docs.swift.org/swift-book
|
||||
|
||||
import Alamofire
|
||||
import Foundation
|
||||
|
||||
public final class BaseNetworkService: BaseNetworkServiceProtocol {
|
||||
|
||||
public var baseURL: String?
|
||||
|
||||
public func execute(path: String, method: HTTPMethod, parameters: Parameters, headers: HTTPHeaders) async throws -> Data {
|
||||
guard let baseURL = baseURL else {
|
||||
fatalError("Error baseURL")
|
||||
}
|
||||
|
||||
let stringURL = baseURL + "/" + path
|
||||
|
||||
let data = try await AF.request(stringURL, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers).serializingData().value
|
||||
|
||||
return data
|
||||
|
||||
}
|
||||
|
||||
public func configure(baseURL: String) {
|
||||
self.baseURL = baseURL
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
// The Swift Programming Language
|
||||
// https://docs.swift.org/swift-book
|
||||
|
||||
import Alamofire
|
||||
import Foundation
|
||||
|
||||
public protocol BaseNetworkServiceProtocol {
|
||||
func execute(path: String, method: HTTPMethod, headers: HTTPHeaders, parameters: Parameters) async throws -> Data
|
||||
func configure(baseURL: String)
|
||||
}
|
44
Sources/NetworkApi/Models/ServerResponseAuth.swift
Normal file
44
Sources/NetworkApi/Models/ServerResponseAuth.swift
Normal file
@ -0,0 +1,44 @@
|
||||
//
|
||||
// File.swift
|
||||
// NetworkApi
|
||||
//
|
||||
// Created by User on 27.05.2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
//{
|
||||
// "record": {
|
||||
// "collectionId": "_pb_users_auth_",
|
||||
// "collectionName": "users",
|
||||
// "created": "2025-05-27 06:07:03.550Z",
|
||||
// "dateBirthday": "",
|
||||
// "email": "exwefwefample@test.ru",
|
||||
// "emailVisibility": false,
|
||||
// "firstname": "",
|
||||
// "gender": "",
|
||||
// "id": "0et69a89t2nyvvc",
|
||||
// "lastname": "",
|
||||
// "secondname": "",
|
||||
// "updated": "2025-05-27 06:07:03.550Z",
|
||||
// "verified": false
|
||||
// },
|
||||
// "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb2xsZWN0aW9uSWQiOiJfcGJfdXNlcnNfYXV0aF8iLCJleHAiOjE3NDg5MzEwOTQsImlkIjoiMGV0NjlhODl0Mm55dnZjIiwicmVmcmVzaGFibGUiOnRydWUsInR5cGUiOiJhdXRoIn0.UlCEyQVSAQ51Euo1UGGIol6s9oqIPUG4kOOFZ9gWgwE"
|
||||
//}
|
||||
|
||||
public struct ServerResponseAuth: Decodable {
|
||||
let record: RecordAuth
|
||||
let token: String
|
||||
|
||||
struct RecordAuth: Decodable {
|
||||
let collectionId: String
|
||||
let collectionName: String
|
||||
let dateBirthday: String
|
||||
let email: String
|
||||
let firstname: String
|
||||
let gender: String
|
||||
let id: String
|
||||
let lastname: String
|
||||
let secondname: String
|
||||
}
|
||||
}
|
17
Sources/NetworkApi/Models/ServerResponseIdToken.swift
Normal file
17
Sources/NetworkApi/Models/ServerResponseIdToken.swift
Normal file
@ -0,0 +1,17 @@
|
||||
//
|
||||
// File.swift
|
||||
// NetworkApi
|
||||
//
|
||||
// Created by User on 27.05.2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct ServerResponseIdToken: Decodable {
|
||||
let items: IdToken
|
||||
|
||||
struct IdToken: Decodable {
|
||||
let id: String
|
||||
}
|
||||
}
|
||||
|
34
Sources/NetworkApi/Models/ServerResponseRegister.swift
Normal file
34
Sources/NetworkApi/Models/ServerResponseRegister.swift
Normal file
@ -0,0 +1,34 @@
|
||||
//
|
||||
// File.swift
|
||||
// NetworkApi
|
||||
//
|
||||
// Created by User on 27.05.2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
//{
|
||||
// "collectionId": "_pb_users_auth_",
|
||||
// "collectionName": "users",
|
||||
// "created": "2025-05-27 06:07:03.550Z",
|
||||
// "dateBirthday": "",
|
||||
// "emailVisibility": false,
|
||||
// "firstname": "",
|
||||
// "gender": "",
|
||||
// "id": "0et69a89t2nyvvc",
|
||||
// "lastname": "",
|
||||
// "secondname": "",
|
||||
// "updated": "2025-05-27 06:07:03.550Z",
|
||||
// "verified": false
|
||||
//}
|
||||
|
||||
public struct ServerResponseRegister: Decodable {
|
||||
let collectionId: String
|
||||
let collectionName: String
|
||||
let dateBirthday: String
|
||||
let firstname: String
|
||||
let gender: String
|
||||
let id: String
|
||||
let lastname: String
|
||||
let secondname: String
|
||||
}
|
61
Sources/NetworkApi/NetworkLayer/Auth/NetworkAuth.swift
Normal file
61
Sources/NetworkApi/NetworkLayer/Auth/NetworkAuth.swift
Normal file
@ -0,0 +1,61 @@
|
||||
//
|
||||
// File.swift
|
||||
// NetworkApi
|
||||
//
|
||||
// Created by User on 27.05.2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public final class NetworkUser: NetworkUserProtocol {
|
||||
|
||||
let baseUrl: BaseNetworkServiceProtocol
|
||||
|
||||
init(baseUrl: BaseNetworkServiceProtocol) {
|
||||
self.baseUrl = baseUrl
|
||||
}
|
||||
|
||||
public func login(identity: String, password: String) async throws -> ServerResponseAuth {
|
||||
let data = try await baseUrl.execute(path: URLS.login, method: .post, headers: Headers.headers(), parameters: [
|
||||
"identity": identity,
|
||||
"password": password
|
||||
])
|
||||
return try JSONDecoder().decode(ServerResponseAuth.self, from: data)
|
||||
}
|
||||
|
||||
public func register(email: String, password: String, passwordConfirm: String, firstname: String, lastname: String, secondname: String, datebirthday: String, gender: String) async throws -> ServerResponseRegister {
|
||||
let data = try await baseUrl.execute(path: URLS.register, method: .post, headers: Headers.headers(), parameters: [
|
||||
"email": email,
|
||||
"password": password,
|
||||
"passwordConfirm": passwordConfirm
|
||||
])
|
||||
let result = try JSONDecoder().decode(ServerResponseRegister.self, from: data)
|
||||
let addUserResult = try await addUser(idUser: result.id, email: email, firstname: firstname, lastname: lastname, secondname: secondname, datebirthday: datebirthday, gender: gender)
|
||||
return addUserResult
|
||||
}
|
||||
|
||||
public func addUser(idUser: String, email: String, firstname: String, lastname: String, secondname: String, datebirthday: String, gender: String) async throws -> ServerResponseRegister {
|
||||
let data = try await baseUrl.execute(path: URLS.login, method: .put, headers: Headers.headerWithToken(token: ""), parameters: [
|
||||
"email" : email,
|
||||
"emailVisibility" : true,
|
||||
"firstname" : firstname,
|
||||
"lastname" : lastname,
|
||||
"secondname" : secondname,
|
||||
"datebirthday" : datebirthday,
|
||||
"gender" : gender
|
||||
])
|
||||
return try JSONDecoder().decode(ServerResponseRegister.self, from: data)
|
||||
}
|
||||
|
||||
public func getIdToken(token: String) async throws -> String {
|
||||
let data = try await baseUrl.execute(path: URLS.getIdToken, method: .get, headers: Headers.headerWithToken(token: token), parameters: ["":""])
|
||||
let tokenID = try JSONDecoder().decode(ServerResponseIdToken.self, from: data)
|
||||
return tokenID.items.id
|
||||
}
|
||||
|
||||
public func deleteUser(idToken: String, token: String) async throws {
|
||||
let data = try await baseUrl.execute(path: URLS.deleteUser, method: .delete, headers: Headers.headerWithToken(token: token), parameters: ["id_token":idToken])
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
//
|
||||
// File.swift
|
||||
// NetworkApi
|
||||
//
|
||||
// Created by User on 27.05.2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public protocol NetworkUserProtocol {
|
||||
func login(identity: String, password: String) async throws -> ServerResponseAuth
|
||||
func register(email: String, password: String, passwordConfirm: String, firstname: String, lastname: String, secondname: String, datebirthday: String, gender: String) async throws -> ServerResponseRegister
|
||||
func addUser(idUser: String, email: String, firstname: String, lastname: String, secondname: String, datebirthday: String, gender: String) async throws -> ServerResponseRegister
|
||||
func getIdToken(token: String) async throws -> String
|
||||
func deleteUser(idToken: String, token: String) async throws
|
||||
}
|
12
Tests/NetworkApiTests/NetworkApiTests.swift
Normal file
12
Tests/NetworkApiTests/NetworkApiTests.swift
Normal file
@ -0,0 +1,12 @@
|
||||
import XCTest
|
||||
@testable import NetworkApi
|
||||
|
||||
final class NetworkApiTests: XCTestCase {
|
||||
func testExample() throws {
|
||||
// XCTest Documentation
|
||||
// https://developer.apple.com/documentation/xctest
|
||||
|
||||
// Defining Test Cases and Test Methods
|
||||
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user