Skip to content

Commit 7911146

Browse files
Merge pull request #14 from DevAnseSenior/refresh-token
Refresh token
2 parents fab775e + c6ef819 commit 7911146

21 files changed

Lines changed: 213 additions & 23 deletions

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,30 @@ go-gym-api/
2727
│ ├── migrations # stored migration history
2828
│ └── schema.prisma # Database tables schemas
2929
├── src/
30+
│ ├── @types/ # Store globals interfaces on project
3031
│ ├── env/
3132
│ │ └── index.ts # Enviroment vars validation
3233
│ ├── http/
3334
│ │ ├── controllers/ # Handler of request response
34-
│ │ └── routes.ts # application endpoints
35+
│ │ └── middlewares # Functions to interact between controllers and endpoints
3536
│ ├── lib/
3637
│ │ └── prisma.ts # Prisma client config
3738
│ ├── repositories/ # Layer for database manipulation tools
3839
│ │ ├── in-memory/
3940
│ │ ├── prisma/
4041
│ │ ├── check-ins-repository.ts # Check-ins Interface
42+
│ │ ├── gyms-repository.ts # Check-ins Interface
4143
│ │ └── users-repository.ts # Users Interface
42-
│ ├── use-cases/ # Use case layer for specific entities
43-
│ │ ├── factories/ # Use case factories for instances
44-
│ │ └── errors/ # Use case error messages
44+
│ ├── use-cases/ # Use cases layer for entities
45+
│ │ ├── errors/ # Use case error messages
46+
│ │ └── factories/ # Use case factories for instances
4547
│ ├── utils/ # Application utility functions
48+
│ │ └── test/ # Utility functions for tests
4649
│ ├── app.ts # Fastify app setup
4750
│ └── server.ts # Server setup config
4851
├── env.example # Enviroment variables example
4952
├── .eslintrc.json # ESLint Config
53+
├── .gitignore
5054
├── .npmrc # NPM config libs control
5155
├── docker-compose.yml
5256
├── LICENSE
@@ -86,7 +90,8 @@ go-gym-api/
8690
5. **Run tests**:
8791
```bash
8892
docker compose up -d # Start the services
89-
npm run test # Run the tests suit
93+
npm run test # Run the unit tests suits
94+
npm run test:e2e # Run the tests e2e suits
9095
npm run test:watch # Run the tests on watch mode
9196
npm run test:coverage # Verify the tests coverage (open index.html from dir coverage/)
9297
npm run test:ui # Execute the test client of vitest
@@ -96,6 +101,18 @@ go-gym-api/
96101
### Users
97102
- POST /users → Register new user.
98103
- POST /sessions → Authenticate (login).
104+
- GET /me → Get logged user info.
105+
106+
### Gyms
107+
- POST /gyms → Create a new gym.
108+
- GET /gyms/search → Search gym by title.
109+
- GET /gyms/nearby → Search nearby user localization gyms.
110+
111+
### Check-ins
112+
- POST /gyms/:gymId/check-ins → Check-in.
113+
- PATCH /check-ins/:checkInId/validate → Gym validations check-in.
114+
- GET /check-ins/history → Get check-ins history.
115+
- GET /check-ins/metrics → Get check-ins count of logged user.
99116

100117
## 📄 License
101118
This project is licensed under MIT [License](./LICENSE).

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"vitest": "3.2.4"
4242
},
4343
"dependencies": {
44+
"@fastify/cookie": "11.0.2",
4445
"@fastify/jwt": "10.0.0",
4546
"@prisma/client": "6.14.0",
4647
"bcryptjs": "3.0.2",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- CreateEnum
2+
CREATE TYPE "public"."Role" AS ENUM ('ADMIN', 'MEMBER');
3+
4+
-- AlterTable
5+
ALTER TABLE "public"."users" ADD COLUMN "role" "public"."Role" NOT NULL DEFAULT 'MEMBER';

prisma/schema.prisma

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ datasource db {
1414
url = env("DATABASE_URL")
1515
}
1616

17+
enum Role {
18+
ADMIN
19+
MEMBER
20+
}
21+
1722
model User {
1823
id String @id @default(uuid())
1924
name String
2025
email String @unique
2126
password_hash String
27+
role Role @default(MEMBER)
2228
created_at DateTime @default(now())
2329
2430
checkIns CheckIn[]

src/@types/fastify-jwt.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import '@fastify/jwt'
33
declare module '@fastify/jwt' {
44
export interface FastifyJWT {
55
user: {
6+
role: 'ADMIN' | 'MEMBER'
67
sub: string
78
}
89
}

src/app.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fastifyCookie from '@fastify/cookie'
12
import fastifyJwt from '@fastify/jwt'
23
import fastify from 'fastify'
34
import { ZodError } from 'zod'
@@ -10,8 +11,17 @@ export const app = fastify()
1011

1112
app.register(fastifyJwt, {
1213
secret: env.JWT_SECRET,
14+
cookie: {
15+
cookieName: 'refreshToken',
16+
signed: false,
17+
},
18+
sign: {
19+
expiresIn: '10m',
20+
},
1321
})
1422

23+
app.register(fastifyCookie)
24+
1525
app.register(usersRoutes)
1626
app.register(gymsRoutes)
1727
app.register(checkInsRoutes)

src/http/controllers/check-ins/create.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Create Check-In (e2e)', () => {
1414
})
1515

1616
it('should be able to create a check-in', async () => {
17-
const { token } = await createAndAuthenticateUser(app)
17+
const { token } = await createAndAuthenticateUser(app, true)
1818

1919
const gym = await prisma.gym.create({
2020
data: {

src/http/controllers/check-ins/routes.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { verifyJwt } from '@/http/middlewares/verify-jwt'
22
import { FastifyInstance } from 'fastify'
33

4+
import { verifyUserRole } from '@/http/middlewares/verify-user-role'
45
import { create } from './create'
56
import { history } from './history'
67
import { metrics } from './metrics'
@@ -13,5 +14,10 @@ export async function checkInsRoutes(app: FastifyInstance) {
1314
app.get('/check-ins/metrics', metrics)
1415

1516
app.post('/gyms/:gymId/check-ins', create)
16-
app.patch('/check-ins/:checkInId/validate', validate)
17+
18+
app.patch(
19+
'/check-ins/:checkInId/validate',
20+
{ onRequest: verifyUserRole('ADMIN') },
21+
validate,
22+
)
1723
}

src/http/controllers/check-ins/validate.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Validate Check-In (e2e)', () => {
1414
})
1515

1616
it('should be able to validate a check-in', async () => {
17-
const { token } = await createAndAuthenticateUser(app)
17+
const { token } = await createAndAuthenticateUser(app, true)
1818

1919
const user = await prisma.user.findFirstOrThrow()
2020

0 commit comments

Comments
 (0)