From 3d0b58b28250b3b2e4ebe41ba48bed6774a01bfd Mon Sep 17 00:00:00 2001 From: MCMXC <16797721+mcmxcdev@users.noreply.github.com> Date: Sun, 18 Feb 2024 20:22:20 -0700 Subject: updates --- .../Http/Api/Static/AnnouncementsController.ts | 3 +- app/Controllers/Http/RecipeController.ts | 8 ++--- app/Controllers/Http/UserController.ts | 3 +- app/Models/User.ts | 4 +-- start/routes/api.ts | 35 ++++++++++++---------- start/routes/web.ts | 9 ++++-- tests/bootstrap.ts | 2 ++ 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/app/Controllers/Http/Api/Static/AnnouncementsController.ts b/app/Controllers/Http/Api/Static/AnnouncementsController.ts index 565df5e..85aed4d 100644 --- a/app/Controllers/Http/Api/Static/AnnouncementsController.ts +++ b/app/Controllers/Http/Api/Static/AnnouncementsController.ts @@ -6,7 +6,8 @@ import fs from 'fs-extra'; export default class AnnouncementsController { public async show({ response, params }: HttpContext) { const announcement = path.join( - app.resourcesPath(), + app.appRoot.host, + 'resources', 'announcements', `${params.version}.json`, ); diff --git a/app/Controllers/Http/RecipeController.ts b/app/Controllers/Http/RecipeController.ts index 4d01a02..fd3c57e 100644 --- a/app/Controllers/Http/RecipeController.ts +++ b/app/Controllers/Http/RecipeController.ts @@ -51,7 +51,7 @@ export default class RecipesController { // List official and custom recipes public async list({ response }: HttpContext) { const officialRecipes = fs.readJsonSync( - path.join(app.appRoot, 'recipes', 'all.json'), + path.join(app.appRoot.host, 'recipes', 'all.json'), ); const customRecipesArray = await Recipe.all(); const customRecipes = customRecipesArray.map(recipe => ({ @@ -113,7 +113,7 @@ export default class RecipesController { // Compress files to .tar.gz file const source = app.tmpPath('recipe'); const destination = path.join( - app.appRoot, + app.appRoot.host, `/recipes/archives/${data.id}.tar.gz`, ); @@ -186,7 +186,7 @@ export default class RecipesController { public popularRecipes({ response }: HttpContext) { return response.send( fs - .readJsonSync(path.join(app.appRoot, 'recipes', 'all.json')) + .readJsonSync(path.join(app.appRoot.host, 'recipes', 'all.json')) // eslint-disable-next-line @typescript-eslint/no-explicit-any .filter((recipe: any) => recipe.featured), ); @@ -197,7 +197,7 @@ export default class RecipesController { const updates = []; const recipes = request.all(); const allJson = fs.readJsonSync( - path.join(app.appRoot, 'recipes', 'all.json'), + path.join(app.appRoot.host, 'recipes', 'all.json'), ); for (const recipe of Object.keys(recipes)) { diff --git a/app/Controllers/Http/UserController.ts b/app/Controllers/Http/UserController.ts index 667786b..bcf0171 100644 --- a/app/Controllers/Http/UserController.ts +++ b/app/Controllers/Http/UserController.ts @@ -292,7 +292,8 @@ export default class UsersController { 'x-franz-source': 'Web', }, }); - const content = await rawResponse.json(); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const content: any = await rawResponse.json(); if (!content.message || content.message !== 'Successfully logged in') { const errorMessage = diff --git a/app/Models/User.ts b/app/Models/User.ts index 34df86f..1f10f37 100644 --- a/app/Models/User.ts +++ b/app/Models/User.ts @@ -3,7 +3,6 @@ import { BaseModel, beforeSave, column, hasMany } from '@adonisjs/lucid/orm'; import hash from '@adonisjs/core/services/hash'; import emitter from '@adonisjs/core/services/emitter'; import moment from 'moment'; -import { Encryption } from '@adonisjs/core/encryption'; import randtoken from 'rand-token'; import Token from './Token.js'; import Workspace from './Workspace.js'; @@ -12,6 +11,7 @@ import mail from '@adonisjs/mail/services/main'; import { url } from '#config/app'; import { mailFrom } from '#config/dashboard'; import type { HasMany } from '@adonisjs/lucid/types/relations'; +import encryption from '@adonisjs/core/services/encryption'; export default class User extends BaseModel { @column({ isPrimary: true }) @@ -99,7 +99,7 @@ export default class User extends BaseModel { return row.token; } - const token = Encryption.encrypt(randtoken.generate(16)); + const token = encryption.encrypt(randtoken.generate(16)); await user.related('tokens').create({ type, token }); diff --git a/start/routes/api.ts b/start/routes/api.ts index 54250a2..37f7859 100644 --- a/start/routes/api.ts +++ b/start/routes/api.ts @@ -1,5 +1,6 @@ // As this is currently a rebuild of the initial API we it is grouped in /v2/ +import { middleware } from '#start/kernel'; import router from '@adonisjs/core/services/router'; const UserController = () => import('#controllers/Http/UserController'); const ServiceController = () => import('#controllers/Http/ServiceController'); @@ -16,33 +17,37 @@ const AnnouncementsController = () => router .group(() => { // User authentification - router.post('auth/signup', [UserController, 'signup']).middleware('guest'); - router.post('auth/login', [UserController, 'login']).middleware('guest'); + router + .post('auth/signup', [UserController, 'signup']) + .use(middleware.guest()); + router + .post('auth/login', [UserController, 'login']) + .use(middleware.guest()); // User info - router.get('me', [UserController, 'me']).middleware('auth:jwt'); - router.put('me', [UserController, 'updateMe']).middleware('auth:jwt'); + router.get('me', [UserController, 'me']).use(middleware.auth()); + router.put('me', [UserController, 'updateMe']).use(middleware.auth()); router .get('me/newtoken', [UserController, 'newToken']) - .middleware('auth:jwt'); + .use(middleware.auth()); // // Service info router .post('service', [ServiceController, 'create']) - .middleware('auth:jwt'); + .use(middleware.auth()); router .put('service/reorder', [ServiceController, 'reorder']) - .middleware('auth:jwt'); + .use(middleware.auth()); router .put('service/:id', [ServiceController, 'edit']) - .middleware('auth:jwt'); + .use(middleware.auth()); router .delete('service/:id', [ServiceController, 'delete']) - .middleware('auth:jwt'); + .use(middleware.auth()); router .get('me/services', [ServiceController, 'list']) - .middleware('auth:jwt'); - router.get('recipe', [ServiceController, 'list']).middleware('auth:jwt'); + .use(middleware.auth()); + router.get('recipe', [ServiceController, 'list']).use(middleware.auth()); router.get('icon/:id', [ServiceController, 'icon']); // Recipe store @@ -55,16 +60,16 @@ router // // Workspaces router .put('workspace/:id', [WorkspaceController, 'edit']) - .middleware('auth:jwt'); + .use(middleware.auth()); router .delete('workspace/:id', [WorkspaceController, 'delete']) - .middleware('auth:jwt'); + .use(middleware.auth()); router .post('workspace', [WorkspaceController, 'create']) - .middleware('auth:jwt'); + .use(middleware.auth()); router .get('workspace', [WorkspaceController, 'list']) - .middleware('auth:jwt'); + .use(middleware.auth()); // Static responses router.get('features/:mode?', [FeaturesController, 'show']); diff --git a/start/routes/web.ts b/start/routes/web.ts index a944c05..f78585c 100644 --- a/start/routes/web.ts +++ b/start/routes/web.ts @@ -1,3 +1,4 @@ +import { middleware } from '#start/kernel'; import router from '@adonisjs/core/services/router'; const HealthController = () => import('#controllers/Http/HealthController'); const LoginController = () => @@ -47,7 +48,8 @@ router router.get('reset', [ResetPasswordController, 'show']); router.post('reset', [ResetPasswordController, 'resetPassword']); }) - .middleware(['dashboard', 'guest']); + .use(middleware.dashboard()) + .use(middleware.guest()); // Authenticated routes router @@ -70,7 +72,8 @@ router response.redirect('/user/account'), ); }) - .middleware(['dashboard', 'auth:web']); + .use(middleware.dashboard()) + .use(middleware.auth()); }) .prefix('user'); @@ -81,4 +84,4 @@ router // 404 handler router.get('/*', ({ response }) => response.redirect('/')); }) - .middleware(['dashboard']); + .use(middleware.dashboard()); diff --git a/tests/bootstrap.ts b/tests/bootstrap.ts index d152db0..83f8ca5 100644 --- a/tests/bootstrap.ts +++ b/tests/bootstrap.ts @@ -12,6 +12,7 @@ import { apiClient } from '@japa/api-client'; import app from '@adonisjs/core/services/app'; import type { Config } from '@japa/runner/types'; import testUtils from '@adonisjs/core/services/test_utils'; +import { authApiClient } from '@adonisjs/auth/plugins/api_client'; import { fakeCsrfField } from './utils.js'; @@ -31,6 +32,7 @@ export const plugins: Config['plugins'] = [ assert(), apiClient(), pluginAdonisJS(app), + authApiClient(app), ]; /* -- cgit v1.2.3-70-g09d2