Javascript 如何使用google登录创建用户ID
我想在新用户使用GoogleAuth登录后,向我的mysql数据库添加一个用户ID。这篇文档暗示了我应该做什么,但我不确定细节。到目前为止,我想我已经把大部分步骤拼凑在一起了,但我在不断学习 一切正常,但我不确定下一步该怎么做。那么假设我的数据库中不存在用户,我具体用什么来创建一个新的用户ID呢 这是我能找到的关于我想做什么的最接近的文档,但它很模糊 如果用户尚未在用户数据库中,请创建新的用户记录 根据ID令牌有效负载中的信息,建立会话 对于用户 我的前端:Javascript 如何使用google登录创建用户ID,javascript,node.js,google-signin,Javascript,Node.js,Google Signin,我想在新用户使用GoogleAuth登录后,向我的mysql数据库添加一个用户ID。这篇文档暗示了我应该做什么,但我不确定细节。到目前为止,我想我已经把大部分步骤拼凑在一起了,但我在不断学习 一切正常,但我不确定下一步该怎么做。那么假设我的数据库中不存在用户,我具体用什么来创建一个新的用户ID呢 这是我能找到的关于我想做什么的最接近的文档,但它很模糊 如果用户尚未在用户数据库中,请创建新的用户记录 根据ID令牌有效负载中的信息,建立会话 对于用户 我的前端: function onSignIn
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
var id_token = googleUser.getAuthResponse().id_token;
var userName = profile.getName();
var userEmail = profile.getEmail();
var image = profile.getImageUrl();
console.log('new user is:', userName,'the ID is',id_token, 'the email is: ', userEmail, 'and the picture is', image)
// POST NEW USER TO BACK END;
var postUrl = `http://localhost:80/newUser`
// Send the data using post;
try{
$.post(postUrl, {token: id_token, userName: userName, userEmail: userEmail, userProfilePicture: image })
.done(function (data) {
console.log('success sign up clause- data posted to backend')
});
console.log('hello')
}catch(err){
console.log('failed to post to backend')
response.send('Error: ' + err)
}
}
const CLIENT_ID = 'MyPresumablyPrivateInfo.apps.googleusercontent.com' const {OAuth2Client} = require('google-auth-library'); const client = new OAuth2Client(CLIENT_ID);
app.post('/newUser', async (request, response) => {
console.log(request.body)
var token = request.body.token
var NewuserName = request.body.userName
var NewuserEmail = request.body.userEmail
//token = NewUserId
// Check authenticity of user id
const client = new OAuth2Client(CLIENT_ID);
async function verify() {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID,
});
const payload = ticket.getPayload();
const userid = payload['sub'];
console.log('id verified!')
}
verify().catch('error caught:', console.error);
});
我的后端:
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
var id_token = googleUser.getAuthResponse().id_token;
var userName = profile.getName();
var userEmail = profile.getEmail();
var image = profile.getImageUrl();
console.log('new user is:', userName,'the ID is',id_token, 'the email is: ', userEmail, 'and the picture is', image)
// POST NEW USER TO BACK END;
var postUrl = `http://localhost:80/newUser`
// Send the data using post;
try{
$.post(postUrl, {token: id_token, userName: userName, userEmail: userEmail, userProfilePicture: image })
.done(function (data) {
console.log('success sign up clause- data posted to backend')
});
console.log('hello')
}catch(err){
console.log('failed to post to backend')
response.send('Error: ' + err)
}
}
const CLIENT_ID = 'MyPresumablyPrivateInfo.apps.googleusercontent.com' const {OAuth2Client} = require('google-auth-library'); const client = new OAuth2Client(CLIENT_ID);
app.post('/newUser', async (request, response) => {
console.log(request.body)
var token = request.body.token
var NewuserName = request.body.userName
var NewuserEmail = request.body.userEmail
//token = NewUserId
// Check authenticity of user id
const client = new OAuth2Client(CLIENT_ID);
async function verify() {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID,
});
const payload = ticket.getPayload();
const userid = payload['sub'];
console.log('id verified!')
}
verify().catch('error caught:', console.error);
});
我会假设用户的电子邮件是唯一的,所以我会做的是散列用户的电子邮件 并将其用作id
我通常使用,因为它可以帮助您以后在其他事情中使用gavatar使用令牌负载中的
sub
键(不建议使用电子邮件,因为它可能会更改)
用户的标识符,在所有Google帐户中是唯一的,永远不会
重复使用。一个谷歌账户可以在不同的地点有多封电子邮件
时间,但子值从未更改。在您的应用程序中使用sub
应用程序作为用户的唯一标识符密钥
我想到了这一点,但这似乎是一种黑客手段,不是吗?你为什么这么认为?正如我在问题中引用的,谷歌文档特别建议如下:“从ID令牌有效负载中的信息创建一个新的用户记录”-因此,将你的建议称为“黑客”可能过于苛刻,但这并不是谷歌所建议的策略,考虑到我是一个新手,而且这些东西很重要,我想“照本宣科”做,进一步支持这一点: