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

User authentication, controller, routes and unit testing #13

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"vitest": "^1.2.1"
},
"dependencies": {
"@fastify/cors": "^9.0.0",
"@fastify/jwt": "^8.0.0",
"@prisma/client": "5.8.1",
"@types/bcryptjs": "^2.4.6",
"@types/express": "^4.17.21",
Expand Down
117 changes: 116 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@ import fastify from 'fastify'
import { userRoutes } from './controller/user/routes'
import { env } from './env'
import { ZodError } from 'zod'
import { authRoutes } from './controller/session/routes'
import fastifyJwt from '@fastify/jwt'
import cors from '@fastify/cors'


export const app = fastify()
app.register(cors, {
origin: ['http://127.0.0.1:5173', 'http://localhost:5173'],
})
app.register(userRoutes)
app.register(authRoutes)


app.register(fastifyJwt, {
secret: 'squad40',
// sign: {
// expiresIn: '10s',
// },
})

app.setErrorHandler((error, _, response) => {
if (env.NODE_ENV !== 'production') {
Expand Down
6 changes: 6 additions & 0 deletions src/configs/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
jwt: {
secret: "squad40",
expiresIn: "7d"
}
}
12 changes: 12 additions & 0 deletions src/controller/middlewares/verifyJwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FastifyReply, FastifyRequest } from 'fastify'

export async function verifyJWT(
request: FastifyRequest,
response: FastifyReply,
) {
try {
await request.jwtVerify()
} catch (e) {
return response.status(401).send({ message: 'Unauthorized' })
}
}
35 changes: 35 additions & 0 deletions src/controller/session/authUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { FastifyReply, FastifyRequest } from 'fastify'
import { InMemoryUserRepository } from '../../repositories/in-memory-db/inMemoryUserRepository'
import { AuthUserUseCase } from '../../use-cases/authUserUseCase'
import { z } from 'zod'
// const { sign } = require('jsonwebtoken')
// const authConfig = require('../../configs/auth')

export async function authUser(
request: FastifyRequest,
response: FastifyReply,
) {

const userRepository = new InMemoryUserRepository()
const authUserUseCase = new AuthUserUseCase(userRepository)

const AuthUserUseCaseSchema = z.object({
email: z.string().email(),
password: z.string()
})

const { email, password } = AuthUserUseCaseSchema.parse(request.body)

const { user } = await authUserUseCase.execute({ email, password })

const token = await response.jwtSign(
{},
{
sign: {
sub: user.id,
},
},
)

return response.status(200).send({ user, token })
}
7 changes: 7 additions & 0 deletions src/controller/session/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FastifyInstance } from 'fastify'
import { authUser } from './authUser'

export async function authRoutes(app: FastifyInstance) {

app.post('/login', authUser)
}
4 changes: 3 additions & 1 deletion src/controller/user/routes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { FastifyInstance } from 'fastify'
import { getUserById } from './getUserById'
import { getUserByEmail } from './getUserByEmail'
import { verifyJWT } from '../middlewares/verifyJwt'

export async function userRoutes(app: FastifyInstance) {

app.get('/user/:id', getUserById)
app.get('/user', getUserByEmail)
// app.get('/user', getUserByEmail)
app.get('/user', { onRequest: [verifyJWT] }, getUserByEmail)
}
Loading