run_query = fn query_def -> Process.sleep(2000); "#{query_def} result" end
async_query = fn query_def -> spawn(fn -> IO.puts(run_query.(query_def)) end) end
Enum.each(1..5, &async_query.("query #{&1}"))
| # first way | |
| # frozen_string_literal: true | |
| class EncryptionService | |
| delegate :encrypt_and_sign, :decrypt_and_verify, to: :encryptor | |
| class << self | |
| def encrypt(secret:) | |
| new.encrypt_and_sign(secret) | |
| end |
This is just some code I recently used in my development application in order to add token-based authentication for my api-only rails app. The api-client was to be consumed by a mobile application, so I needed an authentication solution that would keep the user logged in indefinetly and the only way to do this was either using refresh tokens or sliding sessions.
I also needed a way to both blacklist and whitelist tokens based on a unique identifier (jti)
Before trying it out DIY, I considered using:
Customer.first.groups Customer Load (1.3ms) SELECT "customers".* FROM "customers" WHERE "customers"."deleted_at" IS NULL ORDER BY "customers"."id" ASC LIMIT $1 [["LIMIT", 1]]
Group Load (1.4ms) SELECT "groups".* FROM "groups" WHERE "groups"."deleted_at" IS NULL AND "groups"."customer_id" = $1 LIMIT $2 [["customer_id", 1], ["LIMIT", 11]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Group id: 3, name: "Admin", created_at: "2023-03-23 19:29:42", updated_at: "2023-03-23 19:29:42", customer_id: 1, deleted_at: nil, local_id: "71877852-23aa-43fa-8ed0-ea46ed4b76e2", threshold_quantity: nil>, #<Group id: 13, name: "prueba", created_at: "2023-06-07 22:08:25", updated_at: "2023-06-07 22:08:25", customer_id: 1, deleted_at: nil, local_id: "b4f5e733-2571-49c8-a8bb-3352085c410a", threshold_quantity: nil>]>I recently spent some time dealing with nested forms in Phoenix. Nested forms are great when you want to create multiple database records in a single transaction and associate them with each other. I am new to Phoenix and really struggled to find any resources that helped me with my specific problem. I decided to document what I learned in the process in hopes of helping others that are new to Elixir and Phoenix.
Here is my attempt at a one stop shop to learn everything you will need to know about nested forms. If you would like to view the GitHub repo you can check it out here.
Thanks to Heartbeat and Jose for excellent blog posts on nested forms. Also shoutout to Josh for showing me some examples at Ruby
| require 'bundler/inline' | |
| gemfile(true) do | |
| source 'https://rubygems.org' | |
| gem 'rails', '7.2.0' | |
| gem 'activemodel', '~> 7.2' | |
| gem 'sqlite3', '2.5.0' | |
| gem 'byebug' | |
| end |
| class User < ApplicationRecord | |
| after_commit :send_notification | |
| def send_notification | |
| puts "sendnotification" | |
| ActiveSupport::Notifications.instrument("updated.user", { user: self }) | |
| end | |
| end | |
| # app/subscribers/user_event_subscriber.rb |
I have been using the devise gem with rails to implement user authentication for may applications. When implementing authentication though API requests, then I turn to the devise-jwt gem. I faced an issue when implementing refresh tokens with devise, as devise does not support access tokens. I still needed the superior authentication that devise provides, but needed to manually work with fresh tokens. After looking around the web, here is how I was able to modify the code to implement refresh tokens with devise-jwt.
Take for the example a user model to sore the user email and password.