import { FastifySchema , Response , ResponseError } from "../../../../types/Server" ; import { AbstractController } from "../../../../abstract/controller" ; import { Controller } from "../../../../decorator/Controller" ; import jwt from "jsonwebtoken" ; import { AppleJWTToken , LoginApple } from "../platforms/LoginApple" ; import { ServiceUserApple } from "../../../service/user/UserApple" ; import { v4 } from "uuid" ; import { LoginPlatform , Status } from "../../../../constants/Project" ; @ Controller < RequestType , ResponseType > ( { method : "post" , path : "login/apple/jwt" , auth : false , } ) export class AppleJWT extends AbstractController < RequestType , ResponseType > { public static readonly schema : FastifySchema < RequestType > = { body : { type : "object" , required : [ "jwtToken" ] , properties : { jwtToken : { type : "string" , } , nickname : { type : "string" , nullable : true , } , } , } , } ; public async execute ( ) { const { jwtToken , nickname } = this . body ; await LoginApple . assertJWTTokenCorrect ( jwtToken ) ; const token = jwt . decode ( jwtToken , { complete : true , } ) as AppleJWTToken ; const appleID = token . payload . sub ; const userUUIDByDB = await ServiceUserApple . userUUIDByUnionUUID ( appleID ) ; const userUUID = userUUIDByDB || v4 ( ) ; const loginApple = new LoginApple ( { userUUID , } ) ; if ( ! userUUIDByDB ) { await loginApple . register ( { unionUUID : appleID , avatarURL : "https://flat-storage.oss-cn-hangzhou.aliyuncs.com/flat-resources/avatar/default-00.png" , userName : nickname || "Apple User" , } ) ; } const { userName , avatarURL } = ( await loginApple . svc . user . nameAndAvatar ( ) ) ! ; return { status : Status . Success , data : { name : userName , avatar : avatarURL , userUUID , token : await this . reply . jwtSign ( { userUUID , loginSource : LoginPlatform . Apple , } ) , } , } ; } public errorHandler ( error ) { return this . autoHandlerError ( error ) ; } } interface RequestType { body : { jwtToken : string ; nickname ? : string ; } ; } interface ResponseType { name : string ; avatar : string ; userUUID : string ; token : string ; }