bla*_*ack 4 token node.js firebase firebase-authentication
我不是指使用firebase进行自定义身份验证.我需要的是与在应用程序服务器中生成令牌并允许在firebase中访问的自定义身份验证略有不同.实际上,我正在尝试使用电子邮件和密码在firebase中进行身份验证,并且使用该身份验证可以在某些应用程序服务器中访问restful服务.这可能吗 ?我认为以某种方式可以在firebase身份验证之后将令牌发送到应用程序服务器,并且该服务器将针对firebase验证身份验证令牌.
Client --------authenticates ------->> Firebase
Client <<--------auth token ---------- Firebase
Client --------- sends ------------->> Application server (NodeJS)
App Server ------- validates (auth token) ---->> Firebase
Run Code Online (Sandbox Code Playgroud)
提前致谢.
小智 14
import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
const googleAuthProvider = new firebase.auth.GoogleAuthProvider();
const auth = firebase.auth();
const authenticates = await auth.signInWithPopup(googleAuthProvider).then(user => user).catch(err => err)
Run Code Online (Sandbox Code Playgroud)
您将从authenticates响应中获取数据
authtoken = authenticates.credential.idToken
email = authenticates.user.email
...
Run Code Online (Sandbox Code Playgroud)
const sends = await axios({
method: 'post',
url: `${API_BASE_URL}/request`,
headers: {
'Authorization': `Bearer ${authtoken}`,
},
data: {
from: next_cursor,
size: next_cursor + 100,
}
});
Run Code Online (Sandbox Code Playgroud)
app_oauth2_client_id当我们初始化 firebase 身份验证时我们将会有
import { OAuth2Client } from 'google-auth-library';
const oauth2Client = new OAuth2Client(process.env.app_oauth2_client_id);
function verifyOauth2Token(token) {
const ticket = await oauth2Client.verifyIdToken({
idToken: token,
audience: [process.env.app_oauth2_client_id]
});
return ticket.getPayload();
}
const tokenInfo = await verifyOauth2Token(token);
Run Code Online (Sandbox Code Playgroud)
对于代币信息
{
iss: 'accounts.google.com',
azp: '671303332471-5n8014rorllmd09n8mmadesc2qidpda5.apps.googleusercontent.com',
aud: '671303332471-5n8014rorllmd09n8mmadesc2qidpda5.apps.googleusercontent.com',
sub: '100037911230177975416',
email: 'testapp@gmail.com',
email_verified: true,
at_hash: '3rxsMOftrr9NZWlBkYznuQ',
iat: 1635842823,
exp: 1635846423
}
Run Code Online (Sandbox Code Playgroud)
您可以使用iOS,Web和Android中提供的异步getToken方法获取令牌
网址:
https://firebase.google.com/docs/reference/js/firebase.User#getToken
iOS:https: //firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_user#properties
Android:https: //firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser.html#public-constructor-summary
并将该令牌发送到您的后端服务器,然后您可以使用服务器中的verifyIdToken方法来验证令牌并获取令牌的uid
服务器方法 https://firebase.google.com/docs/auth/server#verify_id_tokens
| 归档时间: |
|
| 查看次数: |
5130 次 |
| 最近记录: |