Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 6cd0f10

Browse files
committed
refactor: some changes here and there
1 parent 234dd45 commit 6cd0f10

File tree

14 files changed

+60
-17
lines changed

14 files changed

+60
-17
lines changed

website/.env.example

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ SMTP_PORT=1025
2222
# Rate limiting
2323
LIMITER_STORE=memory
2424

25+
# Redis
26+
REDIS_HOST=127.0.0.1
27+
REDIS_PORT=6379
28+
REDIS_PASSWORD=
29+
2530
# Ally
2631
GITHUB_CLIENT_ID=********
2732
GITHUB_CLIENT_SECRET=********
@@ -34,6 +39,4 @@ LINKEDIN_CLIENT_SECRET=********
3439
INERTIA_PUBLIC_TZ=Europe/Lisbon
3540
INERTIA_PUBLIC_EVENT_COUNTDOWN_DATE=2025-04-11
3641
INERTIA_PUBLIC_APP_URL=http://127.0.0.1:3333
37-
REDIS_HOST=127.0.0.1
38-
REDIS_PORT=6379
39-
REDIS_PASSWORD=
42+
INERTIA_PUBLIC_AUTH_ENABLED=true

website/app/controllers/authentication_controller.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ export default class AuthenticationController {
6262
return response.redirect().toRoute('pages:auth.verify')
6363
}
6464

