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

[SPGO-12] implement register #3

Merged
merged 13 commits into from
Sep 20, 2023
Merged
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
proto:
protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto -I=./src/proto --ts_proto_out=./src/auth src/proto/auth.proto --ts_proto_opt=nestJs=true --ts_proto_opt=fileSuffix=.pb
protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto.cmd -I=./src/proto --ts_proto_out=./src/auth src/proto/auth.proto --ts_proto_opt=nestJs=true --ts_proto_opt=fileSuffix=.pb

server:
yarn start:dev
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
"@types/bcrypt": "^5.0.0",
"@types/passport-jwt": "^3.0.9",
"bcrypt": "^5.1.1",
"grpc_tools_node_protoc_ts": "^5.3.3",
"nestjs-grpc-exceptions": "^0.2.1",
"nestjs-proto-gen-ts": "^1.0.21",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"protoc-gen-ts": "^0.8.6",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0",
"ts-proto": "^1.157.0",
"ts-proto": "^1.157.1",
"ts-protoc-gen": "^0.15.0"
},
"devDependencies": {
Expand All @@ -55,6 +58,7 @@
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"grpc-tools": "^1.12.4",
"jest": "29.3.1",
"prettier": "^2.3.2",
"prisma": "^5.2.0",
Expand Down
11 changes: 10 additions & 1 deletion src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Controller } from '@nestjs/common';
import { GrpcMethod } from '@nestjs/microservices';
import { LoginRequest, LoginResponse } from './auth.pb';
import {
LoginRequest,
LoginResponse,
RegisterRequest,
RegisterResponse,
} from './auth.pb';
import { AuthService } from './auth.service';

@Controller()
Expand All @@ -11,4 +16,8 @@ export class AuthController {
login(request: LoginRequest): Promise<LoginResponse> {
return this.authService.login(request);
}
@GrpcMethod('AuthService', 'Register')
registerUser(request: RegisterRequest): Promise<RegisterResponse> {
return this.authService.register(request);
}
}
68 changes: 56 additions & 12 deletions src/auth/auth.pb.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
/* eslint-disable */
import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices";
import { Observable } from "rxjs";
import { GrpcMethod, GrpcStreamMethod } from '@nestjs/microservices';
import { Observable } from 'rxjs';

export const protobufPackage = "auth";
export const protobufPackage = 'auth';

export interface RegisterRequest {
firstName: string;
lastName: string;
email: string;
phoneNumber: string;
password: string;
role: string;
}

export interface RegisterResponse {
firstName: string;
lastName: string;
email: string;
phoneNumber: string;
}

export interface Credential {
accessToken: string;
Expand All @@ -28,35 +44,63 @@ export interface RefreshTokenResponse {
credential: Credential | undefined;
}

export const AUTH_PACKAGE_NAME = "auth";
export const AUTH_PACKAGE_NAME = 'auth';

export interface AuthServiceClient {
login(request: LoginRequest): Observable<LoginResponse>;

refreshToken(request: RefreshTokenRequest): Observable<RefreshTokenResponse>;

register(request: RegisterRequest): Observable<RegisterResponse>;
}

export interface AuthServiceController {
login(request: LoginRequest): Promise<LoginResponse> | Observable<LoginResponse> | LoginResponse;
login(
request: LoginRequest,
): Promise<LoginResponse> | Observable<LoginResponse> | LoginResponse;

refreshToken(
request: RefreshTokenRequest,
): Promise<RefreshTokenResponse> | Observable<RefreshTokenResponse> | RefreshTokenResponse;
):
| Promise<RefreshTokenResponse>
| Observable<RefreshTokenResponse>
| RefreshTokenResponse;

register(
request: RegisterRequest,
):
| Promise<RegisterResponse>
| Observable<RegisterResponse>
| RegisterResponse;
}

