44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
import { ZError } from 'common/ZError'
|
|
import BaseController, { ROLE_ANON } from 'common/base.controller'
|
|
import { role, router } from 'decorators/router'
|
|
import logger from 'logger/logger'
|
|
import { Account, PlatEnum } from 'modules/Account'
|
|
import { IPlat } from 'plats/IPlat'
|
|
import { PlatApple } from 'plats/PlatApple'
|
|
import { PlatFacebook } from 'plats/PlatFacebook'
|
|
import { PlatGoogle } from 'plats/PlatGoogle'
|
|
import { PlatTikTok } from 'plats/PlatTikTok'
|
|
|
|
const plats: Map<PlatEnum, IPlat> = new Map([
|
|
[PlatEnum.GOOGLE, new PlatGoogle()],
|
|
[PlatEnum.APPLE, new PlatApple()],
|
|
[PlatEnum.FACEBOOK, new PlatFacebook()],
|
|
[PlatEnum.TIKTOK, new PlatTikTok()],
|
|
])
|
|
|
|
class LoginController extends BaseController {
|
|
@role(ROLE_ANON)
|
|
@router('post /wallet/login/general')
|
|
async generalLogin(req, res) {
|
|
const { channel, account } = req.params
|
|
logger.db('login', req)
|
|
const plat = plats.get(channel)
|
|
if (!plat) {
|
|
throw new ZError(10, 'plat not found')
|
|
}
|
|
const { openId, data } = await plat.verifyToken(req)
|
|
const { api_platform } = req.headers
|
|
if (api_platform) {
|
|
data.platform = api_platform
|
|
}
|
|
let user = await Account.insertOrUpdate({ plat: channel, openId }, data)
|
|
const ztoken = await res.jwtSign({
|
|
id: user.id,
|
|
openid: user.openId,
|
|
version: user.accountVersion || 0,
|
|
plat: user.plat,
|
|
})
|
|
return { token: ztoken }
|
|
}
|
|
}
|