summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/SigninService.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-12-03 19:42:05 +0900
committerGitHub <noreply@github.com>2022-12-03 19:42:05 +0900
commit3a7182bfb5734599321fc03ea77c48b4dbc326d5 (patch)
treec96c46e0a9662809c40381d833e1ed1ca28de873 /packages/backend/src/server/api/SigninService.ts
parentUpdate CHANGELOG.md (diff)
downloadsharkey-3a7182bfb5734599321fc03ea77c48b4dbc326d5.tar.gz
sharkey-3a7182bfb5734599321fc03ea77c48b4dbc326d5.tar.bz2
sharkey-3a7182bfb5734599321fc03ea77c48b4dbc326d5.zip
Fastify (#9106)
* 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
Diffstat (limited to 'packages/backend/src/server/api/SigninService.ts')
-rw-r--r--packages/backend/src/server/api/SigninService.ts45
1 files changed, 22 insertions, 23 deletions
diff --git a/packages/backend/src/server/api/SigninService.ts b/packages/backend/src/server/api/SigninService.ts
index 3b96dfee6f..18a1d6c088 100644
--- a/packages/backend/src/server/api/SigninService.ts
+++ b/packages/backend/src/server/api/SigninService.ts
@@ -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));
- })();
}
}