export function AuthServiceControllerMethods() {
return function (constructor: Function) {
const grpcMethods: string[] = ["login", "refreshToken"];
const grpcMethods: string[] = ['login', 'refreshToken', 'register'];
for (const method of grpcMethods) {
const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method);
GrpcMethod("AuthService", method)(constructor.prototype[method], method, descriptor);
const descriptor: any = Reflect.getOwnPropertyDescriptor(
constructor.prototype,
method,
);
GrpcMethod('AuthService', method)(
constructor.prototype[method],
method,
descriptor,
);
}
const grpcStreamMethods: string[] = [];
for (const method of grpcStreamMethods) {
const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method);
GrpcStreamMethod("AuthService", method)(constructor.prototype[method], method, descriptor);
const descriptor: any = Reflect.getOwnPropertyDescriptor(
constructor.prototype,
method,
);
GrpcStreamMethod('AuthService', method)(
constructor.prototype[method],
method,
descriptor,
);
}
};
}

export const AUTH_SERVICE_NAME = "AuthService";
export const AUTH_SERVICE_NAME = 'AuthService';
31 changes: 31 additions & 0 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import {
LoginResponse,
RefreshTokenRequest,
RefreshTokenResponse,
RegisterRequest,
RegisterResponse,
} from './auth.pb';
import { RpcException } from '@nestjs/microservices';
import { status } from '@grpc/grpc-js';
import { GrpcPermissionDeniedException } from 'nestjs-grpc-exceptions';
import { v4 as uuidv4 } from 'uuid';
import { Role } from '@prisma/client';

@Injectable()
export class AuthService implements AuthServiceController {
Expand Down Expand Up @@ -119,4 +123,31 @@ export class AuthService implements AuthServiceController {
});
}
}

async register(request: RegisterRequest): Promise<RegisterResponse> {
try {
const existUser = this.userRepo.getUserByEmail(request.email);
if (existUser) {
throw new RpcException({
code: status.ALREADY_EXISTS,
message: 'Duplicate Email',
});
} else {
const hashedPassword = await bcrypt.hash(request.password, 12);
const newUser = {
id: uuidv4(),
firstName: request.firstName,
lastName: request.lastName,
email: request.email,
phoneNumber: request.phoneNumber,
role: request.role == 'USER' ? Role.USER : Role.SPORTAREA,
password: hashedPassword,
};
return await this.userRepo.create(newUser);
}
} catch (err) {
console.log(err);
throw err;
}
}
}
2 changes: 1 addition & 1 deletion src/database/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ datasource db {
}

model User {
id String @id
id String @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
firstName String
Expand Down
17 changes: 17 additions & 0 deletions src/proto/auth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ package auth;
service AuthService {
rpc Login(LoginRequest) returns (LoginResponse) {}
rpc RefreshToken(RefreshTokenRequest) returns (RefreshTokenResponse) {}
rpc Register(RegisterRequest) returns (RegisterResponse) {}
}

message RegisterRequest {
string firstName =1;
string lastName =2;
string email =3;
string phoneNumber =4;
string password =5;
string role =6 ;
}

message RegisterResponse {
string firstName =1;
string lastName =2;
string email =3;
string phoneNumber =4;
}

message Credential {
Expand Down
19 changes: 17 additions & 2 deletions src/repository/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { User } from '@prisma/client';
import { PrismaClient, User } from '@prisma/client';
import { PrismaService } from '../prisma/service/prisma.service';
import { Prisma } from '@prisma/client';

@Injectable()
export class UserRepository {
Expand All @@ -14,9 +15,23 @@ export class UserRepository {
userId: string,
refreshToken: string,
): Promise<User> {
return this.db.user.update({
return await this.db.user.update({
where: { id: userId },
data: { refreshToken: refreshToken },
});
}

async create(createUser: Prisma.UserCreateInput): Promise<User> {
return await this.db.user.create({
data: {
id: createUser.id,
firstName: createUser.firstName,
lastName: createUser.lastName,
email: createUser.email,
phoneNumber: createUser.phoneNumber,
password: createUser.password,
role: createUser.role,
},
});
}
}
Loading