add general oauth redirect
This commit is contained in:
parent
3465a63d06
commit
b49bed2bc5
@ -7,6 +7,7 @@ import logger from 'logger/logger'
|
|||||||
import config from 'config/config'
|
import config from 'config/config'
|
||||||
import { ConnectOptions } from 'mongoose'
|
import { ConnectOptions } from 'mongoose'
|
||||||
import { DiscordSvr } from 'services/discord.svr'
|
import { DiscordSvr } from 'services/discord.svr'
|
||||||
|
import DiscordSchedule from 'schedule/discord.schedule'
|
||||||
|
|
||||||
const zReqParserPlugin = require('plugins/zReqParser')
|
const zReqParserPlugin = require('plugins/zReqParser')
|
||||||
|
|
||||||
@ -93,10 +94,12 @@ export class ApiServer {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
initSchedules() {}
|
initSchedules() {
|
||||||
|
// new DiscordSchedule().scheduleAll()
|
||||||
|
}
|
||||||
|
|
||||||
initServcers() {
|
initServcers() {
|
||||||
new DiscordSvr().init()
|
// new DiscordSvr().init()
|
||||||
}
|
}
|
||||||
|
|
||||||
async connectDB() {
|
async connectDB() {
|
||||||
|
@ -117,4 +117,22 @@ class MainController extends BaseController {
|
|||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@role(ROLE_ANON)
|
||||||
|
@router('get /activity/discord/svr/:id')
|
||||||
|
async checkDiscordJoin(req) {
|
||||||
|
let { id } = req.params
|
||||||
|
checkSign(req.params);
|
||||||
|
let verified = new DiscordSvr().checkUser(id)
|
||||||
|
return {verified}
|
||||||
|
}
|
||||||
|
|
||||||
|
@role(ROLE_ANON)
|
||||||
|
@router('get /activity/discord/role/:id')
|
||||||
|
async checkDiscordRole(req) {
|
||||||
|
let { id } = req.params
|
||||||
|
checkSign(req.params);
|
||||||
|
let verified = new DiscordSvr().checkUserRole(id)
|
||||||
|
return {verified}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
20
src/controllers/oauth.controller.ts
Normal file
20
src/controllers/oauth.controller.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
import BaseController, { ROLE_ANON } from 'common/base.controller'
|
||||||
|
import { role, router } from 'decorators/router'
|
||||||
|
|
||||||
|
class OauthController extends BaseController {
|
||||||
|
|
||||||
|
@role(ROLE_ANON)
|
||||||
|
@router('get /oauth/redirect')
|
||||||
|
@router('post /oauth/redirect')
|
||||||
|
async appleWebLoginCb(req, res) {
|
||||||
|
let { code, state, error, token, id_token } = req.params
|
||||||
|
code = code || token
|
||||||
|
console.log(`code: ${code}, state: ${state}, error: ${error}`)
|
||||||
|
if (error) {
|
||||||
|
res.redirect(`cfoauthcb://login_result?state=${state}&error=${JSON.stringify(error)}`)
|
||||||
|
} else {
|
||||||
|
res.redirect(`cfoauthcb://login_result?token=${id_token || code}&state=${state}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
src/schedule/discord.schedule.ts
Normal file
30
src/schedule/discord.schedule.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { singleton } from 'decorators/singleton'
|
||||||
|
import * as schedule from 'node-schedule'
|
||||||
|
import { DiscordSvr } from 'services/discord.svr';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定时更新discord缓存
|
||||||
|
*/
|
||||||
|
@singleton
|
||||||
|
export default class DiscordSchedule {
|
||||||
|
private running: boolean = false;
|
||||||
|
async updateCache() {
|
||||||
|
if (this.running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
this.running = true;
|
||||||
|
await new DiscordSvr().updateCache();
|
||||||
|
this.running = false;
|
||||||
|
} catch (error) {
|
||||||
|
this.running = false;
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduleAll() {
|
||||||
|
schedule.scheduleJob('*/5 * * * * *', async () => {
|
||||||
|
await this.updateCache()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
import { ZError } from 'common/ZError'
|
import { ZError } from 'common/ZError'
|
||||||
import { singleton } from 'decorators/singleton'
|
import { singleton } from 'decorators/singleton'
|
||||||
const { Client, Events, GatewayIntentBits } = require('discord.js')
|
|
||||||
|
import { Client, Events, GatewayIntentBits, Guild } from 'discord.js'
|
||||||
|
|
||||||
export async function exchangeDiscrodCodeForToken(code: string) {
|
export async function exchangeDiscrodCodeForToken(code: string) {
|
||||||
const clientId = process.env.DISCORD_CLIENT_ID
|
const clientId = process.env.DISCORD_CLIENT_ID
|
||||||
@ -40,8 +41,8 @@ export async function userInfo(token: string) {
|
|||||||
|
|
||||||
@singleton
|
@singleton
|
||||||
export class DiscordSvr {
|
export class DiscordSvr {
|
||||||
private client: any
|
private client: Client
|
||||||
private guild: any
|
private guild: Guild
|
||||||
public async init() {
|
public async init() {
|
||||||
console.log('DiscordSvr init')
|
console.log('DiscordSvr init')
|
||||||
this.client = new Client({ intents: [GatewayIntentBits.Guilds] })
|
this.client = new Client({ intents: [GatewayIntentBits.Guilds] })
|
||||||
@ -52,11 +53,28 @@ export class DiscordSvr {
|
|||||||
this.client.login(process.env.DISCORD_BOT_TOKEN)
|
this.client.login(process.env.DISCORD_BOT_TOKEN)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async updateCache() {
|
||||||
|
console.log('update discord cache')
|
||||||
|
if (!this.guild) {
|
||||||
|
throw new ZError(10, 'DiscordSvr not init')
|
||||||
|
}
|
||||||
|
await this.guild.members.fetch()
|
||||||
|
console.log('update discord cache done')
|
||||||
|
}
|
||||||
|
|
||||||
|
public checkUser(uid: string) {
|
||||||
|
if (!this.guild) {
|
||||||
|
throw new ZError(10, 'DiscordSvr not init')
|
||||||
|
}
|
||||||
|
return this.guild.members.cache.has(uid)
|
||||||
|
}
|
||||||
|
|
||||||
public async checkUserRole(uid: string) {
|
public async checkUserRole(uid: string) {
|
||||||
if (!this.guild) {
|
if (!this.guild) {
|
||||||
throw new ZError(10, 'DiscordSvr not init')
|
throw new ZError(10, 'DiscordSvr not init')
|
||||||
}
|
}
|
||||||
const member = await this.guild.members.fetch({ user: uid, force: true })
|
// const member = await this.guild.members.fetch({ user: uid, force: true })
|
||||||
|
const member = this.guild.members.cache.get(uid)
|
||||||
if (!member) {
|
if (!member) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
27
start.json
Normal file
27
start.json
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"apps": [
|
||||||
|
{
|
||||||
|
"name": "oauth-svr",
|
||||||
|
"script": "npm",
|
||||||
|
"args": "run prod:api",
|
||||||
|
"cwd": "/home/kingsome/code/oauth-svr",
|
||||||
|
"max_memory_restart": "1024M",
|
||||||
|
"log_date_format" : "YYYY-MM-DD HH:mm Z",
|
||||||
|
"watch": true,
|
||||||
|
"ignore_watch": [
|
||||||
|
"node_modules",
|
||||||
|
"logs",
|
||||||
|
"fixtures",
|
||||||
|
"tasks"
|
||||||
|
],
|
||||||
|
"instances": 1,
|
||||||
|
"exec_mode": "fork",
|
||||||
|
"env": {
|
||||||
|
"NODE_ENV": "production"
|
||||||
|
},
|
||||||
|
"env_production": {
|
||||||
|
"NODE_ENV": "production"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -8,14 +8,14 @@
|
|||||||
"emitDecoratorMetadata": true,
|
"emitDecoratorMetadata": true,
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"target": "es2018",
|
"target": "es2020",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"baseUrl": "./src",
|
"baseUrl": "./src",
|
||||||
"rootDir": "./src"
|
"rootDir": "./src"
|
||||||
},
|
},
|
||||||
"lib": ["es2018"],
|
"lib": ["es2020"],
|
||||||
"include": [
|
"include": [
|
||||||
"src/**/*.ts",
|
"src/**/*.ts",
|
||||||
"typings/extend.d.ts"
|
"typings/extend.d.ts"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user