Created
October 21, 2020 02:04
-
-
Save mushikago/03a5548d84cc168fcc213373c47c67e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// Hoge | |
// | |
// Created by Tetsuya Shiraishi on 2020/09/18. | |
// Copyright © 2020 MUSHIKAGO DESIGN STUDIO CO., LTD. All rights reserved. | |
// | |
import SwiftUI | |
import LineSDK | |
import Firebase | |
import GTMSessionFetcher | |
struct ContentView: View { | |
@State var errorMsg = "---" | |
var body: some View { | |
VStack { | |
Text(self.errorMsg).padding() | |
HStack { | |
Text("hoge") | |
Text("fuga") | |
} | |
Button(action: {LoginManager.shared.login(permissions: [.profile], in: self.body as? UIViewController) { | |
result in | |
switch result { | |
case .success(let loginResult): | |
let customToken = loginResult.accessToken.value | |
requestFirebaseAuthTokenWithLINEAccessToken(lineAccessToken: customToken) | |
// Do other things you need with the login result | |
case .failure(let error): | |
self.errorMsg = error.errorDescription ?? "some error" | |
} | |
}}) { | |
Text("Line Login") | |
.padding(.all, 20.0) | |
} | |
} | |
} | |
} | |
private func requestFirebaseAuthTokenWithLINEAccessToken(lineAccessToken:String){ | |
let LineSDKConfig = Bundle.main.infoDictionary!["LineSDKConfig"] as! Dictionary<String,AnyObject> | |
let _validationServerDomain = LineSDKConfig["ValidationServerDomain"] as! String | |
var _firebaseTokenFetcher:GTMSessionFetcher? | |
if let fFetcher = _firebaseTokenFetcher{ | |
if fFetcher.isFetching { | |
fFetcher.stopFetching() | |
} | |
} | |
let urlString = _validationServerDomain + "/verifyToken" | |
let url = URL(string: urlString) | |
var request = URLRequest(url: url!) //NSMutableURLRequest(url: url!) | |
request.httpMethod = "POST" | |
request.setValue("application/json", forHTTPHeaderField: "content-type") | |
let params: [String: String] = [ | |
"token": lineAccessToken | |
] | |
do { | |
let requestBody = try JSONSerialization.data(withJSONObject: params, options: []) | |
let jsonStr = String(bytes: requestBody, encoding: .utf8)! | |
print(jsonStr) | |
request.httpBody = requestBody | |
_firebaseTokenFetcher = GTMSessionFetcher.init(request: request) | |
_firebaseTokenFetcher?.beginFetch(completionHandler: { (data:Data?, error:Error?) in | |
if let _data = data{ | |
if let jsonStr = String(data: _data, encoding: String.Encoding.utf8){ | |
if let _data = jsonStr.data(using: .utf8){ | |
do { | |
let dic = try JSONSerialization.jsonObject(with: _data, options: []) as! [String: String] | |
let firebaseToken = dic["firebase_token"] | |
authenticateWithFirebaseToken(firebaseToken: firebaseToken!) | |
} catch { | |
print(error.localizedDescription) | |
} | |
} | |
} | |
}else{ | |
print("data is empty....") | |
} | |
}) | |
} catch (let e) { | |
print(e) | |
} | |
} | |
private func authenticateWithFirebaseToken(firebaseToken:String){ | |
Auth.auth().signIn(withCustomToken: firebaseToken) { (result:AuthDataResult?, error:Error?) in | |
print(result?.user.uid) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment