Skip to content

Commit

Permalink
feat: microsoft login
Browse files Browse the repository at this point in the history
  • Loading branch information
nailiable committed Jun 19, 2024
1 parent ff13b6c commit 0497263
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
"interceptors": "controller"
},
"prisma.fileWatcher": true,
"cSpell.words": ["naily"]
"cSpell.words": ["dtos", "naily", "Qrcode"]
}
2 changes: 1 addition & 1 deletion apps/passport/src/metadata.ts

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions apps/passport/src/modules/login/controllers/login.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PrismaService } from "@nailyjs.nest.modules/prisma";
import { QrCodeService } from "../providers/qrcode.service";
import { PostLoginQrcodeBodyDTO, PostLoginQrcodeConfirmBodyDTO } from "../dtos/qrcode/qrcode.dto";
import { PostLoginEmailCodeBodyDTO } from "../dtos/email/email.dto";
import { PostLoginMicrosoftBodyDTO } from "../dtos/microsoft/microsoft.dto";

@ApiTags("登录")
@Controller("login")
Expand Down Expand Up @@ -102,4 +103,23 @@ export class LoginController {
loginIP: ip,
});
}

/**
* 微软登录
*
* @memberof LoginController
*/
@Post("microsoft")
@UseInterceptors(ResInterceptor)
public async loginByMicrosoft(@Body() body: PostLoginMicrosoftBodyDTO, @Ip() ip: string) {
const user = await this.prismaService.user.findFirst({ where: { microsoftID: body.microsoftID } });
return this.loginService.loginByMicrosoft(user, {
identifier: body.identifier,
loginClient: body.loginClient,
loginType: body.loginType,
loginMethod: "Microsoft",
loginDeviceName: body.loginDeviceName,
loginIP: ip,
});
}
}
68 changes: 68 additions & 0 deletions apps/passport/src/modules/login/dtos/microsoft/microsoft.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ApiProperty } from "@nestjs/swagger";
import { ILoginType } from "@prisma/client";
import { LoginType } from "cc.naily.six.auth";
import { IsIn, IsNotEmpty, IsOptional, IsString } from "class-validator";

export class PostLoginMicrosoftBodyDTO {
/**
* 微软登录的ID
*
* @type {string}
* @memberof PostLoginMicrosoftBodyDTO
*/
@IsString()
@IsNotEmpty()
readonly microsoftID: string;
/**
* 登录设备类型 `请看schema的enum正确传值`
*
* @author Zero <gczgroup@qq.com>
* @date 2024/01/24
* @type {LoginType}
* @memberof PostLoginEmailCodeBodyDTO
*/
@IsIn(LoginType)
@IsString()
@IsNotEmpty()
@ApiProperty({ enum: LoginType })
readonly loginType: ILoginType;
/**
* 登录的客户端 用于记录哪个APP/哪种浏览器(比如谷歌、火狐浏览器)等
*
* Web端登录时,该字段可以为空
*
* @author Zero <gczgroup@qq.com>
* @date 2024/01/24
* @type {string}
* @memberof PostLoginEmailCodeBodyDTO
*/
@IsOptional()
@IsString()
readonly loginClient?: string;
/**
* 登录的设备名
*
* Web端登录时,该字段可以为空
*
* @author Zero <gczgroup@qq.com>
* @date 2024/01/28
* @type {string}
* @memberof PostLoginEmailCodeBodyDTO
*/
@IsOptional()
@IsString()
readonly loginDeviceName?: string;
/**
* 设备唯一标识符
*
* Web端登录时,该字段可以为空
*
* @author Zero <gczgroup@qq.com>
* @date 2024/01/24
* @type {string}
* @memberof PostLoginEmailCodeBodyDTO
*/
@IsOptional()
@IsString()
identifier?: string;
}
13 changes: 13 additions & 0 deletions apps/passport/src/modules/login/providers/login.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,17 @@ export class LoginService {
access_token,
};
}

