mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-22 23:45:02 +00:00
fixed associations, user can auth via api-key
This commit is contained in:
parent
a884268d97
commit
a928e7a917
@ -1,6 +1,6 @@
|
|||||||
import { Error as SequelizeError } from "sequelize";
|
import { Error as SequelizeError } from "sequelize";
|
||||||
import { APIKey } from "./apikey-model";
|
|
||||||
import { User } from "../user/user-model";
|
import { User } from "../user/user-model";
|
||||||
|
import { APIKey } from "./apikey-model";
|
||||||
|
|
||||||
export function findOne(params: {apikey?: string}, cb: (err: Error | null, apikey?: APIKey | undefined, info?: Object | undefined) => void): undefined {
|
export function findOne(params: {apikey?: string}, cb: (err: Error | null, apikey?: APIKey | undefined, info?: Object | undefined) => void): undefined {
|
||||||
const query: any = params;
|
const query: any = params;
|
||||||
@ -16,7 +16,8 @@ export function findOne(params: {apikey?: string}, cb: (err: Error | null, apike
|
|||||||
}
|
}
|
||||||
|
|
||||||
APIKey.findOne({
|
APIKey.findOne({
|
||||||
where: query
|
where: query,
|
||||||
|
include: APIKey.associations.User
|
||||||
}).then(apikey => {
|
}).then(apikey => {
|
||||||
if(apikey)
|
if(apikey)
|
||||||
cb(null, apikey);
|
cb(null, apikey);
|
||||||
@ -27,9 +28,11 @@ export function findOne(params: {apikey?: string}, cb: (err: Error | null, apike
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createAPIKey(user: User | undefined): Promise<APIKey> {
|
export async function createAPIKey(user: User | undefined): Promise<APIKey | undefined> {
|
||||||
|
if(!user) throw new Error("User was undefined");
|
||||||
|
|
||||||
const apikey = crypto.randomUUID(); // TODO: Is this secure enough?
|
const apikey = crypto.randomUUID(); // TODO: Is this secure enough?
|
||||||
const apikeyEntry = await APIKey.create({ apikey: apikey })
|
const apikeyEntry = await user.createAPIKey({ apikey: apikey });
|
||||||
await user?.addAPIKey(apikeyEntry);
|
|
||||||
return apikeyEntry;
|
return apikeyEntry;
|
||||||
}
|
}
|
@ -7,8 +7,10 @@ export class APIKey extends Model<InferAttributes<APIKey>, InferCreationAttribut
|
|||||||
declare id: CreationOptional<number>;
|
declare id: CreationOptional<number>;
|
||||||
declare apikey: string;
|
declare apikey: string;
|
||||||
|
|
||||||
declare ownerId: ForeignKey<User['id']>;
|
declare UserId: ForeignKey<User['id']>;
|
||||||
declare owner?: NonAttribute<User>;
|
|
||||||
|
// `User` is an eagerly-loaded association.
|
||||||
|
declare User?: NonAttribute<User>;
|
||||||
|
|
||||||
declare createdAt: CreationOptional<Date>;
|
declare createdAt: CreationOptional<Date>;
|
||||||
declare updatedAt: CreationOptional<Date>;
|
declare updatedAt: CreationOptional<Date>;
|
||||||
|
@ -37,7 +37,7 @@ export function initialize(passport: typeof import("passport")) {
|
|||||||
if (!apikey) {
|
if (!apikey) {
|
||||||
return done(null, false, info);
|
return done(null, false, info);
|
||||||
}
|
}
|
||||||
return done(null, apikey.owner);
|
return done(null, apikey.User);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
@ -30,7 +30,7 @@ export class User extends Model<InferAttributes<User>, InferCreationAttributes<U
|
|||||||
declare hasAccessRule: HasManyHasAssociationMixin<AccessRule | undefined, number>;
|
declare hasAccessRule: HasManyHasAssociationMixin<AccessRule | undefined, number>;
|
||||||
declare hasAccessRules: HasManyHasAssociationsMixin<AccessRule | undefined, number>;
|
declare hasAccessRules: HasManyHasAssociationsMixin<AccessRule | undefined, number>;
|
||||||
declare countAccessRules: HasManyCountAssociationsMixin;
|
declare countAccessRules: HasManyCountAssociationsMixin;
|
||||||
declare createAccessRule: HasManyCreateAssociationMixin<AccessRule, 'ownerId'>;
|
declare createAccessRule: HasManyCreateAssociationMixin<AccessRule, 'UserId'>;
|
||||||
|
|
||||||
declare getAPIKeys: HasManyGetAssociationsMixin<APIKey | undefined>; // Note the null assertions!
|
declare getAPIKeys: HasManyGetAssociationsMixin<APIKey | undefined>; // Note the null assertions!
|
||||||
declare addAPIKey: HasManyAddAssociationMixin<APIKey | undefined, number>;
|
declare addAPIKey: HasManyAddAssociationMixin<APIKey | undefined, number>;
|
||||||
@ -41,7 +41,11 @@ export class User extends Model<InferAttributes<User>, InferCreationAttributes<U
|
|||||||
declare hasAPIKey: HasManyHasAssociationMixin<APIKey | undefined, number>;
|
declare hasAPIKey: HasManyHasAssociationMixin<APIKey | undefined, number>;
|
||||||
declare hasAPIKeys: HasManyHasAssociationsMixin<APIKey | undefined, number>;
|
declare hasAPIKeys: HasManyHasAssociationsMixin<APIKey | undefined, number>;
|
||||||
declare countAPIKeys: HasManyCountAssociationsMixin;
|
declare countAPIKeys: HasManyCountAssociationsMixin;
|
||||||
declare createAPIKey: HasManyCreateAssociationMixin<APIKey, 'ownerId'>;
|
declare createAPIKey: HasManyCreateAssociationMixin<APIKey, 'UserId'>;
|
||||||
|
|
||||||
|
// You can also pre-declare possible inclusions, these will only be populated if you
|
||||||
|
// actively include a relation.
|
||||||
|
declare apikeys?: NonAttribute<APIKey[]>;
|
||||||
|
|
||||||
declare createdAt: CreationOptional<Date>;
|
declare createdAt: CreationOptional<Date>;
|
||||||
declare updatedAt: CreationOptional<Date>;
|
declare updatedAt: CreationOptional<Date>;
|
||||||
@ -63,8 +67,8 @@ export class AccessRule extends Model<InferAttributes<AccessRule>, InferCreation
|
|||||||
declare id: CreationOptional<number>;
|
declare id: CreationOptional<number>;
|
||||||
declare grants: string;
|
declare grants: string;
|
||||||
|
|
||||||
declare ownerId: ForeignKey<User['id']>;
|
declare UserId: ForeignKey<User['id']>;
|
||||||
declare owner?: NonAttribute<User>;
|
declare User?: NonAttribute<User>;
|
||||||
|
|
||||||
declare createdAt: CreationOptional<Date>;
|
declare createdAt: CreationOptional<Date>;
|
||||||
declare updatedAt: CreationOptional<Date>;
|
declare updatedAt: CreationOptional<Date>;
|
||||||
|
@ -44,8 +44,8 @@ Password.init(
|
|||||||
},
|
},
|
||||||
{ sequelize },
|
{ sequelize },
|
||||||
);
|
);
|
||||||
Password.hasOne(User);
|
|
||||||
User.hasOne(Password);
|
User.hasOne(Password);
|
||||||
|
Password.belongsTo(User);
|
||||||
|
|
||||||
AccessRule.init(
|
AccessRule.init(
|
||||||
{
|
{
|
||||||
@ -60,8 +60,8 @@ AccessRule.init(
|
|||||||
},
|
},
|
||||||
{ sequelize },
|
{ sequelize },
|
||||||
);
|
);
|
||||||
AccessRule.hasOne(User);
|
|
||||||
User.hasMany(AccessRule);
|
User.hasMany(AccessRule);
|
||||||
|
AccessRule.belongsTo(User);
|
||||||
|
|
||||||
APIKey.init(
|
APIKey.init(
|
||||||
{
|
{
|
||||||
@ -76,8 +76,8 @@ APIKey.init(
|
|||||||
},
|
},
|
||||||
{ sequelize },
|
{ sequelize },
|
||||||
);
|
);
|
||||||
APIKey.hasOne(User);
|
|
||||||
User.hasMany(APIKey);
|
User.hasMany(APIKey);
|
||||||
|
APIKey.belongsTo(User);
|
||||||
|
|
||||||
export default sequelize;
|
export default sequelize;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user