-
Notifications
You must be signed in to change notification settings - Fork 0
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
Berat Genç
committed
Jan 20, 2024
1 parent
49ff619
commit e9a6ee7
Showing
15 changed files
with
178 additions
and
120 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
prisma/migrations/20240119212433_cascade_added/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,35 @@ | ||
-- DropForeignKey | ||
ALTER TABLE `EmployeeOnTeams` DROP FOREIGN KEY `EmployeeOnTeams_employeeId_fkey`; | ||
|
||
-- DropForeignKey | ||
ALTER TABLE `EmployeeOnTeams` DROP FOREIGN KEY `EmployeeOnTeams_teamId_fkey`; | ||
|
||
-- DropForeignKey | ||
ALTER TABLE `Project` DROP FOREIGN KEY `Project_employeeId_fkey`; | ||
|
||
-- DropForeignKey | ||
ALTER TABLE `Project` DROP FOREIGN KEY `Project_teamId_fkey`; | ||
|
||
-- DropForeignKey | ||
ALTER TABLE `Task` DROP FOREIGN KEY `Task_projectId_fkey`; | ||
|
||
-- DropForeignKey | ||
ALTER TABLE `Task` DROP FOREIGN KEY `Task_userId_fkey`; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `EmployeeOnTeams` ADD CONSTRAINT `EmployeeOnTeams_teamId_fkey` FOREIGN KEY (`teamId`) REFERENCES `Team`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `EmployeeOnTeams` ADD CONSTRAINT `EmployeeOnTeams_employeeId_fkey` FOREIGN KEY (`employeeId`) REFERENCES `Employee`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `Project` ADD CONSTRAINT `Project_teamId_fkey` FOREIGN KEY (`teamId`) REFERENCES `Team`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `Project` ADD CONSTRAINT `Project_employeeId_fkey` FOREIGN KEY (`employeeId`) REFERENCES `Employee`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `Task` ADD CONSTRAINT `Task_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `Employee`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `Task` ADD CONSTRAINT `Task_projectId_fkey` FOREIGN KEY (`projectId`) REFERENCES `Project`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; |
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,57 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
Post, | ||
Req, | ||
Res, | ||
UnauthorizedException | ||
} from '@nestjs/common' | ||
import { AuthService } from './auth.service' | ||
import { Prisma } from '@prisma/client' | ||
import { Request, Response } from 'express' | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private readonly authService: AuthService) {} | ||
|
||
@Post('/signup') | ||
async signup( | ||
@Body() createUserDto: Prisma.EmployeeCreateInput, | ||
@Req() request: Request | ||
) { | ||
const userData = await this.authService.signup(createUserDto) | ||
|
||
// @ts-ignore- | ||
request.session.userId = userData.userId | ||
// @ts-ignore- | ||
request.session.teamId = userData.teamId | ||
} | ||
|
||
@Post('/signin') | ||
async login( | ||
@Body() loginData: Partial<Prisma.EmployeeCreateInput>, | ||
@Req() request: Request | ||
) { | ||
const userData = await this.authService.signin(loginData) | ||
|
||
if (userData) { | ||
// @ts-ignore- | ||
request.session.userId = userData.userId | ||
// @ts-ignore- | ||
request.session.teamId = userData.teamId | ||
return { teamId: userData.teamId, userId: userData.userId } | ||
} | ||
|
||
throw new UnauthorizedException('Invalid credentials') | ||
} | ||
|
||
@Get() | ||
async logout(@Req() req: Request, @Res() res: Response) { | ||
req.session.destroy(err => { | ||
res | ||
.clearCookie('sessionCookie', { domain: 'auth-test.site' }) | ||
.sendStatus(200) | ||
}) | ||
} | ||
} |
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 { Module } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthController } from './auth.controller'; | ||
|
||
@Module({ | ||
controllers: [AuthController], | ||
providers: [AuthService], | ||
}) | ||
export class AuthModule {} |
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,30 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { Prisma } from '@prisma/client' | ||
import { DatabaseService } from 'src/database/database.service' | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor(private readonly databaseService: DatabaseService) {} | ||
|
||
async signup(createUserDto: Prisma.EmployeeCreateInput) { | ||
const user = await this.databaseService.employee.create({ | ||
data: { | ||
...createUserDto, | ||
teams: { create: { team: { create: { name: 'My Team' } } } } | ||
}, | ||
|
||
include: { teams: { include: { team: true } } } | ||
}) | ||
|
||
return { userId: user.id, teamId: user.teams[0].teamId } | ||
} | ||
|
||
async signin({ email, password }: Partial<Prisma.EmployeeCreateInput>) { | ||
const user = await this.databaseService.employee.findUnique({ | ||
where: { email, password }, | ||
include: { teams: { include: { team: true } } } | ||
}) | ||
|
||
return user ? { userId: user.id, teamId: user.teams[0].teamId } : false | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.