Skip to content

Instantly share code, notes, and snippets.

View mhaagens's full-sized avatar

Martin mhaagens

  • AmmaCreative
  • Oslo, Norway
View GitHub Profile
@mhaagens
mhaagens / demo.ex
Last active February 1, 2023 11:00
defmodule DemoApp.User do
def get_user() do
with {:ok, user} <- Repo.query(%User{}),
{:ok, ^user} <- is_user_email_verified(user),
{:ok, ^user} <- is_user_subscribed(user) do
{:ok, user}
else
{:error, reason} -> {:err, reason}
_ -> {:error, "Something went wrong"}
end
@mhaagens
mhaagens / docker-compose.yml
Created July 1, 2019 07:59
Elasticsearch + App Search
version: "3"
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.2.0
environment:
- cluster.name=elasticsearch-oss
- discovery.type=single-node
- cluster.routing.allocation.disk.threshold_enabled=false
- bootstrap.memory_lock=true
- http.cors.enabled=true
@mhaagens
mhaagens / Dockerfile
Created October 25, 2018 11:04
Docker Wordpress Development
FROM nginx:latest
RUN echo "upstream php { server wordpress:9000; }" > /etc/nginx/conf.d/upstream.conf
COPY default.conf /etc/nginx/conf.d/default.conf
@mhaagens
mhaagens / authentication_middleware.js
Last active April 22, 2020 10:14
Auth middleware
import jwt from "jsonwebtoken";
import User from "@server/models/user_model";
const PRODUCTION = process.env.NODE_ENV === "production";
export default (options) => async (req, res, next) => {
const refreshToken = req.cookies["refresh_token"];
const accessToken = req.cookies["access_token"];
const csrfHeader = req.get("X-Csrf-Token");
@mhaagens
mhaagens / app.js
Created July 1, 2018 11:39
Authentication and authorization using GraphQL Schema Directives: src/app.js snippet
const server = new ApolloServer({
typeDefs,
resolvers,
schemaDirectives: {
requireAuth: requireAuthDirective
}
});
@mhaagens
mhaagens / requireAuthDirective.js
Last active February 15, 2019 13:34
Authentication and authorization using GraphQL Schema Directives: src/directives/requireAuthDirective.js
const {
SchemaDirectiveVisitor,
AuthenticationError
} = require("apollo-server");
class RequireAuthDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
const { resolve = defaultFieldResolver } = field;
const { role } = this.args;
field.resolve = async function(...args) {
@mhaagens
mhaagens / post.js
Last active July 2, 2018 22:10
Authentication and authorization using GraphQL Schema Directives: src/models/post.js
const { gql } = require("apollo-server");
const mockPosts = [
{ id: 1, title: "Helvetica and Times New Roman walk into a bar. Get out of here! shouts the bartender. We don't serve your type!", ownerId: 1 },
{ id: 2, title: "Why do we tell actors to break a leg? Because every play has a cast.", ownerId: 2 },
{ id: 3, title: "Did you hear about the mathematician who’s afraid of negative numbers? He'll stop at nothing to avoid them.", ownerId: 1 }
];
module.exports.postDefs = gql`
extend type Query {
@mhaagens
mhaagens / schema.js
Last active July 2, 2018 21:29
Authentication and authorization using GraphQL Schema Directives: schema.js
const { gql } = require("apollo-server");
const _ = require("lodash");
const { postDefs, postResolvers } = require("./models/post.js");
module.exports.typeDefs = gql`
enum Role {
ADMIN
USER
}
@mhaagens
mhaagens / dev_server.js
Created July 1, 2018 10:36
Authentication and authorization using GraphQL Schema Directives: dev_server.js
const path = require("path");
const chokidar = require("chokidar");
const reload = require("require-reload")(require);
let server = reload("./src/app.js");
const srcDir = path.resolve(__dirname, "src");
const watcher = chokidar.watch(srcDir + "/**/*.js", {});
watcher.on("ready", () =>
server
@mhaagens
mhaagens / app.js
Last active July 2, 2018 21:28
Authentication and authorization using GraphQL Schema Directives: src/app.js
const { ApolloServer } = require("apollo-server");
const { typeDefs, resolvers } = require("./schema.js");
const requireAuthDirective = require("./directives/requireAuthDirective");
const server = new ApolloServer({
typeDefs,
resolvers,
schemaDirectives: {
requireAuth: requireAuthDirective
},