change clientid of ios; save access token and refresh token for discord login

This commit is contained in:
CounterFire2023 2024-02-18 14:57:39 +08:00
parent 4ce584836b
commit c06321e56d
2 changed files with 30 additions and 3 deletions

View File

@ -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<any> {

View File

@ -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