当前位置: 首页 > news >正文

uni-id-pages login-by-google实现

 login-by-google uni-cloud团队没实现,现开源login-by-google的实现

/*** Google登录* @param {Object} params* @param {Object} params.googleInfo - Google用户信息* @param {String} params.googleInfo.id - Google用户唯一ID* @param {String} params.googleInfo.name - 用户全名* @param {String} params.googleInfo.email - 用户邮箱* @param {String} params.googleInfo.picture - 用户头像URL* @param {String} params.googleInfo.given_name - 名字* @param {String} params.googleInfo.family_name - 姓氏* @param {Boolean} params.googleInfo.verified_email - 邮箱是否已验证* @returns {Object} 登录成功后的用户信息和token*/
const {userCollection,db,dbCmd,LOG_TYPE
} = require('../../common/constants')
const {ERROR
} = require('../../common/error')module.exports = async function (params = {}) {try {const { uniIdCommon } = thisconsole.log('Login by Google params received:', JSON.stringify(params))// Check if params is properly structuredif (!params || typeof params !== 'object') {console.error('Invalid params format:', params)throw new Error('Invalid params format')}// Ensure googleInfo exists, using multiple strategies to find valid Google infolet googleInfo = null// Strategy 1: Check if googleInfo is a nested propertyif (params.googleInfo && typeof params.googleInfo === 'object') {googleInfo = params.googleInfoconsole.log('Found googleInfo as nested property')} // Strategy 2: Check if params itself contains the Google IDelse if (params.id) {googleInfo = {id: params.id,name: params.name,email: params.email,picture: params.picture,given_name: params.given_name,family_name: params.family_name,verified_email: params.verified_email}console.log('Extracted googleInfo directly from params')}// Strategy 3: Check if any property of params might contain the Google infoelse {// Look for any property that might be the Google info objectfor (const key in params) {const prop = params[key]if (prop && typeof prop === 'object' && prop.id) {googleInfo = propconsole.log(`Found googleInfo in params.${key}`)break}}}// Final validation of Google infoif (!googleInfo || !googleInfo.id) {console.error('Google info validation failed:', JSON.stringify(googleInfo || params))throw new Error('Google info is required - no valid Google user ID found')}console.log('Using Google Info:', JSON.stringify(googleInfo))// 查询是否已存在此Google账号的用户const userRecord = await userCollection.where({'google.id': googleInfo.id}).get()console.log('User lookup result:', JSON.stringify(userRecord))let uidif (userRecord && userRecord.data && userRecord.data.length > 0) {// 已存在用户,更新Google信息uid = userRecord.data[0]._idconsole.log('Existing user found, updating Google info for user:', uid)await userCollection.doc(uid).update({google: {id: googleInfo.id,name: googleInfo.name || '',email: googleInfo.email || '',picture: googleInfo.picture || '',given_name: googleInfo.given_name || '',family_name: googleInfo.family_name || '',verified_email: googleInfo.verified_email || false},last_login_date: Date.now(),last_login_ip: this.getUniversalClientInfo().clientIP})} else {// 不存在用户,创建新用户console.log('No existing user found, creating new user with Google info')try {const insertResult = await userCollection.add({google: {id: googleInfo.id,name: googleInfo.name || '',email: googleInfo.email || '',picture: googleInfo.picture || '',given_name: googleInfo.given_name || '',family_name: googleInfo.family_name || '',verified_email: googleInfo.verified_email || false},nickname: googleInfo.name || 'Google User', // 使用Google名称作为昵称email: googleInfo.email || '', // 设置邮箱avatar: googleInfo.picture || '', // 设置头像register_date: Date.now(),register_ip: this.getUniversalClientInfo().clientIP,last_login_date: Date.now(),last_login_ip: this.getUniversalClientInfo().clientIP})console.log('New user created, insert result:', JSON.stringify(insertResult))uid = insertResult.id} catch (err) {console.error('Error creating new user:', err)throw new Error('Failed to create new user: ' + (err.message || JSON.stringify(err)))}}if (!uid) {throw new Error('Failed to get or create user ID')}// 记录登录日志await this.middleware.uniIdLog({data: {user_id: uid},type: LOG_TYPE.LOGIN})// 生成token - 使用uniIdCommon的createToken方法console.log('Generating token for user:', uid)let tokenInfotry {tokenInfo = await uniIdCommon.createToken({uid})console.log('Token generated:', tokenInfo)} catch (err) {console.error('Error creating token:', err)throw new Error('Failed to create token: ' + (err.message || JSON.stringify(err)))}// 获取用户完整信息let userInfotry {const userResult = await userCollection.doc(uid).get()if (!userResult.data || userResult.data.length === 0) {throw new Error('User data not found')}userInfo = userResult.data[0]// 返回用户信息和tokenconst result = {...tokenInfo,uid,userInfo: {...userInfo,token: tokenInfo.token,tokenExpired: tokenInfo.tokenExpired}}console.log('Login success, returning result')return result} catch (err) {console.error('Error getting user info:', err)throw new Error('Failed to get user info: ' + (err.message || JSON.stringify(err)))}} catch (err) {console.error('Login by Google failed:', err)throw err}
}

http://www.xdnf.cn/news/771769.html

相关文章:

  • 05.MySQL表的约束
  • 使用免费wordpress成品网站模板需要注意点什么
  • NX847NX855美光固态闪存NX862NX865
  • 向量空间的练习题目
  • 前端高频面试题2:JavaScript/TypeScript
  • SOC-ESP32S3部分:26-物联网MQTT连云
  • 《深度剖析:基于Meta的GameFormer构建自博弈AI游戏代理》
  • 趋势因子均值策略思路
  • 基于STM32的循迹避障小车的Proteus仿真设计
  • hot100 -- 6.矩阵系列
  • Linux(信号)
  • 第十天:Java反射
  • 灵动相册,助力教学:在线教学场景的图片管理利器——仙盟引渡仙君-智能编程——仙盟创梦IDE
  • 通俗理解“高内聚,低耦合”
  • react 生命周期
  • “等待-通知”机制优化(一次性申请)循环等待
  • 电子电路:4017计数器工作原理解析
  • 【Java EE初阶】计算机是如何⼯作的
  • “application/json“,“text/plain“ 分别表示什么
  • 【C#朗读文本DLL动态按钮控件组及按钮事件文本框拖放数据】2022-1-21
  • 【Go-补充】ioReader + ioWriter + bufio
  • Charles青花瓷抓取外网数据包
  • 【笔记】Windows 部署 Suna 开源项目完整流程记录
  • 格密码-LWE问题
  • Python基础:人生重开模拟器(小游戏)
  • 编译原理实验 之 TINY 之 语义分析(第二次作业)
  • PHP舆情监控分析系统(9个平台)
  • 机器学习:支持向量机(SVM)原理解析及垃圾邮件过滤实战
  • Qt信号与槽机制深度解析
  • 【Godot】如何导出 Release 版本的安卓项目