Login
ViewController.swift
import Cocoa extension String { func fromBase64() -> String? { guard let data = Data(base64Encoded: self) else { return nil } return String(data: data, encoding: .utf8) } func toBase64() -> String { return Data(self.utf8).base64EncodedString() } } class ViewController: NSViewController , NSWindowDelegate { var userInfo:UserInfo = UserInfo() struct UserInfo : Decodable{ var email:String="" var username:String="" var password:String="" var token:String="" var status:String="" } func check_login(username:String,password:String) { let urlString = "https://programmerdesign.com/go_through/site/login" let str = username+":"+password; let utf8str = str.data(using: .utf8) if let base64Encoded = utf8str?.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) { if let url = URL(string:urlString) { var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.setValue("Powered by Swift!", forHTTPHeaderField: "X-Powered-By") request.setValue("Basic "+base64Encoded, forHTTPHeaderField: "Authorization") let json = [ "test": "test", ] let jsonData = try! JSONSerialization.data(withJSONObject: json, options: []) let task = URLSession.shared.uploadTask(with: request ,from:jsonData, completionHandler:{ data, response, error in // Check the response let decoder = JSONDecoder() do{ let user = try decoder.decode(UserInfo.self,from:data!) self.userInfo.token = user.token self.userInfo.email = user.email self.userInfo.username = user.username self.userInfo.status = user.status DispatchQueue.main.async(){ if(self.userInfo.status == "true"){ UserDefaults.standard.set(self.userInfo.token,forKey:"token") UserDefaults.standard.synchronize() self.performSegue(withIdentifier:NSStoryboardSegue.Identifier("GoToMain"),sender:self) }else{ print("Invalid Username or Password") } } }catch{ DispatchQueue.main.async(){ print("Invalid Username or Password") } } }) task.resume() } } }
SignUp
ViewController.swift
import Cocoa class ViewController: NSViewController , NSWindowDelegate { struct Response:Decodable{ var status:Int = -1 var errors:String = "" } func registerUser(email:String,username:String,password:String,confirmPassword:String){ let urlString = "https://programmerdesign.com/go_through/site/signup" if let url = URL(string:urlString) { var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.setValue("Powered by Swift!", forHTTPHeaderField: "X-Powered-By") let json = [ "email": email, "username":username, "password":password ] let jsonData = try! JSONSerialization.data(withJSONObject: json, options: []) let task = URLSession.shared.uploadTask(with: request ,from:jsonData, completionHandler:{ data, response, error in // Check the response let decoder = JSONDecoder() do{ let rs = try decoder.decode(Response.self,from:data!) if(rs.status == 1){ DispatchQueue.main.async{ //set input empty value } print("กรุณายืนยัน email ของคุณแล้วทำการ Login เพื่อเข้าสู่ระบบ.") }else{ print("Error.") } }catch{ DispatchQueue.main.async(){ print("Error.") } } }) task.resume() } } }