Skip to content

Commit 12fc222

Browse files
committed
update
Signed-off-by: Kirill Mokevnin <[email protected]>
1 parent d518b44 commit 12fc222

File tree

16 files changed

+365
-39
lines changed

16 files changed

+365
-39
lines changed

main.tsp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ model CourseLesson {
108108
...Timestamps;
109109
}
110110

111+
model AuthInfo {
112+
email: string;
113+
password: string;
114+
}
115+
116+
model TokenInfo {
117+
token: string;
118+
}
119+
111120
model CourseLessonCreateDTO {
112121
name: string;
113122
body: string;
@@ -145,6 +154,15 @@ namespace users {
145154
} | NotFoundError;
146155
}
147156

157+
@route("/tokens")
158+
namespace tokens {
159+
@post
160+
op create(@body auth_info: AuthInfo): {
161+
@body token: TokenInfo;
162+
@statusCode statusCode: 201;
163+
} | UnprocessableEntityError;
164+
}
165+
148166
@route("/courses")
149167
namespace courses {
150168
@get
@@ -168,9 +186,13 @@ namespace courses {
168186

169187
@patch
170188
@useAuth(BearerAuth)
171-
op update(@path id: numeric, @body course: CourseEditDTO): {
172-
@body course: Course;
173-
} | NotFoundError | UnprocessableEntityError | UnauthorizedError;
189+
op update(@path id: numeric, @body course: CourseEditDTO):
190+
| {
191+
@body course: Course;
192+
}
193+
| NotFoundError
194+
| UnprocessableEntityError
195+
| UnauthorizedError;
174196

175197
@delete
176198
@useAuth(BearerAuth)

package-lock.json

Lines changed: 137 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"dependencies": {
1111
"@fastify/autoload": "^5.10.0",
12-
"@fastify/bearer-auth": "^9.4.0",
12+
"@fastify/jwt": "^8.0.1",
1313
"@fastify/sensible": "^5.6.0",
1414
"@fastify/swagger": "^8.15.0",
1515
"@fastify/type-provider-typebox": "^4.1.0",

plugins/bearer.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

plugins/jwt.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import fp from 'fastify-plugin'
2+
import jwtPlugin from '@fastify/jwt'
3+
4+
export default fp(async (fastify) => {
5+
fastify.register(jwtPlugin, {
6+
secret: 'supersecret',
7+
})
8+
fastify.decorate('authenticate', async function (request, reply) {
9+
try {
10+
await request.jwtVerify()
11+
}
12+
catch (err) {
13+
reply.send(err)
14+
}
15+
})
16+
})

routes/api/courses.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default async function (fastify) {
4545
fastify.post(
4646
'/courses',
4747
{
48-
preHandler: fastify.verifyBearerAuth,
48+
onRequest: [fastify.authenticate],
4949
schema: {
5050
body: schema['/courses'].POST.args.properties.body,
5151
response: {
@@ -69,7 +69,7 @@ export default async function (fastify) {
6969
fastify.patch(
7070
'/courses/:id',
7171
{
72-
preHandler: fastify.verifyBearerAuth,
72+
onRequest: [fastify.authenticate],
7373
schema: schema['/courses/{id}'].PATCH.args.properties,
7474
},
7575
async (request) => {
@@ -87,7 +87,7 @@ export default async function (fastify) {
8787
fastify.delete(
8888
'/courses/:id',
8989
{
90-
preHandler: fastify.verifyBearerAuth,
90+
onRequest: [fastify.authenticate],
9191
schema: schema['/courses/{id}'].DELETE.args.properties,
9292
},
9393
async (request, reply) => {

routes/api/tokens.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { and, asc, eq } from 'drizzle-orm'
2+
import { schema } from '../../schema.js'
3+
4+
/**
5+
* @param {import('fastify').FastifyTypebox} fastify
6+
*/
7+
export default async function (fastify) {
8+
const db = fastify.db
9+
10+
const postTokens = schema['/tokens'].POST
11+
fastify.post(
12+
'/tokens',
13+
{
14+
// preHandler: fastify.verifyBearerAuth,
15+
schema: {
16+
body: postTokens.args.properties.body,
17+
response: {
18+
201: postTokens.data,
19+
422: postTokens.error,
20+
},
21+
},
22+
},
23+
async (request, reply) => {
24+
const client = await fastify.db.query.users.findFirst()
25+
fastify.assert.ok(client)
26+
const token = fastify.jwt.sign({ user: { id: client.id } })
27+
return reply.code(201)
28+
.send({ token })
29+
},
30+
)
31+
}

0 commit comments

Comments
 (0)