Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
313 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# postgres | ||
DATABASE_URL_PROD= | ||
DATABASE_URL_PROD= | ||
|
||
# jwt secret | ||
JWT_SECRET= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
prisma/migrations/20220421030425_add_password_and_update_email_to_unique/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
Warnings: | ||
- A unique constraint covering the columns `[email]` on the table `users` will be added. If there are existing duplicate values, this will fail. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "users" ADD COLUMN "password" TEXT; | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "users_email_key" ON "users"("email"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class LoginDto { | ||
@ApiProperty() | ||
email: string; | ||
|
||
@ApiProperty() | ||
password: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { LoginController } from './login.controller'; | ||
|
||
describe('LoginController', () => { | ||
let controller: LoginController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [LoginController], | ||
}).compile(); | ||
|
||
controller = module.get<LoginController>(LoginController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Controller, Post, Body } from '@nestjs/common'; | ||
import { LoginService } from './login.service'; | ||
import { LoginDto } from './dto/login.dto'; | ||
|
||
import { ApiResponse } from '@nestjs/swagger'; | ||
import { LoginResponse } from './response/login.response'; | ||
import jwt from 'jsonwebtoken'; | ||
|
||
@Controller('login') | ||
export class LoginController { | ||
constructor(private readonly loginService: LoginService) {} | ||
|
||
@Post() | ||
@ApiResponse({ | ||
status: 201, | ||
description: 'Successful response', | ||
type: LoginResponse, | ||
}) | ||
async login(@Body() loginDto: LoginDto): Promise<any> { | ||
let response = {}; | ||
|
||
const users = await this.loginService.login(loginDto); | ||
if (users) { | ||
const token = jwt.sign( | ||
{ id: users.id, email: users.email }, | ||
process.env.JWT_SECRET, | ||
{ expiresIn: '1d' }, | ||
); | ||
|
||
response = { | ||
message: 'login', | ||
users: { | ||
id: users.id, | ||
first_name: users.first_name, | ||
last_name: users.last_name, | ||
gender: users.gender, | ||
email: users.email, | ||
created_at: users.created_at, | ||
updated_at: users.updated_at, | ||
}, | ||
token: token, | ||
}; | ||
} | ||
|
||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { LoginController } from './login.controller'; | ||
import { LoginService } from './login.service'; | ||
import { PrismaService } from '../prisma.service'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [LoginController], | ||
providers: [LoginService, PrismaService], | ||
}) | ||
export class LoginModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { LoginService } from './login.service'; | ||
|
||
describe('LoginService', () => { | ||
let service: LoginService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [LoginService], | ||
}).compile(); | ||
|
||
service = module.get<LoginService>(LoginService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { LoginDto } from './dto/login.dto'; | ||
import { PrismaService } from '../prisma.service'; | ||
import { users } from '@prisma/client'; | ||
import bcrypt from 'bcryptjs'; | ||
|
||
@Injectable() | ||
export class LoginService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
async login(loginDto: LoginDto): Promise<users> { | ||
let result = null; | ||
|
||
const users = await this.prisma.users.findFirst({ | ||
where: { | ||
email: loginDto.email, | ||
}, | ||
}); | ||
|
||
const isValidPassword = bcrypt.compareSync( | ||
loginDto.password, | ||
users.password, | ||
); | ||
console.log('isValidPassword = ', isValidPassword); | ||
if (isValidPassword) { | ||
result = users; | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class User { | ||
@ApiProperty() | ||
id: string; | ||
|
||
@ApiProperty() | ||
first_name: string; | ||
|
||
@ApiProperty() | ||
last_name: string; | ||
|
||
@ApiProperty() | ||
gender: string; | ||
|
||
@ApiProperty() | ||
email: string; | ||
|
||
@ApiProperty() | ||
created_at: string; | ||
|
||
@ApiProperty() | ||
updated_at: string; | ||
} | ||
|
||
export class LoginResponse { | ||
@ApiProperty() | ||
message: string; | ||
|
||
@ApiProperty() | ||
user: User; | ||
|
||
@ApiProperty() | ||
token: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.