* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* fix

* Update SignupApiService.ts

* wip

* wip

* Update ClientServerService.ts

* wip

* wip

* wip

* Update WellKnownServerService.ts

* wip

* wip

* update des

* wip

* Update ApiServerService.ts

* wip

* update deps

* Update WellKnownServerService.ts

* wip

* update deps

* Update ApiCallService.ts

* Update ApiCallService.ts

* Update ApiServerService.ts
This commit is contained in:
syuilo
2022-12-03 19:42:05 +09:00
committed by GitHub
parent 2db9f6efe7
commit 3a7182bfb5
40 changed files with 1651 additions and 1977 deletions

View File

@@ -1,13 +1,12 @@
import { Inject, Injectable } from '@nestjs/common';
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { DI } from '@/di-symbols.js';
import type { SigninsRepository } from '@/models/index.js';
import type { UsersRepository } from '@/models/index.js';
import type { SigninsRepository, UsersRepository } from '@/models/index.js';
import type { Config } from '@/config.js';
import { IdService } from '@/core/IdService.js';
import type { ILocalUser } from '@/models/entities/User.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { SigninEntityService } from '@/core/entities/SigninEntityService.js';
import type Koa from 'koa';
@Injectable()
export class SigninService {
@@ -24,10 +23,25 @@ export class SigninService {
) {
}
public signin(ctx: Koa.Context, user: ILocalUser, redirect = false) {
public signin(request: FastifyRequest, reply: FastifyReply, user: ILocalUser, redirect = false) {
setImmediate(async () => {
// Append signin history
const record = await this.signinsRepository.insert({
id: this.idService.genId(),
createdAt: new Date(),
userId: user.id,
ip: request.ip,
headers: request.headers,
success: true,
}).then(x => this.signinsRepository.findOneByOrFail(x.identifiers[0]));
// Publish signin event
this.globalEventService.publishMainStream(user.id, 'signin', await this.signinEntityService.pack(record));
});
if (redirect) {
//#region Cookie
ctx.cookies.set('igi', user.token!, {
reply.cookies.set('igi', user.token!, {
path: '/',
// SEE: https://github.com/koajs/koa/issues/974
// When using a SSL proxy it should be configured to add the "X-Forwarded-Proto: https" header
@@ -36,29 +50,14 @@ export class SigninService {
});
//#endregion
ctx.redirect(this.config.url);
reply.redirect(this.config.url);
} else {
ctx.body = {
reply.code(200);
return {
id: user.id,
i: user.token,
};
ctx.status = 200;
}
(async () => {
// Append signin history
const record = await this.signinsRepository.insert({
id: this.idService.genId(),
createdAt: new Date(),
userId: user.id,
ip: ctx.ip,
headers: ctx.headers,
success: true,
}).then(x => this.signinsRepository.findOneByOrFail(x.identifiers[0]));
// Publish signin event
this.globalEventService.publishMainStream(user.id, 'signin', await this.signinEntityService.pack(record));
})();
}
}