Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database #6

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
96b2734
feat:user and role model implemented
yaredtsy Jan 21, 2023
12d1a16
refa
yaredtsy Jan 21, 2023
69fed21
feat: permission model added
yaredtsy Jan 28, 2023
4f30173
refa: database restructured
yaredtsy Mar 7, 2023
0edc0d6
feat: enum validation added
yaredtsy Mar 7, 2023
4961a0f
feat: user model added
yaredtsy Mar 7, 2023
206f262
feat: user management added
yaredtsy Mar 8, 2023
37a18dc
feat: Auth model added
yaredtsy Mar 11, 2023
e68ccf2
feat: local strategy added
yaredtsy Mar 11, 2023
4ef9d51
feat: secret_key added
yaredtsy Mar 11, 2023
5ff1db5
feat: auth added
yaredtsy Mar 11, 2023
5562c16
feat: basic finalized
yaredtsy Mar 13, 2023
bca48ca
feat: auth validation optimized
yaredtsy Mar 14, 2023
eac9495
refa: code cleaned
yaredtsy Mar 14, 2023
41fbaea
refa: response refactored
yaredtsy Mar 14, 2023
b3c0680
refa: code cleaned
yaredtsy Mar 14, 2023
56e1298
feat: role and permission
yaredtsy Mar 18, 2023
ec8e91a
feat: role permission fixed completed
yaredtsy Mar 20, 2023
b6c9d85
fix: resource should exists in db
yaredtsy Mar 24, 2023
f5c8083
refa: permission refactored
yaredtsy Mar 24, 2023
4b2a413
refa: role refactor
yaredtsy Mar 24, 2023
f242276
fix: login fix
yaredtsy Mar 24, 2023
fc24ad3
refa: user refactored
yaredtsy Mar 24, 2023
3bb5c35
feat: bin added
yaredtsy Mar 25, 2023
124b0d1
feat: seeder added
yaredtsy Mar 25, 2023
98fa02c
test: fix absolute paths
Apr 9, 2023
0b24915
config: config
Apr 9, 2023
37fb8ba
refa: code refa
Apr 9, 2023
48cf3f1
refa: permission refa
Apr 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Auth model added
yaredtsy committed Mar 11, 2023
commit 37a18dc130585d264116c9dc1f9687e7f4995633
53 changes: 53 additions & 0 deletions src/apps/Core/Auth/model/auth.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Schema, model, Types } from 'mongoose';
import { User } from '@apps/Core/User/model/user.model';

//
import { BaseModel } from '../../Base/model/Base';

export const DOCUMENT_NAME = 'JWTtoken';
export const COLLECTION_NAME = 'JWTtokens';

export interface JWTtoken extends BaseModel {
client: Omit<User, 'password'>;
accessKey: string;
ipAddress: string;
blacklisted?: boolean;
deviceName?: string;
}

const schema = new Schema<JWTtoken>(
{
ipAddress: {
type: Schema.Types.String,
required: true,
trim: true
},
client: {
type: Schema.Types.ObjectId,
required: true,
ref: 'User'
},
accessKey: {
type: Schema.Types.String,
required: true,
trim: true
},
blacklisted: {
type: Schema.Types.Boolean,
default: false
}
},
{
versionKey: false
}
);

schema.index({ client: 1 });
schema.index({ client: 1, primaryKey: 1, status: 1 });
schema.index({ client: 1, primaryKey: 1, secondaryKey: 1 });

export const JwtTokenModel = model<JWTtoken>(
DOCUMENT_NAME,
schema,
COLLECTION_NAME
);
55 changes: 55 additions & 0 deletions src/apps/Core/Auth/model/auth.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { BadRequestError, NoDataError } from '@core/ApiError';
import { Types } from 'mongoose';

import { JwtTokenModel, JWTtoken } from './auth.model';

export const createJwtToken = async (
jwtToken: Pick<
JWTtoken,
'accessKey' | 'client' | 'deviceName' | 'ipAddress'
>
): Promise<boolean> => {
await JwtTokenModel.create(jwtToken);
return true;
};

export const findByToken = async (token: string): Promise<JWTtoken> => {
const jwtToken = await JwtTokenModel.findOne({ accessKey: token });
if (jwtToken) return jwtToken;
throw new NoDataError(`No token found for ${token}`);
};

export const isTokenBlackListed = async (token: string): Promise<boolean> => {
const jwtToken = await findByToken(token);
return jwtToken.blacklisted!;
};

export const blackListToken = async (token: string): Promise<void> => {
const jwtToken = await JwtTokenModel.findOneAndUpdate(
{ accessKey: token },
{ $set: { blackList: true } }
);

if (!jwtToken) throw new NoDataError(`No token found for ${token}`);
};

export const blackListTokens = async (token: string[]) => {
await JwtTokenModel.updateMany({ accessKey: token }, { blackList: true });
};

export const getTokenWithUserId = async (
id: Types.ObjectId
): Promise<JWTtoken[]> => {
const jwtTokens = await JwtTokenModel.find({ client: id }).lean();
return jwtTokens;
};

export const getTokenWithIpAddress = async (
ip: string
): Promise<JWTtoken[]> => {
const jwtTokens = await JwtTokenModel.find({
ipAddress: ip,
blacklisted: false
}).lean();
return jwtTokens;
};