typescript - 如何使用 Loopback 04 JWT-Authentication 在自定义模型中生成登录和注销 API,除了用户模型
问题描述
客户.model.ts
import {Entity, model, property} from '@loopback/repository';
import {
Credentials,
MyUserService,
TokenServiceBindings,
User,
UserRepository,
UserServiceBindings,
} from '@loopback/authentication-jwt';
@model({settings: {strict: true}})
export class Customer extends Entity {
@property({
type: 'number',
id: true,
generated: true,
})
customerId?: number;
@property({
type: 'string',
required: true,
})
customer_name: string;
@property({
type: 'string',
required: true,
})
customer_email: string;
@property({
type: 'string',
required: true,
})
customer_nic: string;
@property({
type: 'string',
required: true,
})
customerContact: string;
@property({
type: 'string',
required: true,
})
customerAddress01: string;
@property({
type: 'string',
required: true,
})
customerAddress02: string;
@property({
type: 'string',
required: true,
})
state: string;
@property({
type: 'string',
required: true,
})
zipCode: string;
@property({
type: 'string',
required: true,
})
country: string;
@property({
type: 'string',
required: true,
})
password: string;
// Define well-known properties here
// Indexer property to allow additional data
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[prop: string]: any;
constructor(data?: Partial<Customer>) {
super(data);
}
}
export interface CustomerRelations {
// describe navigational properties here
}
export type CustomerWithRelations = Customer & CustomerRelations;
==================================================== ==================================================== =====
客户.repository.ts
import {inject} from '@loopback/core';
import {DefaultCrudRepository} from '@loopback/repository';
import {DbDataSource} from '../datasources';
import {Customer, CustomerRelations} from '../models';
export class CustomerRepository extends DefaultCrudRepository<
Customer,
typeof Customer.prototype.customerId,
CustomerRelations
> {
constructor(
@inject('datasources.db') dataSource: DbDataSource,
) {
super(Customer, dataSource);
}
}
//================================================= ==================================================== ====
应用程序.ts
import {BootMixin} from '@loopback/boot';
import {ApplicationConfig} from '@loopback/core';
import {
RestExplorerBindings,
RestExplorerComponent,
} from '@loopback/rest-explorer';
import {RepositoryMixin} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {ServiceMixin} from '@loopback/service-proxy';
import path from 'path';
import {MySequence} from './sequence';
import {CrudRestComponent} from '@loopback/rest-crud';
import {AuthenticationComponent} from '@loopback/authentication';
import {
JWTAuthenticationComponent,
SECURITY_SCHEME_SPEC,
UserServiceBindings,
} from '@loopback/authentication-jwt';
import {DbDataSource} from './datasources';
export {ApplicationConfig};
export class SmartyLinkBackendApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
constructor(options: ApplicationConfig = {}) {
super(options);
this.component(AuthenticationComponent);
// Mount jwt component
this.component(JWTAuthenticationComponent);
// Bind datasource
this.dataSource(DbDataSource, UserServiceBindings.DATASOURCE_NAME);
// Set up the custom sequence
this.sequence(MySequence);
// Set up default home page
this.static('/', path.join(__dirname, '../public'));
// Customize @loopback/rest-explorer configuration here
this.configure(RestExplorerBindings.COMPONENT).to({
path: '/explorer',
});
this.component(RestExplorerComponent);
this.projectRoot = __dirname;
// Customize @loopback/boot Booter Conventions here
this.bootOptions = {
controllers: {
// Customize ControllerBooter Conventions here
dirs: ['controllers'],
extensions: ['.controller.js'],
nested: true,
},
};
this.component(CrudRestComponent);
}
}
我研究了文档中有关 JWT 身份验证的示例代码。只是我想为我的客户模型添加登录、注销 API。我正在寻求你的帮助。谢谢你!
解决方案
推荐阅读
- python - 在熊猫数据框中将 YYYYMXX 转换为日期
- wpf - IIS 托管 WCF 服务,对 WPF 客户端使用仅 HTTPS 和 Windows 身份验证
- flutter - Flutter 检查 DateTime 是否确实存在
- android - 适用于 Android 11+ 的可移动 micro-sd 卡上的文件的 ContentProvider
- c# - Html.BeginForm (ASP.NET MVC) 的问题
- asp.net - 在 ItemUpdated 事件中访问 FormView
- postgresql - Postgresql 在函数中返回 NULL 而不是正确的结果
- rancher - 通过 Rancher 设置 k8 集群中的错误消息不明确
- linux - 如何为 Flutter linux 桌面启用屏幕键盘
- sql - 如何从特定日期获取结果 (2016-01-01 / 2016-12-31)