-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAuthenticationService.swift
67 lines (54 loc) · 2 KB
/
AuthenticationService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// AuthenticationService.swift
// MobileAcebook
//
// Created by Josué Estévez Fernández on 01/10/2023.
//
import Foundation
class AuthenticationService: AuthenticationServiceProtocol {
struct Response: Codable {
let message : String
}
func signUp(user: User) -> Bool {
guard let url = URL(string: "http://localhost:3000/users") else {return false}
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
let body = user
urlRequest.httpBody = try? JSONEncoder().encode(user)
let task = URLSession.shared.dataTask(with : urlRequest) {data, response, error in
guard let data = data else {return}
do {
let response = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
print(response)
print("User created successfully")
}
catch {
print(error)
}
}
task.resume()
return true
}
func login(userLogin: UserLogin) -> Bool {
guard let url = URL(string: "http://localhost:3000/tokens") else {return false}
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
let body = userLogin
urlRequest.httpBody = try? JSONEncoder().encode(userLogin)
let task = URLSession.shared.dataTask(with : urlRequest) {data, response, error in
guard let data = data else {return}
do {
let response = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
print("Valid user")
print(response)
}
catch {
print(error)
}
}
task.resume()
return true
}
}