From 5173e8d4c928e997c3f0cb4f1102698a55c916ce Mon Sep 17 00:00:00 2001 From: Sockenklaus Date: Wed, 20 Oct 2021 01:21:44 +0200 Subject: [PATCH] worked on authController --- .adonisrc.json | 6 ++ .env.example | 5 ++ app/Controllers/Http/AuthController.ts | 13 ++-- config/cors.ts | 4 +- config/static.ts | 89 ++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 config/static.ts diff --git a/.adonisrc.json b/.adonisrc.json index c1edcc9..7f1fbd9 100644 --- a/.adonisrc.json +++ b/.adonisrc.json @@ -29,5 +29,11 @@ ], "aceProviders": [ "@adonisjs/repl" + ], + "metaFiles": [ + { + "pattern": "public/**", + "reloadServer": false + } ] } diff --git a/.env.example b/.env.example index c97d34a..5a23b39 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,6 @@ SESSION_DRIVER=cookie +DB_CONNECTION=sqlite +PORT=3333 +HOST=0.0.0.0 +NODE_ENV=development +DRIVE_DISK=local diff --git a/app/Controllers/Http/AuthController.ts b/app/Controllers/Http/AuthController.ts index 574714d..f390276 100644 --- a/app/Controllers/Http/AuthController.ts +++ b/app/Controllers/Http/AuthController.ts @@ -1,16 +1,21 @@ import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import Logger from '@ioc:Adonis/Core/Logger' export default class AuthController { public async login({auth, request, response}: HttpContextContract) { - const username = request.input('username') - const password = request.input('password') + const username = request.body().username + const password = request.body().password try { await auth.attempt(username, password) - response.ok("Login successful") + return response.ok({ + Message: 'Login successful!', + user: auth.user?.username, + role: auth.user?.role + }) } catch (error) { - return error + return response.forbidden('Unauthorized') } } diff --git a/config/cors.ts b/config/cors.ts index 7a5e490..bcb8755 100644 --- a/config/cors.ts +++ b/config/cors.ts @@ -20,7 +20,7 @@ const corsConfig: CorsConfig = { | you can define a function to enable/disable it on per request basis as well. | */ - enabled: false, + enabled: true, // You can also use a function that return true or false. // enabled: (request) => request.url().startsWith('/api') @@ -44,7 +44,7 @@ const corsConfig: CorsConfig = { | one of the above values. | */ - origin: true, + origin: 'http://localhost:3000', /* |-------------------------------------------------------------------------- diff --git a/config/static.ts b/config/static.ts new file mode 100644 index 0000000..de12456 --- /dev/null +++ b/config/static.ts @@ -0,0 +1,89 @@ +/** + * Config source: https://git.io/Jfefl + * + * Feel free to let us know via PR, if you find something broken in this config + * file. + */ + +import { AssetsConfig } from '@ioc:Adonis/Core/Static' + +const staticConfig: AssetsConfig = { + /* + |-------------------------------------------------------------------------- + | Enabled + |-------------------------------------------------------------------------- + | + | A boolean to enable or disable serving static files. The static files + | are served from the `public` directory inside the application root. + | However, you can override the default path inside `.adonisrc.json` + | file. + | + | + */ + enabled: true, + + /* + |-------------------------------------------------------------------------- + | Handling Dot Files + |-------------------------------------------------------------------------- + | + | Decide how you want the static assets server to handle the `dotfiles`. + | By default, we ignore them as if they don't exists. However, you + | can choose between one of the following options. + | + | - ignore: Behave as if the file doesn't exists. Results in 404. + | - deny: Deny access to the file. Results in 403. + | - allow: Serve the file contents + | + */ + dotFiles: 'ignore', + + /* + |-------------------------------------------------------------------------- + | Generating Etag + |-------------------------------------------------------------------------- + | + | Handle whether or not to generate etags for the files. Etag allows browser + | to utilize the cache when file hasn't been changed. + | + */ + etag: true, + + /* + |-------------------------------------------------------------------------- + | Set Last Modified + |-------------------------------------------------------------------------- + | + | Whether or not to set the `Last-Modified` header in the response. Uses + | the file system's last modified value. + | + */ + lastModified: true, + + /* + |-------------------------------------------------------------------------- + | Max age + |-------------------------------------------------------------------------- + | + | Set the value for the max-age directive. Set a higher value in production + | if you fingerprint your assets. + | + | Learn more: https://docs.adonisjs.com/guides/deployment#serving-static-assets + | + */ + maxAge: 0, + + /* + |-------------------------------------------------------------------------- + | Immutable + |-------------------------------------------------------------------------- + | + | Set the immutable directive. Set it to `true` if the assets are generated + | with a fingerprint. In others words the file name changes when the file + | contents change. + | + */ + immutable: false, +} + +export default staticConfig