65-
async callbackForEmailVerification({ request, view, response }: HttpContext) {
66-
if (request.method() !== 'POST') return view.render('automatic_submit')
67-
65+
async callbackForEmailVerification({ request, response }: HttpContext) {
6866
const { email } = await request.validateUsing(emailVerificationCallbackValidator)
6967
await this.userService.verifyEmail(email)
7068

website/app/exceptions/handler.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ export default class HttpExceptionHandler extends ExceptionHandler {
1616
*/
1717
protected renderStatusPages = app.inProduction
1818

19+
protected ignoreCodes = []
20+
protected ignoreStatuses = []
21+
protected ignoreExceptions = []
22+
1923
/**
2024
* Status pages is a collection of error code range and a callback
2125
* to return the HTML contents to send as a response.
2226
*/
2327
protected statusPages: Record<StatusPageRange, StatusPageRenderer> = {
28+
'401': (_error, { response }) => response.status(200).send("Yikes, you're not allowed to do that"),
2429
'404': (error, { inertia }) => inertia.render('errors/not_found', { error }),
2530
'500..599': (error, { inertia }) => inertia.render('errors/server_error', { error }),
2631
}
@@ -30,6 +35,7 @@ export default class HttpExceptionHandler extends ExceptionHandler {
3035
* response to the client
3136
*/
3237
async handle(error: unknown, ctx: HttpContext) {
38+
console.log("handling", error)
3339
return super.handle(error, ctx)
3440
}
3541

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ReactNotification } from './base/react_notification.js'
2-
import type { EmailVerificationProps } from '#resources/emails/authentication/email_verification'
2+
import type { EmailVerificationProps } from '#resources/emails/auth/email_verification'
33

44
export default class EmailVerificationNotification extends ReactNotification {
55
constructor(private props: EmailVerificationProps) {
@@ -9,6 +9,6 @@ export default class EmailVerificationNotification extends ReactNotification {
99
async prepare() {
1010
this.message.to(this.props.email).subject('Confirma o teu e-mail!')
1111

12-
await this.jsx(() => import('#resources/emails/authentication/email_verification'), this.props)
12+
await this.jsx(() => import('#resources/emails/auth/email_verification'), this.props)
1313
}
1414
}

website/app/middleware/automatic_submit_middleware.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import type { HttpContext } from '@adonisjs/core/http'
22
import type { NextFn } from '@adonisjs/core/types/http'
33

44
export default class AutomaticSubmitMiddleware {
5-
async handle(_ctx: HttpContext, next: NextFn) {
6-
// const method = request.method()
7-
// if (method === "POST") return next()
8-
return next()
9-
// return view.render('automatic_submit')
5+
async handle({ request, response, view }: HttpContext, next: NextFn) {
6+
const method = request.method()
7+
if (method === "POST") return next()
8+
9+
// Clever hack by virk to render Edge.js templates in middlewares
10+
return response.status(200).send(await view.render('automatic_submit'))
1011
}
1112
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import env from '#start/env'
2+
import type { HttpContext } from '@adonisjs/core/http'
3+
import type { NextFn } from '@adonisjs/core/types/http'
4+
5+
export default class LogoutIfAuthenticationDisabledMiddleware {
6+
async handle(ctx: HttpContext, next: NextFn) {
7+
if (!env.get('INERTIA_PUBLIC_AUTHENTICATION_ENABLED')) {
8+
await ctx.auth.use('web').logout()
9+
}
10+
11+
return next()
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import env from '#start/env'
2+
import type { HttpContext } from '@adonisjs/core/http'
3+
import type { NextFn } from '@adonisjs/core/types/http'
4+
5+
export default class RequireAuthEnabledMiddleware {
6+
async handle({ response }: HttpContext, next: NextFn) {
7+
if (env.get("INERTIA_PUBLIC_AUTHENTICATION_ENABLED")) {
8+
return next()
9+
}
10+
11+
return response.unauthorized()
12+
}
13+
}

website/inertia/css/app.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
--background: 0 0% 100%;
1111
--foreground: 222.2 84% 4.9%;
12-
/* --card: 0 0% 100%; */
13-
--card: var(--enei-beige);
12+
--card: 0 0% 100%;
1413
/* --card-foreground: 222.2 84% 4.9%; */
1514
--card-foreground: var(--enei-blue);
1615
--popover: 0 0% 100%;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Unauthorized({ error }: { error: any }) {
2+
return (JSON.stringify(error))
3+
}

website/resources/emails/common/layouts/base.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const BaseLayout = ({
1111
}) => {
1212
return (
1313
<Tailwind>
14-
<Html className={cn("mt-[16px] font-sans bg-secondary", className)}>{children}</Html>
14+
<Html className={cn("mt-[16px] font-sans", className)}>{children}</Html>
1515
</Tailwind>
1616
)
1717
}

website/start/env.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const env = await defineEnv(new URL('../', import.meta.url), 'INERTIA_PUBLIC_',
3232
*/
3333
FROM_EMAIL: vine.string(),
3434
REPLY_TO_EMAIL: vine.string().optional(),
35-
35+
3636
SMTP_HOST: vine.string(),
3737
SMTP_PORT: vine.string(),
3838
//AWS_ACCESS_KEY_ID: vine.string(),
@@ -82,6 +82,10 @@ const env = await defineEnv(new URL('../', import.meta.url), 'INERTIA_PUBLIC_',
8282
INERTIA_PUBLIC_TZ: vine.string(),
8383
INERTIA_PUBLIC_EVENT_COUNTDOWN_DATE: vine.string(),
8484
INERTIA_PUBLIC_APP_URL: vine.string(),
85+
INERTIA_PUBLIC_AUTHENTICATION_ENABLED: vine
86+
.boolean({ strict: false })
87+
.optional()
88+
.transform((val) => val ?? false),
8589
})
8690
})
8791

website/start/kernel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ router.use([
3939
() => import('@adonisjs/session/session_middleware'),
4040
() => import('@adonisjs/shield/shield_middleware'),
4141
() => import('@adonisjs/auth/initialize_auth_middleware'),
42+
() => import('#middleware/logout_if_authentication_disabled_middleware'),
4243
])
4344

4445
/**
4546
* Named middleware collection must be explicitly assigned to
4647
* the routes or the routes group.
4748
*/
4849
export const middleware = router.named({
50+
requireAuthenticationEnabled: () => import('#middleware/require_authentication_enabled_middleware'),
4951
verifyUrlSignature: () => import('#middleware/verify_url_signature_middleware'),
5052
automaticSubmit: () => import('#middleware/automatic_submit_middleware'),
5153
redirectIfAuthenticated: () => import('#middleware/redirect_if_authenticated_middleware'),

website/start/routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,5 @@ router
9898
// .middleware(middleware.verifySocialCallback({ provider: 'linkedin' }))
9999
// .as('actions:auth.linkedin.callback')
100100
})
101+
.middleware(middleware.requireAuthenticationEnabled())
101102
.prefix('/auth')

0 commit comments

Comments
 (0)