public async loginByMicrosoft(user: User, loginPayload: ILoginPayload) {
user = await this.updateIp(loginPayload, user);
const access_token = this.getJwtToken(user, loginPayload);
const identifier = await this.identifierService.renewIdentifier(user, loginPayload);
if (identifier === "ERROR") throw new BadRequestException(1039);
user.password = undefined;
return {
user,
identifier,
access_token,
};
}
}
4 changes: 2 additions & 2 deletions apps/passport/src/modules/microsoft/microsoft.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class MicrosoftController {
@UseInterceptors(ResInterceptor)
@ApiCreatedResponse({ description: "返回的信息data字段是更新后的user大对象哦" })
public async bindMicrosoft(@User() user: JwtLoginPayload, @Body() { info }: PostAndPutMicrosoftBodyDTO) {
if (await this.prismaService.user.findFirst({ where: { microsoftID: info } })) throw new BadRequestException("该微软账号已绑定");
if (await this.prismaService.user.findFirst({ where: { microsoftID: info } })) throw new BadRequestException(1098);
const userInstance = await this.prismaService.user.update({
where: { userID: user.userID },
data: { microsoftID: info },
Expand All @@ -41,7 +41,7 @@ export class MicrosoftController {
@UseInterceptors(ResInterceptor)
@ApiCreatedResponse({ description: "返回的信息data字段是更新后的user大对象哦" })
public async updateMicrosoft(@User() user: JwtLoginPayload, @Body() { info }: PostAndPutMicrosoftBodyDTO) {
if (await this.prismaService.user.findFirst({ where: { microsoftID: info } })) throw new BadRequestException("该微软账号已绑定");
if (await this.prismaService.user.findFirst({ where: { microsoftID: info } })) throw new BadRequestException(1098);
const userInstance = await this.prismaService.user.update({
where: { userID: user.userID },
data: { microsoftID: info },
Expand Down
1 change: 1 addition & 0 deletions libraries/generated/src/i18n.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export type I18nTranslations = {
"1095": string;
"1096": string;
"1097": string;
"1098": string;
"40001005": string;
"---": string;
"----": string;
Expand Down
3 changes: 2 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//🧩Don't Edit this file.✨Generated in Sun Jun 16 2024 15:40:50 GMT+0800 (中国标准时间)✨
//🧩Don't Edit this file.✨Generated in Wed Jun 19 2024 16:18:02 GMT+0800 (中国标准时间)✨

generator client {
provider = "prisma-client-js"
Expand Down Expand Up @@ -447,6 +447,7 @@ enum ILoginMethod {
PhoneCode
EmailCode
UsernamePassword
Microsoft
}

enum IReceiptStatus {
Expand Down
1 change: 1 addition & 0 deletions prisma/subschemas/types/user.type.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum ILoginMethod {
PhoneCode
EmailCode
UsernamePassword
Microsoft
}

enum IReceiptStatus {
Expand Down
33 changes: 32 additions & 1 deletion resources/docs/passport.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@
"tags": ["登录"]
}
},
"/login/microsoft": {
"post": {
"operationId": "LoginController_loginByMicrosoft",
"summary": "微软登录",
"parameters": [],
"requestBody": {
"required": true,
"content": { "application/json": { "schema": { "$ref": "#/components/schemas/PostLoginMicrosoftBodyDTO" } } }
},
"responses": { "201": { "description": "" } },
"tags": ["登录"]
}
},
"/logout": {
"post": {
"operationId": "LogoutController_logout",
Expand Down Expand Up @@ -466,6 +479,24 @@
},
"required": ["loginType", "key"]
},
"PostLoginMicrosoftBodyDTO": {
"type": "object",
"properties": {
"loginType": {
"type": "string",
"description": "登录设备类型 `请看schema的enum正确传值`",
"enum": ["WatchOS", "HarmonyOS_Wearable", "Android", "Web", "IOS", "Panel"]
},
"microsoftID": { "type": "string", "description": "微软登录的ID" },
"loginClient": {
"type": "string",
"description": "登录的客户端 用于记录哪个APP/哪种浏览器(比如谷歌、火狐浏览器)等\n\nWeb端登录时,该字段可以为空"
},
"loginDeviceName": { "type": "string", "description": "登录的设备名\n\nWeb端登录时,该字段可以为空" },
"identifier": { "type": "string", "description": "设备唯一标识符\n\nWeb端登录时,该字段可以为空" }
},
"required": ["loginType", "microsoftID"]
},
"PostRegisterPhoneCodeBodyDTO": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -594,7 +625,7 @@
"description": "消息 除非请求过程中网络请求失败才会抛出其他错误。其余的情况,请根据`remoteData`的内容参照对应的判断",
"default": "成功"
},
"timestamp": { "type": "object", "description": "时间戳", "default": "2024-06-16T13:07:12.404Z" },
"timestamp": { "type": "object", "description": "时间戳", "default": "2024-06-19T08:23:55.233Z" },
"data": { "description": "数据", "allOf": [{ "$ref": "#/components/schemas/PostUserPayData201ResDTO" }] }
},
"required": ["statusCode", "code", "message", "timestamp", "data"]
Expand Down
1 change: 1 addition & 0 deletions resources/i18n/zh/global.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"1095": "该p8key下的p8文件内容为空或不存在 请联系管理员",
"1096": "个性签名不能为空",
"1097": "邮箱已经被注册",
"1098": "该微软账号已绑定",

"----": "Prisma错误",
"P2020": "未找到该商品",
Expand Down

0 comments on commit 0497263

Please sign in to comment.