mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 21:21:02 +00:00
feat(api): API tokens for authentication
Create and managed simple API tokens for access to the API from external tools.
This commit is contained in:
parent
eda89716ef
commit
8f7eebb806
15 changed files with 614 additions and 154 deletions
24
src/auth/api-token.repository.ts
Normal file
24
src/auth/api-token.repository.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* eslint-disable max-classes-per-file */
|
||||
import { Repository, SelectQueryBuilder } from "typeorm";
|
||||
import { EntityRepository } from "../database/entity-repository";
|
||||
import { User } from "../users/user.entity";
|
||||
import { ApiToken } from "./api-token.entity";
|
||||
|
||||
export class ApiTokenScopes extends SelectQueryBuilder<ApiToken> {
|
||||
/**
|
||||
* `byUser` scopes the query to ApiTokens created by the user.
|
||||
* @param currentUser
|
||||
*/
|
||||
byUser(currentUser: User): this {
|
||||
return this.andWhere(`token."userId" = :userID`, {
|
||||
userID: currentUser.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@EntityRepository(ApiToken)
|
||||
export class ApiTokenRepository extends Repository<ApiToken> {
|
||||
get scoped(): ApiTokenScopes {
|
||||
return new ApiTokenScopes(this.createQueryBuilder("token"));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue