Skip to content
This repository was archived by the owner on Mar 13, 2024. It is now read-only.

Commit 7e7f8b6

Browse files
committed
sign in moved from auth to employee
1 parent 670db62 commit 7e7f8b6

18 files changed

+214
-177
lines changed

sdk/core.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { AvatarParams } from './types/objects';
2-
import { SignInByEmailRequest, SignInByEmailResponse } from './endpoints';
32
import { NextFetchRequestConfig } from './types/next';
43
import { JWTEmployeeAccessTokenPayload } from './types/jwt';
54
import { ErrorBase } from './errors';
65
import {
76
ChannelEntity,
87
CheckoutEntity,
98
ClientEntity,
9+
EmployeeEntity,
1010
MediaEntity,
1111
MenuCategoryEntity,
1212
MenuEntity,
@@ -29,6 +29,7 @@ export class MainAPI {
2929
public readonly checkout: CheckoutEntity;
3030
public readonly product: ProductEntity;
3131
public readonly productVariant: ProductVariantEntity;
32+
public readonly employee: EmployeeEntity;
3233

3334
constructor(apiUrl: string, apiToken: string) {
3435
this.apiUrl = apiUrl;
@@ -43,6 +44,7 @@ export class MainAPI {
4344
this.checkout = new CheckoutEntity(apiUrl, apiToken);
4445
this.product = new ProductEntity(apiUrl, apiToken);
4546
this.productVariant = new ProductVariantEntity(apiUrl, apiToken);
47+
this.employee = new EmployeeEntity(apiUrl, apiToken);
4648
}
4749

4850
public async getApiVersion(externalConfig?: NextFetchRequestConfig) {
@@ -54,18 +56,6 @@ export class MainAPI {
5456
);
5557
}
5658

57-
public async signInEmployeeByEmail(
58-
data: SignInByEmailRequest,
59-
externalConfig?: NextFetchRequestConfig,
60-
) {
61-
return this.request<SignInByEmailResponse>(
62-
'auth/employee/email',
63-
'POST',
64-
data,
65-
externalConfig,
66-
);
67-
}
68-
6959
public async verifyToken(
7060
token: string,
7161
externalConfig?: NextFetchRequestConfig,

sdk/endpoints/auth.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

sdk/endpoints/employee.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export type EmployeeCreateResponse = {
2929
result: Employee;
3030
};
3131

32+
// ---------------------------------------------------- //
3233
export const EmployeeContactCreateRequestSchema = z.object({
3334
employeeId: z.string(),
3435
type: z.string(),
@@ -44,6 +45,7 @@ export type EmployeeContactCreateResponse = {
4445
result: EmployeeContact;
4546
};
4647

48+
// ---------------------------------------------------- //
4749
export const EmployeePasswordCreateRequestSchema = z.object({
4850
employeeId: z.string(),
4951
password: z.string(),
@@ -56,6 +58,7 @@ export type EmployeePasswordCreateResponse = {
5658
ok: boolean;
5759
};
5860

61+
// ---------------------------------------------------- //
5962
export const EmployeePermissionCreateRequestSchema = z.object({
6063
employeeId: z.string(),
6164
type: z.enum(employeePermissionTypes as [string, ...string[]]),
@@ -68,3 +71,17 @@ export type EmployeePermissionCreateResponse = {
6871
ok: boolean;
6972
result: EmployeePermission;
7073
};
74+
75+
// ---------------------------------------------------- //
76+
export const SignInByEmailRequestSchema = z.object({
77+
email: z.string().email(),
78+
password: z.string().min(6),
79+
});
80+
81+
export type SignInByEmailRequest = z.infer<typeof SignInByEmailRequestSchema>;
82+
export type SignInByEmailResponse = {
83+
ok: boolean;
84+
result: {
85+
access_token: string;
86+
};
87+
};

sdk/endpoints/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from './product';
2-
export * from './auth';
32
export * from './media';
43
export * from './checkout';
54
export * from './shop';

sdk/entities/EmployeeEntity.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { SignInByEmailRequest, SignInByEmailResponse } from '../endpoints';
2+
import { NextFetchRequestConfig } from '../types/next';
3+
import { ErrorBase } from '../errors';
4+
import { fetchAPI } from '../fetchAPI';
5+
6+
export class EmployeeEntity {
7+
private readonly apiUrl: string;
8+
private readonly apiToken: string;
9+
10+
constructor(apiUrl: string, apiToken: string) {
11+
this.apiUrl = apiUrl;
12+
this.apiToken = apiToken;
13+
}
14+
15+
private async request<T, E = ErrorBase>(
16+
endpoint: string,
17+
method: 'POST' | 'GET' | 'PATCH' = 'POST',
18+
data?: unknown,
19+
externalConfig?: NextFetchRequestConfig,
20+
): Promise<T | E> {
21+
return fetchAPI<T, E>(
22+
{
23+
token: this.apiToken,
24+
url: this.apiUrl,
25+
},
26+
endpoint,
27+
{
28+
body: JSON.stringify(data),
29+
method,
30+
},
31+
externalConfig,
32+
);
33+
}
34+
35+
public async signInByEmail(
36+
data: SignInByEmailRequest,
37+
externalConfig?: NextFetchRequestConfig,
38+
) {
39+
return this.request<SignInByEmailResponse>(
40+
'employee/email',
41+
'POST',
42+
data,
43+
externalConfig,
44+
);
45+
}
46+
}

sdk/entities/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export { MenuCategoryEntity } from './MenuCategoryEntity';
77
export { CheckoutEntity } from './CheckoutEntity';
88
export { ProductEntity } from './ProductEntity';
99
export { ProductVariantEntity } from './ProductVariantEntity';
10+
export { EmployeeEntity } from './EmployeeEntity';

sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@next-orders/api-sdk",
3-
"version": "0.3.4",
3+
"version": "0.3.5",
44
"description": "TS Lib: Easy ability to make requests to Main API via NPM package. 100% typed.",
55
"scripts": {
66
"build": "tsup",

src/core/auth/auth.controller.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
import {
2-
BadRequestException,
3-
Body,
4-
Controller,
5-
Get,
6-
HttpCode,
7-
HttpStatus,
8-
Param,
9-
Post,
10-
UnauthorizedException,
11-
} from '@nestjs/common';
1+
import { BadRequestException, Controller, Get, Param } from '@nestjs/common';
122
import { AuthService } from '@/core/auth/auth.service';
13-
import { SignInByEmailDto } from '@/core/auth/dto/signin-by-email.dto';
14-
import { SignInByEmailResponse } from '../../../sdk/endpoints';
153
import { Public } from '@/core/auth/auth.decorator';
164
import { ConfigService } from '@nestjs/config';
175

@@ -33,20 +21,6 @@ export class AuthController {
3321
return payload;
3422
}
3523

36-
@Public()
37-
@HttpCode(HttpStatus.OK)
38-
@Post('employee/email')
39-
async signInByEmail(
40-
@Body() dto: SignInByEmailDto,
41-
): Promise<SignInByEmailResponse> {
42-
const jwt = await this.service.signInByEmail(dto);
43-
if (!jwt) {
44-
throw new UnauthorizedException();
45-
}
46-
47-
return jwt;
48-
}
49-
5024
@Public()
5125
@Get('employee/demo')
5226
async getDemoSignIn() {

src/core/auth/auth.module.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Module } from '@nestjs/common';
2-
import { PrismaService } from '@/db/prisma.service';
32
import { AuthController } from '@/core/auth/auth.controller';
43
import { AuthService } from '@/core/auth/auth.service';
5-
import { EmployeeService } from '@/core/employee/employee.service';
64
import { JwtService } from '@nestjs/jwt';
75

86
@Module({
97
controllers: [AuthController],
10-
providers: [AuthService, EmployeeService, PrismaService, JwtService],
8+
providers: [AuthService, JwtService],
9+
exports: [AuthService],
1110
})
1211
export class AuthModule {}

src/core/auth/auth.service.ts

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@ import type {
77
JWTEmployeeAccessTokenPayload,
88
JWTEmployeeData,
99
} from '@api-sdk';
10-
import { PrismaService } from '@/db/prisma.service';
11-
import { EmployeeService } from '@/core/employee/employee.service';
12-
import { SignInByEmailDto } from '@/core/auth/dto/signin-by-email.dto';
1310

1411
@Injectable()
1512
export class AuthService {
1613
constructor(
1714
private readonly config: ConfigService,
18-
private readonly employee: EmployeeService,
1915
private readonly jwt: JwtService,
20-
private readonly prisma: PrismaService,
2116
) {}
2217

2318
async verifyToken(token: string) {
@@ -38,32 +33,13 @@ export class AuthService {
3833
}
3934
}
4035

41-
async signInByEmail(dto: SignInByEmailDto) {
42-
const employee = await this.employee.findEmployeeByContact(
43-
dto.email,
44-
'EMAIL',
45-
);
46-
if (!employee) {
47-
return null;
48-
}
49-
50-
const isPasswordValid = await this.employee.checkPassword(
51-
employee.id,
52-
dto.password,
53-
);
54-
if (!isPasswordValid) {
55-
return null;
56-
}
57-
58-
// Get all Permissions
59-
const permissions = employee.permissions.map(
60-
(p: { type: EmployeePermissionType }) => p.type,
61-
);
62-
63-
// Generate a JWT
36+
async createToken(
37+
userId: string,
38+
permissions: EmployeePermissionType[],
39+
): Promise<string> {
6440
const sub = createId();
6541
const user: JWTEmployeeData = {
66-
id: employee.id,
42+
id: userId,
6743
permissions,
6844
};
6945

@@ -72,15 +48,8 @@ export class AuthService {
7248
user,
7349
};
7450

75-
const access_token = await this.jwt.signAsync(payload, {
51+
return this.jwt.signAsync(payload, {
7652
secret: this.config.getOrThrow('JWT_SECRET'),
7753
});
78-
79-
return {
80-
ok: true,
81-
result: {
82-
access_token,
83-
},
84-
};
8554
}
8655
}

0 commit comments

Comments
 (0)