From c06321e56db10b35ee84ea49db588bf8824fb55a Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Sun, 18 Feb 2024 14:57:39 +0800 Subject: [PATCH] change clientid of ios; save access token and refresh token for discord login --- src/plats/PlatApple.ts | 2 +- src/plats/PlatDiscord.ts | 31 +++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/plats/PlatApple.ts b/src/plats/PlatApple.ts index 46b4d30..5addeaf 100644 --- a/src/plats/PlatApple.ts +++ b/src/plats/PlatApple.ts @@ -3,7 +3,7 @@ import verifyAppleToken from 'verify-apple-id-token' const CLIENT_ID_DEBUG = 'com.jc.tebg' const CLIENT_ID_RELEASE = 'com.cege.games.release' -const CLIEND_ID_ANDROID = 'wallet.cebggame.com' +const CLIEND_ID_ANDROID = 'wallet.counterfire.games' export class PlatApple implements IPlat { async verifyToken(req: any): Promise { diff --git a/src/plats/PlatDiscord.ts b/src/plats/PlatDiscord.ts index 920a8d3..c937ae7 100644 --- a/src/plats/PlatDiscord.ts +++ b/src/plats/PlatDiscord.ts @@ -2,13 +2,14 @@ import { ZError } from 'zutils' import { IPlat } from './IPlat' import { handleFetch } from 'zutils/utils/net.util' +const EXPIRE_REDUCE_SECOND = 3600 export async function exchangeDiscrodCodeForToken(code: string) { const clientId = process.env.DISCORD_CLIENT_ID const clientSecret = process.env.DISCORD_CLIENT_SECRET const redirectUri = process.env.DISCORD_REDIRECT_URI - const data = await handleFetch('https://discord.com/api/oauth2/token', { + const data = await handleFetch('https://discord.com/api/v10/oauth2/token', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', @@ -22,7 +23,30 @@ export async function exchangeDiscrodCodeForToken(code: string) { scope: 'identify email', }), }) - + + return data +} + +export async function refreshDiscordToken(refreshToken: string) { + const clientId = process.env.DISCORD_CLIENT_ID + const clientSecret = process.env.DISCORD_CLIENT_SECRET + const redirectUri = process.env.DISCORD_REDIRECT_URI + + const data = await handleFetch('https://discord.com/api/v10/oauth2/token', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: new URLSearchParams({ + grant_type: 'refresh_token', + client_id: clientId, + client_secret: clientSecret, + redirect_uri: redirectUri, + refresh_token: refreshToken, + scope: 'identify email', + }), + }) + return data } @@ -46,6 +70,9 @@ export class PlatDiscord implements IPlat { let payload = await userInfo(tokenResponse.access_token) const openId = payload.id let data: any = {} + data.accessToken = tokenResponse['access_token'] + data.refreshToken = tokenResponse['refresh_token'] + data.accessTokenExpire = ((Date.now() / 1000) | 0) + tokenResponse['expires_in'] - EXPIRE_REDUCE_SECOND if (payload.username) data.nickname = payload.username if (payload.avatar) data.avatar = payload.avatar if (payload.email) data.email = payload.email