Skip to content

Instantly share code, notes, and snippets.

View tomgobich's full-sized avatar

Tom Gobich tomgobich

View GitHub Profile
@tomgobich
tomgobich / auth.ts
Last active January 11, 2025 12:37
AdonisJS Web & API Guard Configuration
// config/auth.ts
import { defineConfig } from '@adonisjs/auth'
import { sessionGuard, sessionUserProvider } from '@adonisjs/auth/session'
import type {
InferAuthEvents,
Authenticators as AuthenticatorEvents,
InferAuthenticators,
} from '@adonisjs/auth/types'
import { tokensGuard, tokensUserProvider } from '@adonisjs/auth/access_tokens'
@tomgobich
tomgobich / foreward.md
Created September 11, 2024 13:12
Adocasts VSCode Configuration
@tomgobich
tomgobich / movies.ts
Last active March 9, 2024 12:06
List of random movies from the National Film Registry (1989-2017) normalized to typescript/javascript
export const movies = [{
title: "3:10 to Yuma",
releaseYear: 1957
}, {
title: "The 7th Voyage of Sinbad",
releaseYear: 1958
}, {
title: "12 Angry Men",
releaseYear: 1957
}, {
@tomgobich
tomgobich / settings.json
Created March 31, 2023 13:08
Adocasts VCCode Settings JSON
{
"editor.fontFamily": "'Operator Mono Light','Dank Mono','Cascadia Code Light', Menlo, Monaco, 'Courier New', monospace",
"workbench.colorTheme": "GitHub Blue",
"editor.inlineSuggest.enabled": true,
"workbench.iconTheme": "minimal-icons-without-explorer-arrows",
"editor.tabSize": 2,
"workbench.statusBar.visible": false,
"workbench.fontAliasing": "auto",
"workbench.tips.enabled": false,
"breadcrumbs.enabled": false,
export default class ExampleController {
public async index({ request, response }: HttpContextContract) {
const data = await request.validate(ExampleValidator);
const domain = psl.get(data.host);
const agent = request.headers()['user-agent'] || data.agent;
const { browser, browserVersion, os } = PlatformService.parse(agent);
// ...
}
}
@tomgobich
tomgobich / Guest.ts
Last active December 17, 2021 02:20
AdonisJS Guest Middleware Example
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
export default class Guest {
public async handle({ auth, response }: HttpContextContract, next: () => Promise<void>) {
if (auth.user) {
// redirect user where ever you'd like
return response.redirect('/');
}
await next()
/*
* This middleware will take in a user relation to check against.
* It will then check:
* 1. Is there an authenticated user? If not, Unauthorized access is thrown
* 2. It'll then check if the authenticated user is either an admin or the record owner. If not, Access denied is thrown
*
* Ownership here is enforced by query off the authenticated user's relationships
*/
import { AuthenticationException } from '@adonisjs/auth/build/standalone'
public async register({ request, response, auth, session }: HttpContextContract) {
const validationSchema = schema.create({
username: schema.string({ trim: true }, [
rules.maxLength(50),
rules.minLength(3),
rules.unique({ table: 'users', column: 'username' }),
rules.regex(/^[a-zA-Z0-9-_]+$/),
rules.notIn(['admin', 'super', 'power', 'jagr', 'jagrco', '_jagr', '_jagrco', 'jagr_', 'jagrco_', 'jagr-co', 'moderator', 'public', 'dev', 'alpha', 'mail'])
]),
email: schema.string({ trim: true }, [rules.unique({ table: 'users', column: 'email' })]),
@tomgobich
tomgobich / nuxt.config.js
Created December 2, 2020 23:17
Nuxt config to deploy in SPA mode to Amplify
export default {
// Disable server-side rendering (https://go.nuxtjs.dev/ssr-mode)
ssr: false,
// Global page headers (https://go.nuxtjs.dev/config-head)
head: {
title: 'throwaway',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
@tomgobich
tomgobich / gist:aa0c5a555e6beb4b5d7ffa27bd09c57a
Created July 25, 2020 17:16
AWS Amplify example of how to SignUp a user when using a username and email
async signUp({ username, password, email }) {
try {
const user = await Auth.signUp({
username,
password,
attributes: {
email
},
validationData: []
});