1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-13 11:17:26 +02:00
unleash.unleash/src/lib/routes/auth/simple-password-provider.ts
Thomas Heartman 9448461aaa
docs: prep to add OpenAPI spec to Unleash docs (#1907)
* Docs: start experimenting with OpenAPI and docusaurus

* Docs: add docusaurus-theme-openapi-docs pkg

* Wip: current status

* Docs: Add 'docusaurus-plugin-api-docs'

* Move openapi into own sidebar; generate from localhost

* Chore: Update docusaurus plugin for OpenAPI

* Add website/yarn.lock to git

* Fix: fix CSS warning by using flex-end instead of end

* docs: make openapi generated code work again

* docs: make tags work properly with openapi sidebar

* Docs/chore: update OpenAPI tag scheme.

Add a whole bunch of new tags to make it easier to understand
available tags in OpenAPI.

* docs: point to new openapi docs from old api docs

* docs: typo

* Docs:  link restructure

* docs: add operation indicators to openapi docs

* docs: change badge color for operations

* docs: update openapi-docs package

It now sorts tags the same as the schema

* docs: pluralize APIs in slug

* docs: update links to generated api docs

* docs: update openapi snapshot tests with new tags

* docs: conditionally load spec from localhost or from file

* docs: Remove changes relating to immediate switchover

* refactor: rename types; extract into separate file

* docs: fix api doc links
2022-08-12 11:37:57 +02:00

69 lines
2.3 KiB
TypeScript

import { Response } from 'express';
import { OpenApiService } from '../../services/openapi-service';
import { Logger } from '../../logger';
import { IUnleashConfig } from '../../server-impl';
import UserService from '../../services/user-service';
import { IUnleashServices } from '../../types';
import { NONE } from '../../types/permissions';
import Controller from '../controller';
import { IAuthRequest } from '../unleash-types';
import { createRequestSchema } from '../../openapi/util/create-request-schema';
import { createResponseSchema } from '../../openapi/util/create-response-schema';
import { userSchema, UserSchema } from '../../openapi/spec/user-schema';
import { LoginSchema } from '../../openapi/spec/login-schema';
import { serializeDates } from '../../types/serialize-dates';
export class SimplePasswordProvider extends Controller {
private logger: Logger;
private openApiService: OpenApiService;
private userService: UserService;
constructor(
config: IUnleashConfig,
{
userService,
openApiService,
}: Pick<IUnleashServices, 'userService' | 'openApiService'>,
) {
super(config);
this.logger = config.getLogger('/auth/password-provider.js');
this.openApiService = openApiService;
this.userService = userService;
this.route({
method: 'post',
path: '/login',
handler: this.login,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Auth'],
operationId: 'login',
requestBody: createRequestSchema('loginSchema'),
responses: {
200: createResponseSchema('userSchema'),
},
}),
],
});
}
async login(
req: IAuthRequest<void, void, LoginSchema>,
res: Response<UserSchema>,
): Promise<void> {
const { username, password } = req.body;
const user = await this.userService.loginUser(username, password);
req.session.user = user;
this.openApiService.respondWithValidation(
200,
res,
userSchema.$id,
serializeDates(user),
);
}
}