Skip to content

Commit

Permalink
wip: login process
Browse files Browse the repository at this point in the history
  • Loading branch information
donghquinn committed Feb 6, 2024
1 parent 559d6f2 commit 49202ae
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 79 deletions.
20 changes: 16 additions & 4 deletions src/providers/auth/account-manager.pvd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class AccountManager {

if (!isLogined) {
const clientKey = { uuid: clientUuid };

this.userKeys.push(clientKey);
this.setItem(email, clientKey, password);

Expand All @@ -32,10 +33,13 @@ export class AccountManager {
this.userKeys.shift();
}

return clientKey;
ClientLogger.info('[ACCOUNT] Set Finished');

return clientUuid;
}

ManagerLogger.debug('[ACCOUNT] Client Map Inspection: %o', {
ManagerLogger.debug('[ACCOUNT] Found User Key: %o', {
clientUuid,
map: this.userMap,
});

Expand All @@ -61,11 +65,12 @@ export class AccountManager {
clientUuid,
map: this.userMap,
});

this.deleteItem(clientUuid);
}
}, interval);

return isLogined;
return clientUuid;
}

public deleteLogoutUser(clientUuid: string) {
Expand Down Expand Up @@ -96,6 +101,13 @@ export class AccountManager {
const index = this.userKeys.findIndex((item) => item.uuid === clientUuid);

if (index > -1) this.userKeys.splice(index, 1);

ClientLogger.debug('[ACCOUNT] Delete Finished: %o', {
clientUuid,
index,
keyList: this.userKeys,
map: this.userMap,
});
}

public setItem(email: string, clientKey: ClientLoginMapKey, password?: string) {
Expand All @@ -107,7 +119,7 @@ export class AccountManager {

if (!key) return null;

return this.userMap.get(key);
return this.userMap.get(key) as ClientLoginMapItem;
}

// public stop() {
Expand Down
128 changes: 64 additions & 64 deletions src/providers/auth/jwt.pvd.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
import { AuthError } from '@errors/auth.error';
import { Injectable, Logger, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Response } from 'express';
import { AccountManager } from './account-manager.pvd';
// import { AuthError } from '@errors/auth.error';
// import { Injectable, Logger, UnauthorizedException } from '@nestjs/common';
// import { JwtService } from '@nestjs/jwt';
// import { Response } from 'express';
// import { AccountManager } from './account-manager.pvd';

@Injectable()
export class JwtProvider {
constructor(
private readonly jwt: JwtService,
private readonly account: AccountManager,
) {}
// @Injectable()
// export class JwtProvider {
// constructor(
// private readonly jwt: JwtService,
// private readonly account: AccountManager,
// ) {}

async getAccessToken(email: string, userUuid: string): Promise<string> {
try {
return await this.jwt.signAsync(
{ email, userUuid },
{ secret: process.env.JWT_ACCESS_TOKEN_SECRET, expiresIn: '5m' },
);
} catch (error) {
Logger.error('[JWT] Issue Access Token Error: %o', {
error: error instanceof Error ? error : new Error(JSON.stringify(error)),
});
// async getAccessToken(email: string, userUuid: string): Promise<string> {
// try {
// return await this.jwt.signAsync(
// { email, userUuid },
// { secret: process.env.JWT_ACCESS_TOKEN_SECRET, expiresIn: '5m' },
// );
// } catch (error) {
// Logger.error('[JWT] Issue Access Token Error: %o', {
// error: error instanceof Error ? error : new Error(JSON.stringify(error)),
// });

throw new AuthError(
'[JWT] Issue Access Token',
'Issue Access Token Error. Please Try Again.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}
// throw new AuthError(
// '[JWT] Issue Access Token',
// 'Issue Access Token Error. Please Try Again.',
// error instanceof Error ? error : new Error(JSON.stringify(error)),
// );
// }
// }

async signIn(email: string, pass: string): Promise<{ access_token: string }> {
const userInfo = this.account.getItem(email);
// async signIn(email: string, pass: string): Promise<{ access_token: string }> {
// const userInfo = this.account.getItem(email);

if (userInfo === undefined) throw new UnauthorizedException();
// if (!userInfo) throw new UnauthorizedException();

if (userInfo.password !== pass) {
throw new UnauthorizedException();
}
const payload = { uuid: userInfo.uuid, email };
return {
access_token: await this.jwt.signAsync(payload),
};
}
// if (userInfo.password !== pass) {
// throw new UnauthorizedException();
// }
// const payload = { uuid: userInfo.uuid, email };
// return {
// access_token: await this.jwt.signAsync(payload),
// };
// }

async setRefreshToken(email: string, userUuid: string, response: Response) {
try {
const refreshToken = await this.jwt.signAsync(
{
email,
userUuid,
},
{
secret: process.env.JWT_REFRESH_TOKEN_SECRET,
expiresIn: '2w',
},
);
// async setRefreshToken(email: string, userUuid: string, response: Response) {
// try {
// const refreshToken = await this.jwt.signAsync(
// {
// email,
// userUuid,
// },
// {
// secret: process.env.JWT_REFRESH_TOKEN_SECRET,
// expiresIn: '2w',
// },
// );

response.setHeader('Set-Cookie', `refreshToken=${refreshToken}`);
} catch (error) {
Logger.error('[JWT] Issue Refresh Token Error: %o', {
error: error instanceof Error ? error : new Error(JSON.stringify(error)),
});
// response.setHeader('Set-Cookie', `refreshToken=${refreshToken}`);
// } catch (error) {
// Logger.error('[JWT] Issue Refresh Token Error: %o', {
// error: error instanceof Error ? error : new Error(JSON.stringify(error)),
// });

throw new AuthError(
'[JWT] Issue Refresh Token',
'Issue Refresh Token Error. Please Try Again.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}
}
// throw new AuthError(
// '[JWT] Issue Refresh Token',
// 'Issue Refresh Token Error. Please Try Again.',
// error instanceof Error ? error : new Error(JSON.stringify(error)),
// );
// }
// }
// }
15 changes: 4 additions & 11 deletions src/providers/client/client.pvd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { comparePassword } from '@libraries/client/decrypt.lib';
import { cryptPassword } from '@libraries/client/encrypt.lib';
import { Injectable } from '@nestjs/common';
import { ClientLogger } from '@utils/logger.util';
import { ClientLoginMapKey } from 'types/client.type';
import { AccountManager } from '../auth/account-manager.pvd';
import { ClientPrismaLibrary } from './client-prisma.pvd';

Expand Down Expand Up @@ -66,9 +65,7 @@ export class ClientProvider {
throw new ClientError('[LOGIN] Password Matching ', ' Password Matching is Not Match. Reject.');
}

const itemKey: ClientLoginMapKey = { uuid };

this.accountManager.setLoginUser(itemKey, email, foundPassword);
this.accountManager.setLoginUser(uuid, email, foundPassword);

await this.prisma.updateClientLoginStatus(uuid, 1);

Expand All @@ -88,9 +85,7 @@ export class ClientProvider {

async logout(clientUuid: string) {
try {
const itemKey: ClientLoginMapKey = { uuid: clientUuid };

const foundKey = this.accountManager.searchItem(itemKey);
const foundKey = this.accountManager.getItem(clientUuid);

if (foundKey === null) {
ClientLogger.debug('[LOGIN] No Matching User Found: %o', {
Expand All @@ -102,7 +97,7 @@ export class ClientProvider {

await this.prisma.updateClientLoginStatus(clientUuid, 0);

const deleteItem = this.accountManager.deleteLogoutUser(itemKey);
const deleteItem = this.accountManager.deleteLogoutUser(clientUuid);

if (!deleteItem) throw new ClientError('[LOGOUT] Logout', 'No Data Found. Ignore.');

Expand All @@ -122,9 +117,7 @@ export class ClientProvider {

async myPage(clientUuid: string, page: number) {
try {
const itemKey: ClientLoginMapKey = { uuid: clientUuid };

const foundKey = this.accountManager.searchItem(itemKey);
const foundKey = this.accountManager.getItem(clientUuid);

if (foundKey === null) {
ClientLogger.debug('[MYPAGE] No Matching User Found: %o', {
Expand Down

0 comments on commit 49202ae

Please sign in to comment.