Initial commit: PC Builder AdonisJS project
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import type { NextFn } from '@adonisjs/core/types/http'
|
||||
import type { Authenticators } from '@adonisjs/auth/types'
|
||||
|
||||
export default class AuthMiddleware {
|
||||
redirectTo = '/auth/login'
|
||||
|
||||
async handle(
|
||||
ctx: HttpContext,
|
||||
next: NextFn,
|
||||
options: { guards?: (keyof Authenticators)[] } = {}
|
||||
) {
|
||||
await ctx.auth.authenticateUsing(options.guards, { loginRoute: this.redirectTo })
|
||||
return next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Logger } from '@adonisjs/core/logger'
|
||||
import { HttpContext } from '@adonisjs/core/http'
|
||||
import { type NextFn } from '@adonisjs/core/types/http'
|
||||
|
||||
/**
|
||||
* The container bindings middleware binds classes to their request
|
||||
* specific value using the container resolver.
|
||||
*
|
||||
* - We bind "HttpContext" class to the "ctx" object
|
||||
* - And bind "Logger" class to the "ctx.logger" object
|
||||
*/
|
||||
export default class ContainerBindingsMiddleware {
|
||||
handle(ctx: HttpContext, next: NextFn) {
|
||||
ctx.containerResolver.bindValue(HttpContext, ctx)
|
||||
ctx.containerResolver.bindValue(Logger, ctx.logger)
|
||||
|
||||
return next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import type { NextFn } from '@adonisjs/core/types/http'
|
||||
import type { Authenticators } from '@adonisjs/auth/types'
|
||||
|
||||
/**
|
||||
* Guest middleware is used to deny access to routes that should
|
||||
* be accessed by unauthenticated users.
|
||||
*
|
||||
* For example, the login page should not be accessible if the user
|
||||
* is already logged-in
|
||||
*/
|
||||
export default class GuestMiddleware {
|
||||
/**
|
||||
* The URL to redirect to when user is logged-in
|
||||
*/
|
||||
redirectTo = '/computers'
|
||||
|
||||
async handle(
|
||||
ctx: HttpContext,
|
||||
next: NextFn,
|
||||
options: { guards?: (keyof Authenticators)[] } = {}
|
||||
) {
|
||||
for (let guard of options.guards || [ctx.auth.defaultGuard]) {
|
||||
if (await ctx.auth.use(guard).check()) {
|
||||
ctx.session.reflash()
|
||||
return ctx.response.redirect(this.redirectTo, true)
|
||||
}
|
||||
}
|
||||
|
||||
return next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import type { NextFn } from '@adonisjs/core/types/http'
|
||||
import UserTransformer from '#transformers/user_transformer'
|
||||
import BaseInertiaMiddleware from '@adonisjs/inertia/inertia_middleware'
|
||||
|
||||
export default class InertiaMiddleware extends BaseInertiaMiddleware {
|
||||
share(ctx: HttpContext) {
|
||||
/**
|
||||
* The share method is called everytime an Inertia page is rendered. In
|
||||
* certain cases, a page may get rendered before the session middleware
|
||||
* or the auth middleware are executed. For example: During a 404 request.
|
||||
*
|
||||
* In that case, we must always assume that HttpContext is not fully hydrated
|
||||
* with all the properties
|
||||
*/
|
||||
const { session, auth } = ctx as Partial<HttpContext>
|
||||
|
||||
/**
|
||||
* Fetching the first error from the flash messages
|
||||
*/
|
||||
const error = session?.flashMessages.get('error') as string
|
||||
const success = session?.flashMessages.get('success') as string
|
||||
|
||||
/**
|
||||
* Data shared with all Inertia pages. Make sure you are using
|
||||
* transformers for rich data-types like Models.
|
||||
*/
|
||||
return {
|
||||
errors: ctx.inertia.always(this.getValidationErrors(ctx)),
|
||||
flash: ctx.inertia.always({
|
||||
error,
|
||||
success,
|
||||
}),
|
||||
user: ctx.inertia.always(auth?.user ? UserTransformer.transform(auth.user) : undefined),
|
||||
}
|
||||
}
|
||||
|
||||
async handle(ctx: HttpContext, next: NextFn) {
|
||||
await this.init(ctx)
|
||||
|
||||
const output = await next()
|
||||
this.dispose(ctx)
|
||||
|
||||
return output
|
||||
}
|
||||
}
|
||||
|
||||
declare module '@adonisjs/inertia/types' {
|
||||
type MiddlewareSharedProps = InferSharedProps<InertiaMiddleware>
|
||||
export interface SharedProps extends MiddlewareSharedProps {}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import type { NextFn } from '@adonisjs/core/types/http'
|
||||
|
||||
/**
|
||||
* Silent auth middleware can be used as a global middleware to silent check
|
||||
* if the user is logged-in or not.
|
||||
*
|
||||
* The request continues as usual, even when the user is not logged-in.
|
||||
*/
|
||||
export default class SilentAuthMiddleware {
|
||||
async handle(ctx: HttpContext, next: NextFn) {
|
||||
await ctx.auth.check()
|
||||
|
||||
return next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user