[英]Sending a simple string from iOS app to Node.js server and decoding it
I have an iOS app and I have to sign in the user into the Firebase Database with their Linkedin account. 我有一个iOS应用程序,我必须使用其Linkedin帐户登录用户到Firebase数据库。 I have already managed to sign them into my app with their Linkedin and so I have the user info. 我已经设法使用他们的Linkedin将他们登录到我的应用程序中,因此我拥有了用户信息。
According to Firebase, what I have to do is to send the user id to my server, where I have to mint a custom token, which I then have to send back to the app and sign the user in with the custom token. 根据Firebase的介绍,我要做的是将用户ID发送到我的服务器,在这里我必须铸造一个自定义令牌,然后我必须将其发送回应用程序并使用自定义令牌登录用户。
I have set up a Node.js server, I managed to send the user id to the server. 我已经设置了一个Node.js服务器,我设法将用户ID发送到服务器。 I'm trying to send back the user id just to see that everything works, and then I will be able to mint the token by following the related tutorial on Firebase. 我试图发回用户ID只是为了查看一切正常,然后我将能够按照Firebase上的相关教程创建令牌。
This is my code so far in the iOS app: 到目前为止,这是我在iOS应用中的代码:
func sendInfoToServer() {
let uid = "this-is-the-user-id"
let linkedinUserID = uid.data(using: .utf8)
let apiRoute = "the-address-to-my-server.com"
guard let url = URL(string: "https://\(apiRoute)/LItoken") else {
return
}
var urlRequest = URLRequest(url: url, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 30.0)
urlRequest.httpMethod = "POST"
urlRequest.httpBody = linkedinUserID
let session = URLSession(configuration: URLSessionConfiguration.default)
let task: URLSessionDataTask = session.dataTask(with: urlRequest) { (data, res, err) in
guard data != nil, err == nil else {return}
guard let response = String(data: data!, encoding: .utf8) else {return}
print("this is the response from server: \(response)")
}
task.resume()
}
And this is what I have in Node.js: 这就是我在Node.js中拥有的:
const express = require('express')
const app = express()
const morgan = require('morgan')
const bodyParser = require('body-parser')
const utf8 = require('utf8')
app.use(morgan('combined'))
app.use(bodyParser.urlencoded({
extended: true
}))
app.post('/LItoken', (req, res) => {
const linkID = req.body
const decodedID = utf8.decode(linkID)
res.send("This is the decoded id" + decodedID)
})
const PORT = process.env.PORT || 3003
app.listen(PORT, () => {
console.log("Server is up on" + PORT)
})
When I print the response from the server in the iOS app I just get an empty string. 当我在iOS应用中从服务器打印响应时,我只会得到一个空字符串。
I have absolutely no experience with server-side development or Node.js so please forgive me if my question is very silly or if I'm missing something very basic. 我绝对没有服务器端开发或Node.js的经验,因此,如果我的问题很愚蠢或缺少一些基本知识,请原谅我。
Thanks in advance for any help! 在此先感谢您的帮助!
I eventually solved this problem as follows: the linkID const had to be declared as req.body.lid
to actually refer to the linkedin ID. 我最终解决了以下问题:必须将linkID const声明为req.body.lid
才能实际引用linkedin ID。
Then I required the firebase-admin sdk in my js file, and got the custom token with the following code: 然后,在我的js文件中需要firebase-admin sdk,并使用以下代码获取了自定义令牌:
admin.auth().createCustomToken(linkID)
.then((customToken) => {
res.json({
customToken
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.